2008 Oct 17
Property | Values | Initial Value | Applies to | Inherited | Percentage |
---|---|---|---|---|---|
float | left | right | none | inherit | none | all elements | no | |
clear | left | right | both | none | none | block-level elements | no | |
position | static | relative | absolute | fixed | inherit | static | all elements | no | |
bottom top |
<length> | <percentage> | auto | inherit | auto | positioned elements when value of position is other than static | no | of height |
left right |
<length> | <percentage> | auto | inherit | auto | positioned elements when value of position is other than static | no | of width |
z-index | <integer> | auto | inherit | auto | positioned elements | no |
CSS+HTML | Results |
---|---|
<p style="float: right; border: double; width: 96; font-size: 80%; margin: 3px; padding: 2px; background-color: #CCEEFF;"> This paragraph is floated to the right in a sky of blue.</p> <p>This paragraph is a stodgy immobile non floated boring one, which has its roots too firmly fixed in the page. If this paragraph was before the floated one its normal break behavior would keep it completely before the floated paragraph, which is why these two paragraphs are in the order shown.</p> |
This paragraph is floated to the right in a sky of blue. This paragraph is a stodgy immobile non floated boring one, which has its roots too firmly fixed in its behavior. If this paragraph was before the floated one its normal break behavior would keep it completely before the floated paragraph, which is why these two paragraphs are in the order shown. |
img { border: 1px solid green; } ... <p>This paragraph is the ocean within which the floated <img src=leftg.gif style="float: left;"> images swim. <img src=fishbowl.gif style="float: left;"> It is a big ocean so more than one fish <img src=rightg.gif style="float: right;"> is floating in it, since more than one image is floating in it. Are fishing boats needed in this ocean? The images have zero margin hence the text nearly touches the images.</p> |
This paragraph is the ocean within which the floated images swim. It is a big ocean so more than one fish is floating in it, since more than one image is floating in it. Are fishing boats needed in this ocean? The images have zero margin hence the text nearly touches the images. |
The containing block of a floated element is the nearest block-level ancestor element. Placement rules for floated elements follows, the above examples illustrate these rules. The primary purpose of these rules is to prevent overlap of floated blocks and to keep a floated element from floating up beyond its ancestor's edge.
When a floating element is taller than its parent element it will float with respect to subsequent elements as in example below.
CSS+HTML | Results |
---|---|
img { border: 1px solid green; } ... <p>This <img src=v64.jpg style="float: right;"> paragraph is short, a mere lake, but does have a fish or two in it.</p> <p>This is the second paragraph which also flows around the floater. The time to vote for your favorite fish is now.</p> |
This paragraph is short, a mere lake, but does have a fish or two in it. This is the second paragraph which also flows around the floater. The time to vote for your favorite fish is now. |
When a floating element is wider than its parent element it will overflow the parent in the opposite direction to its float direction. Notice the collapse of the margin on the right. IE does not do so well with this.
CSS+HTML | Results |
---|---|
img { border: 1px solid green; } ... <p style="width: 128px; border: 5px groove; margin: 32px;"> This paragraph <img src=w160.jpg style="float: left; margin: 3px;"> is narrow, a mere stream, and has a wider image in it.</p> |
This paragraph is narrow, a mere stream, and has a wider image in it. |
Negative margins can displace floated elements to move outside of their parent element. This does not violate the rules. See example below.
CSS+HTML | Results |
---|---|
img { border: 1px solid green; } ... <p style="border: 1px dashed; margin: 18px;">This <img src=v64.jpg style="float: left; margin: -9px;"> paragraph has a positive margin and the image has a negative margin.</p> |
This paragraph has a positive margin and the image has a negative margin. |
In CSS2.1 the situation for inline elements (which up to then was unspecified) has been clarified by the following rules:
CSS+HTML | Results |
---|---|
img.lefty { float: left; margin: 12px 12px 12px -12px; } ... <p style="margin: 16">This paragraph has an embedded <img src=v64.jpg class=lefty> <b style="border: double; margin: -16px; background: #00CC99"> long bold phrase is showing itself with color and border and negative margins with some overlap.</p> <h3 style="margin-top: -16px; background: #66CCFF;"> Heading3.</h3> |
This paragraph has an embedded long bold phrase is showing itself with color and border and negative margins with some overlap. Heading3. |
When you do not want your content to flow past a floated element, especially from one section to the next, then set the first element in each section to prevent foating elements from appearing next to it. This is done with the clear property. This property define the sides of an element on which no floating elements may appear. Modifying the above example for the heading3 the results are as follows.
CSS+HTML | Results |
---|---|
img.lefty { float: left; margin: 12px 12px 12px -12px; } ... <p style="margin: 16">This paragraph has an embedded <img src=v64.jpg class=lefty> <b style="border: double; margin: -16px; background: #00CC99"> long bold phrase is showing itself with color and border and negative margins some overlap.</p> <h3 style="clear: left; margin-top: -16px; background: #66CCFF;"> Heading3.</h3> |
This paragraph has an embedded long bold phrase is showing itself with color and border and negative margins some overlap. Heading3. |
Shape and size are preserved. Meaning of position values:
A float's containing block is the nearest block-level ancestor element. The situation for positioning is more complex.
Negative values will position oustide of the containing block. These offset properties are: bottom left right top.
CSS+HTML | Results |
---|---|
<div style="background: #CCFFCC; height: 100px; width: 150px; border: double; margin: 6px;"> Containing Element <div style="position: relative; top: 20%; left: 40%; width: 60%; padding: 4px; border: solid; background: #CC9933;"> Positioned<br>Element </div> Stuff </div> |
Containing Element
Positioned
Stuff
Element |
Normally elements are drawn in the order of the document flow. This property alters this renedering order and creates layers. There is conceptually a third axis which runs from front (highest integer values) to back (lowest integer values). Descendent elements have their stacking order relative to their ancestor (this is tree recursion as for containing blocks).
CSS+HTML | Results |
---|---|
<span style="background: #CCCCFF; position: relative; top: 0%; left: 2em; border: double; float: left; z-index: 2;"> z-index: 2; </span> <span style="background: #CCFFCC; position: relative; top: 1em; left: -1.8em; border: double; float: left; z-index: 3;"> z-index: 3; </span> <span style="background: #CC9933; position: relative; top: -.4em; left: .1em; border: 1px dashed; float: left; z-index: 1;"> z-index:<br>1; </span> |
z-index: 2;
z-index: 3;
z-index: 1; |