spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / html / tutorials / 21 / 4

index12345

Tutorial 21: CSS Floats, Part II

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Floats in practice

Seeing this problem with floats and columns, the only use of floats today is to create floats with the default appearance: a block of text up against the side of the viewport with text flowing around. Although not as neat as multiple columns, this effect is useful and not something you can do with HTML alone. So, we can change the style sheet we used above to the following:

/* Looks */
BODY {
 background-color: #FFFFFF;
 color: #000000;
}
A:link { color: #8B7CFF; }
A:visited { color: #483ACE; }
A:active { color: #CC0000; }
H1 {
 font: bold italic large Arial, Helvetica, sans-serif;
}
H2 {
 font: italic large Arial, Helvetica, sans-serif;
}
#intro {
 border: medium solid #ACA250;
}
#header P {
 border: medium solid #ACA250;
 text-align: center;
}
#footer P {
 font-size: smaller;
 text-align: right;
}

/* Layout */
#intro {
 float: right;
 width: 35%;
}
#footer {
 clear: right;
}

This puts our little intro on the upper right hand corner of the page. The good thing about this simple technique is that it works with all CSS-supporting browsers, with one little niggle. Take a look at a screenshot of this page on Internet Explorer 4.0:

A right float in Internet Explorer 4.0.
A right float in Internet Explorer 4.0. [Full-sized image]

As you can see, IE4 right-aligns the text in the float. This is a bug in IE4, but is easy to work around: Just add an explicit text-align property for the float in your style sheet, which will now look like this:

/* Looks */
BODY {
 background-color: #FFFFFF;
 color: #000000;
}
A:link { color: #8B7CFF; }
A:visited { color: #483ACE; }
A:active { color: #CC0000; }
H1 {
 font: bold italic large Arial, Helvetica, sans-serif;
}
H2 {
 font: italic large Arial, Helvetica, sans-serif;
}
#intro {
 border: medium solid #ACA250;
 text-align: left;
}
#header P {
 border: medium solid #ACA250;
 text-align: center;
}
#footer P {
 font-size: smaller;
 text-align: right;
}

/* Layout */
#intro {
 float: right;
 width: 35%;
}
#footer {
 clear: right;
}

This should give you a nice float that works with all browsers, has no problems with viewport resizing, and falls back gracefully to a default layout on non-CSS browsers.

index12345

http://www.internet.com/

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Legal Notices.

URL: http://www.webreference.com/html/tutorial21/4.html

Produced by Stephanos Piperoglou
Created: April 05, 2000
Revised: April 5, 2000