position: ***;
Browser |
|
---|
The position property sets how a box will be positioned.
.example {
position: absolute;
top: 50px;
left: 50px;
}
Property | Value | Explanation |
---|---|---|
position | static | the normal position (default) |
relative | the relative position from the normal position | |
absolute | the absolute position from the parent element | |
fixed | the absolute and fixed position from the window |
static (The normal position)
The static value ignores the top, bottom, left, or right properties.
div {
width: 200px;
height: 100px;
}
<div style="background-color: #85b9e9;">The first box</div>
<div style="background-color: #ffd78c; position: static;">The second box</div>
<div style="background-color: #bde9ba;">The third box</div>
The second box is displayed at the normal position.
relative (The relative position from the normal position)
The box's position can be moved using the top, bottom, left, or right properties.
div {
width: 200px;
height: 100px;
}
<div style="background-color: #85b9e9;">The first box</div>
<div style="background-color: #ffd78c; position: relative; top: 0; left:100px;">The second box</div>
<div style="background-color: #bde9ba;">The third box</div>
The second box is moved from the left of the normal position to the position of 100px.
absolute (The absolute position from the parent element)
The box's position can be moved using the top, bottom, left, or right properties.
Specify the relative or absolute for the parent element. If it is not specified, this becomes the absolute position from the window.
div {
width: 200px;
height: 100px;
}
<div style="background-color: #85b9e9;">The first box</div>
<div style="background-color: #ffd78c;">The second box</div>
<div style="background-color: #bde9ba; position: absolute; top: 50px; left:100px;">The third box</div>
The third box is moved from the top of the window to the position of 50px and the left to the position of 100px.
fixed (The absolute and fixed position from the window)
The box's position can be moved using the top, bottom, left, or right properties. (The box is fixed to the specified position)
div {
width: 200px;
height: 100px;
}
<div style="background-color: #85b9e9;">The first box</div>
<div style="background-color: #ffd78c;">The second box</div>
<div style="background-color: #bde9ba; position: fixed; top: 50px; left:100px;">The third box</div>
The third box is moved and fixed from the top of the window to the position of 50px and the left to the position of 100px. (The position doesn't change even if it scrolls)