I believe it is a common sense that the CSS layout properties are not as easy to understand and apply as for example color
and border
. This is due to the fact that a rule-language like CSS is not the adequate means to specify layout. But anyway it is used for it, and we have to live with a property like position. Even when the display
property is of same importance, I will focus only on position
in this Blog. Here are its possible values:
position: static;
position: relative;
position: absolute;
position: fixed;
position: static;
is the default setting when no position was given. It means that block elements are placed below each other, and inline elements beside each other. In other words, everything is like we know it from web browsers.
It makes no sense to define top, left, right
and bottom
coordinates on a static element, they will simply be ignored.
position: relative;
has two meanings:
top, left, right
and bottom
properties available for the element (which would be ignored by static
positioning)In practice this means that you can relatively adjust such an element by using top, left, right
and bottom
, but more frequently such positioning means that the element is used as parent for absolutely positioned child elements.
The "Oops" effect when you find out that position: absolute;
means positioning relatively to the parent element. But not to any parent element, only to a non-static
parent element!
In other words, you pin the absolute
element to a non-static
parent and then position it using top, left, right
and bottom
. It will always stay there, even when other children might scroll away.
This relative/absolute pairing also works recursively. Useful e.g. for a fixed table header. Mind that the top, left, right
and bottom
coordinates are relative to the HTML page, not to the browser view-port.
There are two more things to say about absolutely positioned elements:
static
elements (in z-direction), in a way that it will overlap them, because additionally static
elements behave as it would not be there any moreWhen you want to have a button that always stays in some corner of the browser client window, then you must use a position: fixed;
assignment. This pins the element to the browser view-port, and it won't scroll when the browser content is scrolled. Of course you can also use top, left, right
and bottom
here.
Very suitable also for dialog windows that should appear relative to the browser window, and not relative to the (possibly much bigger) page content.
We need to play around with this to understand it. I created a test page that provides several levels of nested div
elements. You can use it for testing by setting position, top, left, right, bottom
properties to these elements, and see where it goes then.
Click to see the HTML structure.
1 | <!DOCTYPE HTML> |
This colorful test page needs some explanations.
div
elements (see source above), each has another color. You can change CSS properties of any div
by using the according properties table below. div
to absolute
, content from below will be raised and might appear under it (in z-order), or under the properties panel, because an absolute positioned element plays no more role in position calculations for static elements. The property settings panel might be obscured then, this is what the "Pin Properties Panel" checkbox is for: it positions the panel absolutely to its current x-coordinate, and sets its z-index to 2 (thus it should always be available to repair settings).Shift-Reload
). fixed
positioning, the element will disappear. You will see it again when you set top
to e.g. 1
. Watch then that you can scroll the page content below it, the fixed element won't move. There might be some more comments needed here, but maybe you'll discover everything on your own now. Mind that in most browsers the top
and left
properties overrule right
and bottom
. You can not size the element by using e.g. both left
and right
.
Faites vos sable jeux, s'il vous plaƮt :-)
Level 0 | |
---|---|
position: | |
height: | |
width: | |
top: | |
left: | |
right: | |
bottom: |
Level 1 | |
---|---|
position: | |
height: | |
width: | |
top: | |
left: | |
right: | |
bottom: |
Level 2 | |
---|---|
position: | |
height: | |
width: | |
top: | |
left: | |
right: | |
bottom: |
Level 3 / 1 | |
---|---|
position: | |
height: | |
width: | |
top: | |
left: | |
right: | |
bottom: |
Level 4 / 1 | |
---|---|
position: | |
height: | |
width: | |
top: | |
left: | |
right: | |
bottom: |
Level 4 / 2 | |
---|---|
position: | |
height: | |
width: | |
top: | |
left: | |
right: | |
bottom: |
Level 3 / 2 | |
---|---|
position: | |
height: | |
width: | |
top: | |
left: | |
right: | |
bottom: |
ɔ⃝ Fritz Ritzberger, 2015-07-19