Cascading Style Sheets/Positioning
Position Schemes
editInstead of using table(s) to layout a page you can use CSS positioning. By using the CSS positioning you can make a page more dynamic. Positioning is not compatible with all browsers, so when coding it is necessary to know your audience.
Types of positioning:
- Normal Flow/Static Positioning
- Relative Positioning
- Absolute Positioning
- Fixed Positioning
The examples below are all using basically the same XHTML document. Only the CSS positioning changes. To make it easy to see what is happening with the CSS there are colored borders around the two blocks. The outer block has a blue border (#0000FF
, abbreviated #00F
). The inner block has a red border (#FF0000
, abbreviated #F00
). Only the rule for the inner block changes in these examples.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Standard XHTML</title>
<style type="text/css">
#main {
border: 1px solid #00F;
}
#content { /* This rule changes in the example below. */
border: 1px solid #F00;
}
</style>
</head>
<body>
<div id="main">
<p>Lorem ipsum pro ne apeirian cotidieque, eu per nihil everti ornatus.
At nam iudico dolore suavitate. Harum quaeque consetetur ei usu, ius et
impetus aliquid consequat, at pro nullam oporteat partiendo. Sed ea nonummy
suscipiantur. Nec libris erroribus scriptorem ut.</p>
<div id="content">
<p>His ne albucius liberavisse definitionem. His eu kasd eligendi
invidunt, et prima legimus adolescens mea. Nonumy aliquid pri et, qui
ex dicant nostrum moderatius. Eam in malorum efficiantur, falli eleifend
cotidieque qui ne. At modus vituperata dissentiet has. Mel postea aeterno
diceret eu, eu postea laoreet sea, nam te meliore platonem necessitatibus.</p>
</div>
<p>Vix in causac adipisci, dicit facete vulputate te mel. Et vis noster
admodum mediocritatem, quaeque mnesarchum sea id. Quas vocibus id qui. Ne
delenti iracundia conitituam sed, erudin invenire consulanu usu in. Vero
legimus duo ex, his no suscipit vituperata delicatissimi.</p>
</div>
</body>
</html>
Normal Flow
editNormal Flow is the default behavior of a web browser. You do not specify this in your style sheet since it is the default. With normal flow boxes will show up in the order that you placed them in your code, and each box level element is stacked on the next.
<style type="text/css">
#main {
border: 1px solid #00F;
}
#content {
border: 1px solid #F00;
}
</style>
Static Positioning
editStatic positioning is applied by the declaration position: static
. This places the element in the normal flow. Since normal flow is the default it is not normally necessary to explicitly use this.
Where it is useful is over-riding another rule of lower specifity, e.g.
div { position:absolute; }
#notMe { position:static; }
would absolutely position all div
elements except the one whose id
attribute has the value notMe.
The left
, top
, right
and bottom
properties are not needed since they don't influence static positioning. They are used below to show they have no influence.
<style type="text/css">
#main {
border: 1px solid #00F;
}
#content {
border: 1px solid #F00;
position: static;
left: 100px;
top: 125px;
right: 50px;
bottom: 30px;
}
</style>
|
Relative Positioning
editThe browser first lays out the element as though it was in the normal flow. The element is then displaced by the amount specified by the left
or right
properties and the top
or bottom
properties. A gap is left in the normal flow at the point the element should have appeared. Relative positioning does not allow an element to change size. If both left
and right
are specified right
will be ignored in languages which are written left to right such as English. bottom
is ignored if top
is specified.
<style type="text/css">
#main {
border: 1px solid #00F;
}
#content {
border: 1px solid #F00;
position: relative;
left: 100px;
top: 125px;
right: 50px;
bottom: 30px;
}
</style>
|
Absolute Positioning
editThis positions a box relative to its containing block. However, unlike relative positioning the gap in the normal flow left by removing the element closes up. The containing block is the nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed'.
You can use any one or combination of left, top, right, and bottom properties to position the box. The co-ordinates for absolute position have (0,0) at the top left of the containing block. Increasing the value of top moves the element down the page.
Since absolutely positioned boxes are taken out of the normal flow they can be positioned anywhere on the page regardless of their position in the document's source order.
<style type="text/css">
#main {
border: 1px solid #00F;
}
#content {
border: 1px solid #F00;
position: absolute;
left: 100px;
top: 125px;
right: 50px;
bottom: 30px;
}
</style>
|
Fixed Positioning
editFixed positioning is a subcategory of absolute positioning. The only difference is that for a fixed positioned box, the containing block is established by the browser window size. A fixed element does not move when a web page is scrolled as all other elements do. It is calculated in the same way as absolute positioning with respect to containing blocks in that it pulls the positioned box out of the normal flow.
<style type="text/css">
#main {
border: 1px solid #00F;
}
#content {
border: 1px solid #F00;
position: fixed;
left: 100px;
top: 125px;
right: 50px;
bottom: 30px;
}
</style>
|
Floating Elements
editElements can be made to float within the normal flow. Boxes are moved left or right as far as they can go. Elements after the float box will move up to fill any gap left behind thus flowing around the box with the float position.
Notice that float is not a position property, but it acts like one. Float is applied with the float
property not the position
property. The float is positioned to the left in the example, but you could easily make it positioned to the right. A width was added to the content block so you can see how elements after the float block move up and wrap around the area the content block does not occupy.
You must set the width
property when floating block-level elements otherwise they will expand to fill the entire width of their container.
<style type="text/css">
#main {
border: 1px solid #00F;
}
#content {
border: 1px solid #F00;
left: 100px;
top: 125px;
right: 50px;
bottom: 30px;
float: left;
width: 425px;
}
</style>
|