How to Resize the Images using CSS without losing the Aspect Ratio?

Sat, Aug 2, 2008

Tech, Tiny Tip, Tutorial  




Yesterday, if you have noticed, my blog was down for few hours. This was because I had problems with my Wordpress theme with sidebars getting messed up and found it hard to fix it up. I finally managed to debug this issue and found out it was all due to image over-sizing in my latest post. I googled a lot to find a fix for auto resizing images in wordpress, but most of them were either blog theme specific or were controlling the image size before image upload.

Then I found out a quick and easy way to rezise images with CSS by keeping your image’s aspect ratio. This is very quick you can use it in less than 5 minutes, lets proceed!

Lets say you want to show large images on your web page with maximum 200 pixels width, blog or forums just create the following css class into your styling css file:

.resize {
width: 200px;
height : auto;
}

.resize {
width: auto;
height : 300px;
}

Second class will resize the images by keeping height as 300 pixels. You can use the following classes in <IMG> tags like:

<img src="http://mywebsite.com/myphoto.jpg"  class="resize" alt="my photo">

The above solution is always better than putting width and height attributes into the <IMG> tag. Hope it works out for you too


If you enjoyed this post, make sure you subscribe to my RSS feed!

Related Posts:

  1. Quickly Resize PNG Images Online and Retain Transparency
  2. Top 5 Facebook Apps for Images
  3. Enlarge Your Digital Photographs Without Losing Quality
  4. Resizing Images Online for Free with My-Image-Size
  5. Find Similar Images Using Google Image Search





, , ,

9 Comments For This Post

  1. techniqueal t. Says:

    OMG. and here i was, manually as in painstakingly and manually placing width and height attributes on almost ALL of my images just to optimize them.

    fantastic tip! dugg it, bookmarked it, love it! ^_^

    techniqueal t.s last blog post..ShowOffRankings: Blog Widget To Show Blog Rankings

    [Reply]

  2. Raju Says:

    Thanks a lot buddy :)

    [Reply]

  3. stratosg Says:

    the only problem with this is if the image is 2Mb then the whole thing will be downloaded and then bowser-side resized. so it’s useful to an extent of sane usage ;)

    stratosgs last blog post..Search comments with “Search Reloaded”

    [Reply]

  4. Raju Says:

    wow! obviously i didnt know this Stratos. I have been using this for the past 3 months without a problem, must be within that sane usage range then !

    [Reply]

  5. 310 Says:

    WHERE EXACTLY DO I PUT THIS IN MY CSS?

    [Reply]

    Raju Reply:

    @310,
    Inside your content body, there will be”img” tag. Under that you need to have “resize” defined.

    [Reply]

  6. Gareth Says:

    Im having abit of trouble… when I put the code into the CSS file and the ‘class=”resize”‘ in the html tag, it only seems to resize the images height wise to a certain pixel but not width wise, its as if its almost one or the other, which defeats the object of the code.

    I want code to make individual images load into a 650px x 500px div, resizing images accordingly depending on their dimensions. Am I looking in completely the wrong place?

    [Reply]

  7. Gareth Says:

    Found what I was looking for….

    http://archivist.incutio.com/viewlist/css-discuss/40356

    [Reply]

  8. bimbolera Says:

    this helped me but in a separate website. i have a problem regarding with plugins (for banner) in wordpress, different banners has its own ups and downs, i have found a new plugin and its great but the catch is, it doesn’t have the automatic resize feature. i do hope i can fix this bug!

    [Reply]

  9. Trackbacks Says:

    [Reply]

Leave a Reply