The position property specifies what kind of position is used. Possible values include:
- static (default): indicates that the element will be placed at the default location. Please note that if static is specified, values for the top, bottom, right, and left properties will have no effect.
- absolute: places an element in relation to the actual browser window. The position of the element moves along with the rest of the content when the page is scrolled.
- relative: specifies how the element will be positions relative to how it would have been positioned by default.
- fixed: places an element in relation to the actual browser window. The position of the element remains fixed even when the page is scrolled.
From the list of possible values, we can see that the position property by itself does not have a lot of value. After all, what does {position:relative;} mean if there is no information on what it is relative to? So, clearly additional properties are needed to complement the position property. They are as follows:
Absolute
p { position:absolute; top:50px; left:600px; } |
The HTML code,
<div> <p><img src="red-ball.jpg"> </div> |
renders the image you see that is 50px from the top of the browser and 600px from the left of the browser. Please notice that as you scroll up and down and page, the red ball moves along with the rest of the page. This is different from when we specify {position:fixed;}, when the red ball stays in the same place as the rest of the page moves.
Relative
CSS Code
The HTML code,
renders Example to show relative position. |
Example to show relative position. |
In the above example, applying the {position:relative; top:15px;} style added 15px of space on top of the text. In other words, this style shifted the text down by 15px relative to the default position.
Fixed
p { position:fixed; top:50px; left:650px; } |
The HTML code,
<div> <p><img src="red-ball.jpg"> </div> |
renders the image you see that is 50px from the top of the browser and 650px from the left of the browser. Please notice that as you scroll up and down and page, the red ball stays in the same place.
Static
Since static is the default value of position, we seldom see it declared explicitly. The only instances are when we are trying to undo a declaration that was done to the same element.
If static is the value of position, the directional properties (top, bottom, left, and right) are not used even if specified.
Top
Let's take a look at two examples:
Example 1: CSS Code
div { background-color:#FF00FF; width:500px; height:60px; } p { position:relative; top:10px; } |
The HTML code,
<div> <p>Sample text. </div> |
renders
Sample text.
Example 2: CSS Code
div { background-color:#FF00FF; width:500px; height:60px; } p { position:relative; top:-10px; } |
The HTML code,
<div> <p>Sample text. </div> |
renders
Sample text.
Bottom
Let's take a look the following example:
CSS Code
The HTML code,
renders No bottom property. |
Apply bottom:5px. |
In the above example, applying the {position:relative; bottom:5px;} style moved the text up by 5px.
Left
Let's take a look at two examples:
Example 1: CSS Code
div { background-color:#FF00FF; width:500px; height:60px; } p { position:relative; left:30px; } |
The HTML code,
<div> <p>This text is 30 pixels from the left of the box. </div> |
renders
This text is 30 pixels from the left of the box.
Example 2: CSS Code
div { background-color:#FF00FF; width:500px; height:60px; } p { position:relative; left:-10px; } |
The HTML code,
<div> <p>This text is 10% from the left of the box. </div> |
renders
This text is 10% from the left of the box.
Right
Let's illustrate with two examples:
Example 1
CSS Code
The HTML code,
renders No right property. |
Apply right:30px. |
In the above example, we see applying the {position:relative; right:30px;} style shifts the text 30 pixels away from the original location.
Example 2
CSS Code
The HTML code,
renders No right property. |
Apply right:30%. |
In the above example, we see applying {position:relative; right:30%;} shifts the text 30% (75 pixels) away from the original location.
Overflow
- visible: The entirety of the content is shown, regardless of the height specified for the element.
- hidden: Only the content within the containing element is shown.
- scroll: Displays the vertical and horizontal scroll bar to allow scroll of the content, regardless of whether the content exceeds the containing element.
- auto: If the content is beyond the containing element, a scroll bar is displayed.
We are using this area to show how different values of the overflow property will behave.This particular one is on the auto value. | |
We are using this area to show how different values of the overflow property will behave.This particular one is on the visible value. | We are using this area to show how different values of the overflow property will behave.This particular one is on the scroll value. |
Z-Index
For example, say we define two blocks with the following CSS code:
#redblock { z-index: 1; position: absolute; width:80px; height:100px; top:20px; left:20px; border: 1px solid #FFF; background-color: #FF0000; } #pinkblock { z-index: 2; position: absolute; width:100px; height:80px; top:50px; left:50px; border: 1px solid #FFF; background-color: #FF00FF; } |
The following HTML code,
<div id="redblock"></div> <div id="pinkblock"></div> |
renders the following:
As we can see, the pink block, with a z-index value of 2, lies on top of the red block, which has a z-index value of 1.
0 comments:
Post a Comment