04.23
So I was chatting yesterday with a friend (DeZign Diva, check her site out!) about one of her latest inspiration posts, and in glancing at it, I saw that she was adjusting the size of her images in the post code and this was causing confusion and clutter and squishing her images at times. “There’s got to be a better way,” I said to myself. And Lo! A few moments later I realized that it is quite easy to control image size and location without having to add items to the html code in your posting.
Using my blog for an example, my post section is 780px wide has a 20px right margin, then the sidebar is 160px wide (based off the 960 grid model best described here. I got rid of the exterior margins though, so I could add another 20px to my post section). Now you want to be able to control the size of your images but keep the aspect ratio…in HTML that means adding a width and height adjustment to each one by doing the math (the way Mia was doing it). More simply, you can replace whatever the height is with height=“auto” and this will constrain the height to the width in the proper proportions. But you still have to add that to every img tag. If you have access to your style sheets this can be controlled globally across all your posts(past and future!) with 1 line of code. Boom! (do you prefer Whammy!?) you have uniform image widths throughout all your posts without affecting any other image on your site.
Example from my blog:div.entry img{
max-width:580px;
height: auto;
}
Original:

CSS Controlled:

This works because of the specificity call to the img selector, and the CSS interprets the original aspect ratio, and retains in relation to the new max-width through the “auto” value. Specificity is a rule within CSS that very basically decides which is more important by the parent selectors added to a line of code. div.entry img{} overwrites img{} for any images within the div that has the class=“entry” attached to it. Any other img{} code on the site is kept the same. You don’t need to add div to the front, but doing so gives your custom code a higher specificity than .entry img{} should that be elsewhere in the style sheet. (read up on specificity at Smashing Mag here.)
You can control any aspect of the img selector through this method, set paddings, margins, centering, etc. Thanks for reading my quick tip, If you have any questions, comment here. And here’s some zen::






Note: I really don’t agree with this one because I’m from NY and any 9/11 images piss me off but, this is really effective.







