Floating Images and adding captions in wordpress posts

Posted on August 26th, 2011

The images in the posts cane be aligned  “left”, “right” or “centered”. WordPress adds class=”alignleft”, class=”alignright” or class=”aligncentered” to the ‘img’ tags or its surrouding ‘div’ element, when you choose left, right or center alignment for images.

The post looks aligned to the left, right or centered within the WordPress admin area, but when you see the post on your site, it may not look properly aligned. It is because the theme that you are using, may not be aware of those classes added by WordPress. So add the following css to style.css

img.alignleft, div.alignleft
margin:0 0.5em 0.5em 0;
img.alignright, div.alignright
margin:0 0 0.5em 0.5em;
img.aligncenter, div.aligncenter
margin:0 auto;

Same in the case of captions for images. The style corresponding to caption is as follows:

.wp-caption {
border: 1px solid #ddd;
text-align: center;
background-color: #f3f3f3;
padding-top: 4px;
margin: 10px;
/* optional rounded corners for browsers that support it */
-moz-border-radius: 3px;
-khtml-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
.wp-caption img {
margin: 0;
padding: 0;
border: 0 none;
.wp-caption p.wp-caption-text {
font-size: 11px;
line-height: 17px;
padding: 0 4px 5px;
margin: 0;