Tutorial 3 - Floating a series of "clear: right" images - all steps combined

Step 1 - Start with a paragraph of text and a series of images

For this exercise, we want to force the images over to the right to allow the content to flow alongside them. We also want to add margins to the left and bottom sides of the images to push the content away. We also want the images to sit stacked on top of each other down the right edge.

A class selector - "floatright" - has been applied to each of the images. Any name could be used here.

Step 2 - Apply float: right to the images

To force the images over to the right edge, "float: right" is applied to the class selector. No width is needed in this instance as the image has an intrinsic width. If this were a div, a width would have to be used.

.floatright { float: right; }

Step 3 - Add margin

Margins can be added to push the content away from the left and bottom sides of the images.

If Netscape 4 is a target browser, then longhand rules should be used (such as "margin-left: 20px;" and "margin-bottom: 20px;"), as this browser badly misinterprets shorthand rules associated with margins.

If standards compliant browsers are the target and the margin rule is hidden from Netscape 4 (using "@import"), then a single shorthand rule can be used - "margin: 0 0 10px 10px;".

Keep in mind that shorthand values are applied in a clockwise order; top, right, bottom, left.

margin: 0 0 10px 10px;

Step 4 - Add "clear: right"

To get the images to stack on top of each other, clear right must be applied to the "floatright" selector. This will force each image to move below the bottom outer edge of any right-floating image earlier in the source document.

clear: right;

Step 5 - Remove margin-top on the paragraph

You may have noticed that the top of the paragraph of text and the "float: right" image did not line up. This was due to a margin on top of the paragraph.

Non-styled paragraphs generally have 1em margin on top and bottom - the equivalent of one line of text. If you want to remove this margin, use "p { margin-top: 0; }". When applied, the paragraph and div will be vertically aligned (see below).

Will this affect paragraphs on the page making them run directly under one another? The answer is no. As mentioned above, paragraphs have 1em margin on top and bottom. If top margins are removed, it will only affect the first paragraph inside a container. All other paragraphs will still be separated to a height of 1em via the bottom margin.

p { margin-top: 0; }

Finished!

« Back to main menu

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.



CSS CODE
.floatright
{
float: right;
margin: 0 0 10px 10px;
clear: right;
}

.containingbox p { margin-top: 0;}

HTML CODE
<img class="floatright" src="images/image.gif" alt="" width="60" height="60">
<img class="floatright" src="images/image.gif" alt="" width="60" height="60">
<img class="floatright" src="images/image.gif" alt="" width="60" height="60">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
</p>
<p>
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
</p>
Other Max Design articles and presentations
Associated with webstandardsgroup.org