Introduction to CSS Positioning Properties Part 1 - The "position" property
(Page 5 of 6 )
As its name implies, this property is used to set the type of position applicable to the selected element. The following values can be defined:
position: static; (default behavior for all elements)
position: relative; (uses relative positioning for this element using the top, right, bottom and left properties)
position: absolute; (uses absolute positioning for this element using the top, right, bottom and left properties)
position:fixed; (uses fixed positioning for this element using the top, right, bottom and left properties)
As previously mentioned, for most of positioning schemas, it’s necessary to specify "top," "left," "right" and "bottom" properties. Let’s see each one of them in detail.
The "top," "right," "bottom" and "left" properties
As we can see, to properly implement CCS2 positioning schemas, we need to define "top," "left," "right" and "bottom" properties. However, specifing both "top" and "bottom," or "left" and "right" properties simultaneously, will render an undesirable visual effect, and not achieve what we really want from them. Generally, we’ll end up utilizing the top and left properties to efficiently position an element in the desired location.
We can use the following values for these properties:
Fixed Length: It’s possible to specify a fixed value expressed in pixels (px), points(pt), inches(in), centimeters (cm), millimeters (mm), ems (em), or picas (pc). For instance, we can define several examples, such as this:
#content {
position: absolute;
width: 500px;
left: 300px;
top: 150px;
}
And this:
#content {
position: absolute;
width: 500px;
right: 200px;
bottom: 150px;
}
However, the last example is somewhat unusual, and would not normally be used. For most cases, specifying the left and top properties is the safest way to position an element in a Web document.
Also, percentage values may be set. It’s possible to assign a percentage value, generally well suited for liquid designs, where the element’s size gets expanded or collapsed according to different screen resolutions:
/P>
#content {
position: absolute;
width: 68%;
left: 30%;
top: 20%;
}
Or, using the right and bottom properties:
#content {
position: absolute;
width: 68%;
right: 20%;
bottom: 30%;
}
In our two last examples, we specified a percentage value for each element. In the first case, the content selector will be positioned at 30 percent of the parent element’s width and 20 percent of its height. The second case shows that the element will be positioned at 20 percent of the parent’s width and 30 percent of its height. Once again, this approach is rarely used. Keep in mind that "top" and "left" properties are perfectly suitable for all of the situations.
That’s our quick guide to CSS2 position schemes in Web documents. Certainly, concerning each CSS property, browser support may vary considerably, particularly when referring to old browsers.
It’s time to have a look to another CSS2 visual property: the "display" property.
Next: The "display" property >>
More Style Sheets Articles
More By Alejandro Gervasio