October 23, 2007
Setting a maximum width for images in a content area
Sometimes you want to specify a maximum width images should be, mostly to prevent them from overflowing out of the content area of your website. Maybe you don’t want to resize your images - or your client doesn’t have a clue how to use an image editor. In any case, this is the code you need:
img {
max-width: 525px;
/* Change 525 to the desired width value */
}
img {
_width: expression(this.width > 525 ? 525: true);
/* IE6 fix for max-width */
/* Change both 525s to the desired width value */
}
Some notes:
- It’s a good idea to specify something along the lines of
#content imginstead of justimg, since other images on the website might be wider. - It’s a much better idea to resize images before uploading, or resize the images when uploaded through tools like PHPThumb.
- It’s also recommended to remove the underscore hack and place the second statement in your conditional comments for IE6.
- IE expressions ignore media types, so use with caution.
Any insights on the often tight coupling between images in the content area and the layout?
I can easily imagine a situation in which all images (part of the content) have the exact same width of the content area. But after a redesign they now don’t fit the layout anywore, either they are too large or too small or their flow is now flawed (i.e. when positioning a set of images/thumbnails) . This often means that you are forced to go through every post/page and adjust or reposition the images.
November 27th, 2007 at 1:20 ∞