How To Resize Images With CSS – Keeping Aspect Ratio As It Is: Quick & Easy

Add a comment July 17th, 2008

I found a quick and easy way to re-size 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 re-size the images by keeping height as 300 pixels. You can use the following classes in <IMG> tags like:

<img src="http://mywbsite.com/myphoto.jpg"  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. ;)

Anyways you can use any image re-size script with server side script technology like PHP. If you find any fast script, please share a link in comments.

Share or Bookmark this Post:
  • StumbleUpon
  • Digg
  • Twitter
  • del.icio.us
  • Facebook
  • Mixx
  • MySpace
  • Reddit
  • Google Bookmarks
  • Yahoo! Buzz
  • Technorati
  • Live
  • DZone
  • Netvibes
  • RSS
  • Print

 

  1. December 17th, 2009 at 17:24 | #1

    Simple, works a treat

  2. January 15th, 2010 at 00:34 | #2
    David

    It looks like max-width and max-height are what work for me.

  3. January 22nd, 2010 at 22:02 | #3

    Hello,

    I stumbled upon your site when i was stuck on resizing images via CSS.

    Your code works perfect on my personal website.

    Thanks for you contribution!

  4. January 28th, 2010 at 01:14 | #4

    I used your approach in one of my website and got good results. However when the width of one picture is less than 200px then it is not giving good results. Do you have any idea on this? For large picture picture having width equals or greater than 200px, this works great.

  5. February 16th, 2010 at 15:52 | #5
    Christine

    Amazing! Who needs Javascript. If I'd only thought about that earlier…:). Thanks.

  6. April 9th, 2010 at 14:44 | #6
    punund

    Doesn't work with Opera.

  7. April 25th, 2010 at 23:40 | #7

    I tried all kinds of ways, as others suggested, and tried different browsers, but I can't get this to work at all. Perhaps I misunderstand what you're trying you do.

    What I thought was going to happen that a large image would get scaled down as you changed the size of the viewing window. But it doesn't. The image is scaled as per "isize" css and remain fixed.

    Any idea how I could accomplish what I would like to do?

    Thanks,

  8. May 5th, 2010 at 19:08 | #8
    Trax

    Great ,, it's done .
    Thanks sir .

  9. June 5th, 2010 at 00:38 | #9
    Greg

    Thank you very much, great trick!

  10. August 9th, 2010 at 14:22 | #10
    Michal

    I need to resize an image for the background in inline CSS for a facebook fanpage box – nothing is working -what would the code look like?

  11. August 24th, 2010 at 22:14 | #11

    that is css genius

  1. |
    December 23rd, 2009 at 09:31 | #1

    [...] and links of each component of the social events app. Also, major thanks this holiday season for dynamic image resizing with CSS. (Please note: our users submitted the messages, not the [...]

Comments feed
Real Time Web Analytics