Aligning Images With Your Stylesheet.
UPDATE:
If you are using the new version of WordPress 2.6 and above, check this post: Align Image In WordPress 2.7
A lot of WordPress Themes don’t include the functions to align images, something which I find very strange and have no explanation for. I mean, they have written the codes for the whole theme which would have taken them days to do, leaving out the extra few lines that would have made life easier for those using the theme is really beyond me. This theme that you see now is exactly that. There are no functions for me to call when I want to align the image.
If you align the image manually and you don’t have the knowledge to give the image the proper paddings, the image and the text is going to stick together making the whole layout of the post looks really ugly and amateurish. Before I went digging on how to overcome this, I use to edit the image to give it a white border before uploading the image into my post. At least this way, there are some space between the image and the text. Later I learned how it can be done by a few css codes inserted into my theme’s stylesheet.
As always, before attempting to mess with your theme’s stylesheet, back it up first. Simply open your Stylesheet php in your theme editor, select all and copy them into notepad. Make sure that copy is safe until you are sure the modifications you have done is okay before throwing it away. If somehow, you screw up, just copy those original codes and paste them back totally and you will restore everything to it’s previous state. That was how I did things when I modify each theme that comes my way.
Now, to add in the codes to make alignment of your images more neat and elegant, go to Dashboard – Presentation – Theme Editor – Stylesheet. It is always the default php when you open your Theme Editor anyway.
Under style css. paste in these codes below:
img.alignright {
float: right;
clear: right;
margin: 5px 10px 5px 10px;
background: #fff;
padding: 2px;
border: 2px solid #ffffff;
}
img.alignleft {
float: left;
clear: left;
margin: 5px 5px 5px 5px;
background: #fff;
padding: 2px;
border: 2px solid #ffffff;
}
img.centered {
display: block;
padding: 2px;
margin: 0 auto 20px auto;
border: 1px solid #ffffff;
float: none;
clear: both;
}
These can go anywhere but just make sure you don’t dump it some where in between lines. Css codes normally ends with this } sign, so insert those codes after that sign and nowhere in between any this { or this } sign. Also it would be better to insert it after any whole functions. For example, from the codes above, you see there are 3 functions that you can call to align your image. Like wise, there will also be many functions for other parts of the themes as well, like the headers, post contents and so on. Insert it after the end of all the codes for that function.
You can edit the codes if you want to. Looking at those codes, the img.alignright means what will happen if you want the image to be aligned to your right. The margins, backgrounds, paddings and borders have all been set. If you want your border in other color, you can change the color codes. If you have itchy hands and inquisitive mind like me, just experiment around and see what happens. It won’t kill you. You have a backup remember?
After inserting the codes, click “Update File,” assuming you have changed your file attributes to “writable”. Else you won’t see the “Update File” option at the bottom of your page.

Now you can align your images easily by giving it a “class”. For example, if you want your image to be floated to the left with all the text nicely around it, you assign the class=”alignleft” function for the image. If you it on the right, assign the class=”alignright”. Likewise for centered. When you assign these codes, make sure you spell it correctly and similar to those codes you inserted to your stylesheet just now. See the cat at the right, it is nicely placed.
The codes looks like this in its raw form. The letters in black ore the function that you have to assign to the image.

Other Awesome Articles
- Inserting and Aligning Images In Your WordPress Blog
- How To Use The Custom Stylesheet In Your WordPress Theme
- Should You Add Frames To Your Images
- Insert Thumbnail Images In Your Homepage Post Excerpts
- Align Image In Post WordPress 2.7
- Display Thumbnail Images In Post Excerpts
- Video Tutorial – Working With WordPress CSS Stylesheet
- How To Frame And Stack Your Blog Image
Users who found this page were searching for:
- how to align left in stylesheet
- stylesheet code image borders
- Align image blogger
- style sheet for image alignment
- stylesheet img align
7 Responses to “Aligning Images With Your Stylesheet.”
Comments
Read below or add a comment...
Trackbacks
-
[...] You may want to subscribe to my RSS feed. Thanks for visiting!More than a year ago, I posted the stylesheet codes for aligning images in your WordPress posts. Times have changed and WordPress has added many new advanced features. One [...]

If you enjoyed this post, whether it's awesome or lame, consider leaving a comment or 









Holy crap.. I always thot my WP theme was Fxrked up becos I can’t align my images like Blogger. Thank God you have spoken. It’s these little things in life that annoy the crap out of me. lol
OK OK this is a real life saver. All my pictures in my WP blog are huge. YOu know why? Becos if I don’t make them huge, I’ll have the broken text on my left. Grr Grrr
Thanks.
Thanks, good artical. You can add these alignment features to just about any theme you want customized. If you are a Dreamweaver user, you can ‘see’ this happen in realtime while tweeking your theme Design View using a Dw extension called ThemeDreamer. There is an artical that addresses just in their How To secion.
Thanks for the article and copy-pastable code. I’d figured out that my inserted images were calling the non-existent “alignright” from my css, but was struggling to insert my own code…yours worked like a charm!
Hello All –
I thought I’d see if anyone can work out the kink in my image-align issue:
I have tried about EVERY solution I could find on all the forum sites concerning this, but nothing’s worked yet. Here is a prime example of my problem:
The David Bowie quote is meant to be centered, but obviously, is not. Here is what my style.css currently looks like:
/*img.alignright { float:right; margin: 0 0 1px 5px; padding: 2px;}
img.alignleft { float:left; margin: 0 5px 5px 0; padding: 2px;}
img.aligncenter { float:center; margin: 0 5px 5px 0; padding: 2px;}*/
/*This works to RIGHT align images, but not centered ones!!!*/
img.alignleft, div.alignleft {
float:left;
margin:2px 2px 2px 0px;
}
img.alignright, div.alignright {
float:right;
margin:2px 0px 2px 2px;
}
img.centered, .aligncenter {
display: block;
margin-left: auto;
margin-right: auto;
}
/*img.centered, .aligncenter, div.aligncenter {
display: block;
margin-left: auto;
margin-right: auto;
}
img.alignright {
padding: 4px;
margin: 0 0 2px 7px;
display: inline;
}
img.alignleft {
padding: 4px;
margin: 0 7px 2px 0;
display: inline;
}
.alignright {
float: right;
}
.alignleft {
float: left;
}*/
If you can help me I would be infinitely grateful. I’ve been trying to fix this for about 3-4 hours, and if I can’t fix it by tomorrow night, my head might explode.
Thanks!
Hey, Thanks a lot. Was having the toughest time trying to figure this out.
I’ve been wrestling with this problem all day with a new theme I installed and was on the verge of kicking my laptop. I’m not a techie, so thank you for explaining the fix so clearly!