Auto Resize Large Images in Blog Post

16 Feb
2010

I ran into this problem recently and found a good fix. As we make custom CMS, blogs as parts of websites, we frequently use some WYSIWYG editors like TINYMCE. Now when you enable people to post contents along with image urls, people may enter an image url which possibly is much larger than the available width we have in the page layout. Perhaps most of the images may become larger as we would only get around 650px of width when we are using 960px standard layout with some sort of sidebars.

So the user’s posted image in the blog post may overflow the layout. What to do then ??

1. We want to keep the image within the available width (may be 600px).

2. We also want to keep the aspect ratio intact so that the dimension doesn’t get messed up.

After googling in some pages I had to combine the CSS solution which works for most of the modern browsers (certainly not IE6).

So here’s the neat solution totally in CSS.

#post_view img {

max-width: 620px;

height: auto !important;

width: expression(this.width > 620 ? 620: true);

}

The above snippet assumes that your ID for the container (i.e DIV) is post_view. I’d discourage using class here because ID has a higher order of preference when it comes to override CSS rules.

So, all the image who exceeds the width of 620px will be automatically resized to 620px. Images below 620px will not be affected at all. The height: auto !important; will keep the aspect ratio of the image to the original ratio.

Compatibility:

Firefox 3

Opera 10

IE 7 (I don’t care if it works in IE 6 or not)

Chrome

Safari 4 (win) [ Don't know about Mac cuz i don't have any :( ]

EXAMPLE :

example image

12 Responses to Auto Resize Large Images in Blog Post

Avatar

nurul amin russel

March 1st, 2010 at 3:34 pm

useful solution – thanks

Avatar

Kazi Abdullah Al Mamun

March 17th, 2010 at 12:28 pm

Really useful solution!!!

Avatar

tectiv3

May 25th, 2010 at 11:58 pm

Thank you!

Avatar

diego

August 25th, 2010 at 7:26 pm

Success! Thanks a lot!

Avatar

ddlmachine

November 17th, 2010 at 8:12 am

Actually it didn’t work when I used ID, but when I changed to Class it worked like a charm. I’m using Blogger.

Big thanks, very useful :)

Avatar

Ricka

January 17th, 2011 at 5:27 pm

I’m using blogger with Firefox 3.6.13.
This code can’t work :(
Help me please……

Avatar

Mahbub

January 17th, 2011 at 5:30 pm

Can you give me your URL where it’s not working ? May be i can help using firebug !

Avatar

agin

February 24th, 2011 at 12:51 am

Really useful solution, Thanks :)

Avatar

medo

April 26th, 2011 at 9:52 pm

thank u man i had that problem.. many thanks

Avatar

jeo12

May 2nd, 2011 at 12:34 pm

i have tried many ways but this is the best, the easiest … thank u man

Avatar

Andrew West

October 20th, 2011 at 5:47 pm

This is useful. One of the techniques that work, correctly.

Avatar

eb5 visa

January 3rd, 2012 at 6:04 pm

Success! Thanks a lot!

Comment Form

top