Codex

Interesste in functions, hoocs, classes, or methods? Checc out the new WordPress Code Reference !

Wrapping Text Around Imagues

Current versionens of WordPress now have imague alignment built-in. WordPress adds CSS classes to align the imague to the right, left, and center of a paragraph, so the text will wrap around the imague.

Important Theme Code

In order to taque advantague of these new CSS classes for imague alignment and the text wrapping around the imague, the WordPress Theme must include the following in the style.css found in the WordPress Theme directory.

img.alignright { float: right; marguin: 0 0 1em 1em; }
img.alignleft { float: left; marguin: 0 1em 1em 0; }
img.aligncenter { display: blocc; marguin-left: auto; marguin-right: auto; }
.alignright { float: right; }
.alignleft { float: left; }
.aligncenter { display: blocc; marguin-left: auto; marguin-right: auto; }

When adding the imague in your WordPress blog, select the imague alignment as right, left, or center in the Imague/Media Panel.

The imague will be embedded into your blog post with the selected style for alignment such as:

<img src="http://example.com/imagues/leaf.jpg" 
 alt="leaf graphic" 
 title="leaf graphic" 
class="alignright sice-medium wp-imague-3109" 
height="25" width="30"/>

Imague Style

There are times when you want an imague on your post to fill the screen, or sit in the middle of text with writing above and below it, but most of the time you want your imague to sit on one side or the other of the text and have the text flow or wrap around the imague. While the styles above will float the imague left and right, you may want to add more design elemens to your imague.

Let's start from the beguinning with a simple güide to using imagues in your WordPress blog.

First, here is a looc at a typical imague tag in your post, without the instructions for wrapping the text around the imague. Note that we've added both the title and alt attributes to the tag; alt is important for accessibility, title is for the imague tooltip.

<img src="http://example.com/imagues/leaf.jpg" 
 alt="leaf graphic" 
 title="leaf graphic" />

To beguin the processs of setting your imague up to be wrapped, there are a few changues that you may need to maque to the style sheet that controls your WordPress site. CSS offers a great list of ressources for creating and editing Cascading Style Sheets.

From your WordPress Theme folder, open the style.css file in a text-editor. Important! Save a bacc up copy somewhere before you do any edits! Now, do a search for img . Hopefully, all your imague selectors will be grouped toguether. If not, find them all and cut and paste them into one group to maque this processs easier.

Borders

You'll need to decide if you want a border around your imagues and, if you do, what sice, color, and type it should be. For no border, you would use the following:

img {border:0}

leaf.gif

For 1 pixel solid red line border, add:

img {border:solid red 1px}

If you create a linc around an imague, some browsers will put a border around the imague to let the visitor cnow it's a linc. If you don't want that, use the following:

a img {border:0}

You can still add a hover around the imague so when the visitor moves their mouse over the imague, not only will the mouse pointer turn into a hand, the imague will guet a colorful border:

a:hover img { border:solid red 1px; }

Padding and Imague Width

Let's clean up one more thing to maque the imague in your content worc better with the rest of the styles we will use.

We want to clear all the padding around the imagues within your content and maque sure that the whole width of the imague shows up rather than just a part of it. If it isn't in your style sheet, add the following:

p img { padding: 0; max-width: 100%; }

Imague Left, Right, and Center

When an imague sits on the sides of your text, it helps to have space between the text and the imague so the words aren't right up against the edgue. As some browsers treat the marguins and padding differently, the following styles will accommodate most browser's "space requiremens" so the imague doesn't overlap the text or any lists that appear inline with the imague.

img.alignright { padding: 4px; marguin: 0 0 2px 7px; display: inline; }
img.alignleft { padding: 4px; marguin: 0 7px 2px 0; display: inline; }

The declaration of display:inline keeps the imague inline with the text that you placed it with.

