118.CSS Positioning
118.CSS Positioning
The CSS positioning properties allow you to position an element. It can also place
an element behind another, and specify what should happen when an element's content
is too big.
Elements can be positioned using the top, bottom, left, and right properties.
However, these properties will not work unless the position property is set first.
They also work differently depending on the positioning method.
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.
Example
p.pos_fixed {
position: fixed;
top: 30px;
right: 5px;
}
[demo]
<!DOCTYPE html>
<html>
<head>
<style>
p.pos_fixed {
position: fixed;
top: 30px;
right: 5px;
color: red;
}
</style>
</head>
<body>
<p><b>Note:</b> IE7 and IE8 supports the fixed value only 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>
<p class="pos_fixed">Some positioned text.</p>
</body>
</html>
[/demo]
Note Note: IE7 and IE8 support the fixed value only if a !DOCTYPE is specified.
Fixed positioned elements are removed from the normal flow. The document and other
elements behave like the fixed positioned element does not exist.
Relative Positioning
A relative positioned element is positioned relative to its normal position.
Example
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
[demo]
<!DOCTYPE html>
<html>
<head>
<style>
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
</style>
</head>
<body>
</body>
</html>
[/demo]
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.
Example
h2.pos_top {
position: relative;
top: -50px;
}
[demo]
<!DOCTYPE html>
<html>
<head>
<style>
h2.pos_top {
position: relative;
top: -30px;
}
</style>
</head>
<body>
</body>
</html>
[/demo]
Relatively positioned elements are often used as container blocks for absolutely
positioned elements.
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>:
Example
h2 {
position: absolute;
left: 100px;
top: 150px;
}
[demo]
<!DOCTYPE html>
<html>
<head>
<style>
h2 {
position: absolute;
left: 100px;
top: 150px;
}
</style>
</head>
<body>
</body>
</html>
[/demo]
Absolutely positioned elements are removed from the normal flow. The document and
other elements behave like the absolutely positioned element does not exist.
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).
Example
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
[demo]
<!DOCTYPE html>
<html>
<head>
<style>
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<img src="./imagecopy1234567890/test.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>
[/demo]
An element with greater stack order is always in front of an element with a lower
stack order.
Note Note: If two positioned elements overlap without a z-index specified, the
element positioned last in the HTML code will be shown on top.
Examples
More Examples
Set the shape of an element
[demo]
<!DOCTYPE html>
<html>
<head>
<style>
img {
position: absolute;
clip: rect(0px,60px,200px,0px);
}
</style>
</head>
<body>
</body>
</html>
[/demo]
This example demonstrates how to set the shape of an element. The element is
clipped into this shape, and displayed.
[demo]
<!DOCTYPE html>
<html>
<head>
<style>
div.scroll {
background-color: #00FFFF;
width: 100px;
height: 100px;
overflow: scroll;
}
div.hidden {
background-color: #00FF00;
width: 100px;
height: 100px;
overflow: hidden;
}
</style>
</head>
<body>
</body>
</html>
[/demo]
This example demonstrates how to set the overflow property to create a scroll bar
when an element's content is too big to fit in a specified area.
[demo]
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #00FFFF;
width: 150px;
height: 150px;
overflow: auto;
}
</style>
</head>
<body>
</body>
</html>
[/demo]
This example demonstrates how to set the browser to automatically handle overflow.
[demo]
<!DOCTYPE html>
<html>
<body>
<p>Mouse over the words to change the cursor.</p>
<span style="cursor:auto">auto</span><br>
<span style="cursor:crosshair">crosshair</span><br>
<span style="cursor:default">default</span><br>
<span style="cursor:e-resize">e-resize</span><br>
<span style="cursor:help">help</span><br>
<span style="cursor:move">move</span><br>
<span style="cursor:n-resize">n-resize</span><br>
<span style="cursor:ne-resize">ne-resize</span><br>
<span style="cursor:nw-resize">nw-resize</span><br>
<span style="cursor:pointer">pointer</span><br>
<span style="cursor:progress">progress</span><br>
<span style="cursor:s-resize">s-resize</span><br>
<span style="cursor:se-resize">se-resize</span><br>
<span style="cursor:sw-resize">sw-resize</span><br>
<span style="cursor:text">text</span><br>
<span style="cursor:w-resize">w-resize</span><br>
<span style="cursor:wait">wait</span><br>
</body>
</html>
[/demo]