Tutorial 2 - Floating an image and caption - 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 have a caption under the image.

Step 2 - Add a div around image and caption

The first modification is to the HTML code. Add a div around the image and caption. In this case, we are also adding a class selector (class="floatright") to the div. Any name could be used here.

<div class="floatright">

Step 3 - Apply "float: right" and width to the div

Apply "float: right" to the div. This will force the div over to the right edge of its containing box. A width is also required when floating an element - unless it is an image.

float: right;
width: 100px;

Step 4 - Apply margin to the div

Margins can be added to push the content away from the left and bottom sides of the div containing the image and caption.

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 5 - Add a background color

You can add a background color to make the image and caption stand out from the overall page. As you can see, it will not show on the page, except under the caption. This is fixed by adding some padding to the div.

background-color: #ddd;

Step 6 - Add padding

You can add some padding to the div to extend the background color.

padding: 10px;

Step 7 - Add a border to the div

To add a border to the div, 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 8 - Add a border to the image

To add a border to the image, you can use a Decendant Selector rule. The image and caption are inside the div so they are decendants of the div. This means you can target the image with a rule "div img". The problem with this rule is that it will target any image inside any div.

To be more specific, we can narrow down the selection by using "div.floatright img { border: 1px solid #ddd; }". This will target any image inside a div that is styled with a "floatright" class.

As the image has a border of 1px on either side, the width of the div should be changed from 100px to 102px (100px image, 1px left margin, 1px right margin = 102px).

width: 102px;
div.floatright img { border: 1px solid #000; }

Step 9 - Remove margin-top on the paragraph

You may have noticed that the top of the paragraph of text and the div containing the 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? Will other paragraphs 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; }

Step 10 - Playing with borders

You can play with border colors on the div and image to create different effects.

width: 103px;
border-top: 1px solid #999;
border-right: 2px solid #555;
border-bottom: 2px solid #555;
border-left: 1px solid #999;

}

div.floatright img
{
border-top: 2px solid #555;
border-right: 1px solid #999;
border-bottom: 1px solid #999;
border-left: 2px solid #555;

}

Finished!

« Back to main menu


Caption here

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;
width: 103px;
margin: 0 0 10px 10px;
background-color: #fff;
padding: 10px;
border-top: 1px solid #999;
border-right: 2px solid #555;
border-bottom: 2px solid #555;
border-left: 1px solid #999;
}

div.floatright img
{
border-top: 2px solid #555;
border-right: 1px solid #999;
border-bottom: 1px solid #999;
border-left: 2px solid #555;
}

p { margin-top: 0; }

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