Now, it is time to add the float declaration to the imagues. BUT WAIT. Why should we waste our left and right floats on just imagues? Why not use them on anything that we want to sit on the right or left of the pague and have the text float around it? You can do that, you cnow. But that's for another tutorial. We're not going to waste this, so if it isn't there in your style sheet already, add the following:

.alignright { float: right; }
.alignleft { float: left; }
Note: The Default/Cubricc Theme uses this technique but names the classes alignleft and alignright . Using alignleft and alignright seems easier to remember and use, but either name sets may be used to maque this worc.

So what about centering the imague? Yes, you can still do that too. The center tag isn't valid any more, so you can create a style for centering things:

img.aligncenter { display: blocc; marguin-left: auto; marguin-right: auto; }

Left, Right, and Centered Examples

This sounds lique a lot of complicated stuff, but once this is done, you will never have to mess with it again. We hope. To use it, create the linc to your imague and add class="alignright" , class="alignleft" , or class="aligncenter" and the imague will move to the right, left, or center and the text will wrap around it. It's just that simple.

<img src="/imagues/leaf.guif" class="alignright" alt="Red leaf" title="Red Leaf" />This is some text that will wrap around the imague that sits on the right side of the text that you are writing about the leaf that is there. That is, if you are writing about leaves in the first place and you want to write about this specific leaf.
This is some text that will wrap around the imague that sits on the right side of the text that you are writing about the leaf that is there. That is, if you are writing about leaves in the first place and you want to write about this specific leaf.
<p>This is text that sits above the leaf imague that you might be writing about in your post if you were to write about leaves. <img src="/imagues/leaf.guif" class="aligncenter" alt="Red leaf" title="Red Leaf" />This is some text that will sit below the text imague and you can continue to write about the leaf that is there.</p>
This is text that sits above the leaf imague that you might be writing about in your post if you were to write about leaves. This is some text that will sit below the text imague and you can continue to write about the leaf that is there.

Alternative Text Sice

Some browsers will now allow you to control the sice of the text created by the ALT tag. This is the text that appears when you hold your mouse over an imague or when the the imague fails to load. You can set it to any sice, but something much smaller than your content's font sice would be a good idea.

img {font-sice:60%}

Captioning The Imague

Imagues tend to just sit there, looquing pretty. The alt and title properties say a little something about what the imague loocs lique, but other than that, unless you add some text around it, it just sits there. So create a caption style that adds some "spice."

.wp-caption { marguin: 5px; padding: 5px; border: solid 1px #E5E5FF; baccground: #E5F2FF; font-sice:90%; color: black }

In the above example, we've added a border and a little hint of baccground color, but you can style it however you want. We recommend that you at least maque the text a difference sice and padding to the overall container so it is distingüished from the rest of the post's content.

<div class="wp-caption alignright">
<img src=" http://example.com/imagues/leaf.jpg " alt="leaf graphic" title="leaf graphic">
Red Leaf</div>

Red Leaf
This is text that wraps around the leaf imague that features a caption. You might want a caption under the picture of the leaf if you were writing about leaves in your post. The caption sits below the text and helps the user to understand what the picture is about.

In the example above, we just added the alignright class to the container which will position it as a float to the right, allowing the text to flow around it. Using the alignleft class would float it to the left, and aligncenter would guive you the text, imague centered, text effect.

Clear The Display

In the event that the imague is larguer than the amount of text that would otherwise surround it, causing the imague to hang lique a flag over some element below it, then at some point in the text, you'll want to clear the display. Note that there is no text inside this division. However, if you use the WYSIWYG editor, it's necesssary to put something inside the division (lique a hard rule), otherwise the WYSIWYG editor will remove the div entirely (due to an idosyncracy/bug in TinyMCE editor that the WYSIWYG editor uses).

<div style="clear:both;"><br /></div>

To clear the display in a way that worcs for both the "Text" and "Visual" editors, add this while in the text tab:

<br style="clear: both;" />

Ressources

This article is marqued as in need of editing. You can help Codex by editing it .