CSS Positioning
There are four different positioning methods.Static Positioning
HTML elements are positioned static by default. A static positioned element is always positioned according to the normal flow of the page.Static positioned elements are not affected by the top, bottom, left, and right properties.
Fixed Positioning
An element with fixed position is positioned relative to the browser window.It will not move even if the window is scrolled:
<!DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style
type="text/css">
p.pos_fixed
{
position:fixed;
top:30px;
right:5px;
}
</style>
</head>
<body>
<p
class="pos_fixed">Some more text</p>
<p><b>Note:</b>
Internet Explorer 7 (and higher) supports the fixed value if a
!DOCTYPE
is specified.</p>
<p>Some
text</p><p>Some text</p><p>Some
text</p><p>Some text</p><p>Some
text</p><p>Some text</p><p>Some
text</p><p>Some text</p><p>Some text</p><p>Some
text</p><p>Some text</p><p>Some
text</p><p>Some text</p><p>Some
text</p><p>Some text</p><p>Some text</p>
</body>
</html>
Fixed positioned elements can overlap other elements.
Relative Positioning
A relative positioned element is positioned relative to its normal position.
<html>
<head>
<style
type="text/css">
h2.pos_left
{
position:relative;
left:-20px;
}
h2.pos_right
{
position:relative;
left:20px;
}
</style>
</head>
<body>
<h2>This
is a heading with no position</h2>
<h2
class="pos_left">This heading is moved left according to its
normal position</h2>
<h2
class="pos_right">This heading is moved right according to its
normal position</h2>
<p>Relative
positioning moves an element RELATIVE to its original position.</p>
<p>The
style "left:-20px" subtracts 20 pixels from the element's original
left position.</p>
<p>The
style "left:20px" adds 20 pixels to the element's original left
position.</p>
</body>
</html>
The content of relatively positioned elements can be moved
and overlap other elements, but the reserved space for the element is still
preserved in the normal flow.
h2.pos_top
{
position:relative;
top:-50px;
}
Relatively positioned elements are often used as container blocks for
absolutely positioned elements.{
position:relative;
top:-50px;
}
Absolute Positioning
An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is <html>:
<html>
<head>
<style
type="text/css">
h2
{
position:absolute;
left:100px;
top:150px;
}
</style>
</head>
<body>
<h2>This
is a heading with an absolute position</h2>
<p>With
absolute positioning, an element can be placed anywhere on a page. The heading
below is placed 100px from the left of the page and 150px from the top of the
page.</p>
</body>
</html>
Absolutely positioned elements are removed from the normal flow. The
document and other elements behave like the absolutely positioned element does
not exist.Absolutely positioned elements can overlap other elements.
Overlapping Elements
When elements are positioned outside the normal flow, they can overlap other elements.The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).
An element can have a positive or negative stack order:
<html>
<head>
<style
type="text/css">
img
{
position:absolute;
left:0px;
top:0px;
z-index:-1;
}
</style>
</head>
<body>
<h1>This
is a heading</h1>
<img
src="w3css.gif" width="100" height="140" />
<p>Because
the image has a z-index of -1, it will be placed behind the text.</p>
</body>
</html>
An element with greater stack order is always in front of an
element with a lower stack order.
Note: If two positioned elements overlap,
without a z-index specified, the element positioned last in the HTML code will
be shown on top.
All CSS Positioning Properties
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
Property
|
Description
|
Values
|
bottom
|
Sets the bottom margin edge for a positioned box
|
auto
length % inherit |
clip
|
Clips an absolutely positioned element
|
shape
auto inherit |
cursor
|
Specifies the type of cursor to be displayed
|
url
auto crosshair default pointer move e-resize ne-resize nw-resize n-resize se-resize sw-resize s-resize w-resize text wait help |
left
|
Sets the left margin edge for a positioned box
|
auto
length % inherit |
overflow
|
Specifies what happens if content overflows an element's box
|
auto
hidden scroll visible inherit |
position
|
Specifies the type of positioning for an element
|
absolute
fixed relative static inherit |
right
|
Sets the right margin edge for a positioned box
|
auto
length % inherit |
top
|
Sets the top margin edge for a positioned box
|
auto
length % inherit |
z-index
|
Sets the stack order of an element
|
number
auto inherit |
<div style="position:fixed;left:80px;top:20px;background-color:yellow;">
This
div has fixed positioning.
</div>
No comments:
Post a Comment
Please write your view and suggestion....