Tutorial 1 - Floating an image to the right - all steps combined

Step 1 - Start with a paragraph of text and an image

For this exercise, we want to force an image over to the right to allow the content to flow alongside it. We also want to add margins to the left and bottom sides of the image to push the content away. Finally, we want to add a border around the image. We start with an image sitting inside a paragraph of text.

Step 2 - Apply float: right to the image

To force the image over to the right edge, "float: right" is applied to the image using a class selector. In this case, the name of the class is ".floatright" but any name can be used. 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 image.

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 a border

To add a border to the image, use "border: 1px solid #666;". Again, Netscape 4 does not like this rule, so it should be hidden from the browser using "@import".

border: 1px solid #666;

Step 5 - Add padding

If you want the border to sit slightly off the edge of the image, use padding.

padding: 2px;

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.


CSS CODE
.floatright
{
float: right;
margin: 0 0 10px 10px;
border: 1px solid #666;
padding: 2px;
}

HTML CODE
<p>
<img class="floatright" src="images/image.gif" alt="" width="100" height="100">Lorem ipsum dolor sit amet, consectetuer...
</p>
Other Max Design articles and presentations
Associated with webstandardsgroup.org