Positioning allows you to move an element from where it would be
placed when in normal flow to another location. Positioning isnβt a
method for creating your main page layouts, it is more about
managing and fine-tuning the position of specific items on the page.
There are however useful techniques for certain layout patterns that
rely on the position
property. Understanding
positioning also helps in understanding normal flow, and what it is
to move an item out of normal flow.
There are five types of positioning you should know about:
-
Static positioning is the default that every
element gets β it just means "put the element into its normal
position in the document layout flow β nothing special to see
here".
-
Relative positioning allows you to modify an
element's position on the page, moving it relative to its position
in normal flow β including making it overlap other elements on the
page.
-
Absolute positioning moves an element completely
out of the page's normal layout flow, like it is sitting on its
own separate layer. From there, you can fix it in a position
relative to the edges of the page's <html> element (or its
nearest positioned ancestor element). This is useful for creating
complex layout effects such as tabbed boxes where different
content panels sit on top of one another and are shown and hidden
as desired, or information panels that sit off screen by default,
but can be made to slide on screen using a control button.
-
Fixed positioning is very similar to absolute
positioning, except that it fixes an element relative to the
browser viewport, not another element. This is useful for creating
effects such as a persistent navigation menu that always stays in
the same place on the screen as the rest of the content scrolls.
-
Sticky positioningis a newer positioning method
which makes an element act like
position: static
until it hits a defined offset from
the viewport, at which point it acts like
position: fixed
.
Simple positioning example
To provide familiarity with these page layout techniques, we'll show
you a couple of quick examples. Our examples will all feature the
same HTML, which is as follows:
<h1>Positioning</h1>
<p>I am a basic block level element.</p>
<p class="positioned">I am a basic block level element.</p>
<p>I am a basic block level element.</p>
This HTML will be styled by default using the following CSS:
body {
width: 500px;
margin: 0 auto;
}
p {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79,185,227);
padding: 10px;
margin: 10px;
border-radius: 5px;
}
The rendered output is as follows:
Relative positioning
Relative positioning allows you to offset an item from the position
in normal flow it would have by default. This means you could
achieve a task such as moving an icon down a bit so it lines up with
a text label. To do this, we could add the following rule to add
relative positioning:
.positioned {
position: relative;
top: 30px;
left: 30px;
}
Here we give our middle paragraph a position
value of
relative
β this doesn't do anything on its own, so we
also add top
and left
properties. These
serve to move the affected element down and to the right β this
might seem like the opposite of what you were expecting, but you
need to think of it as the element being pushed on its left and top
sides, which result in it moving right and down.
Adding this code will give the following result:
.positioned {
position: relative;
background: rgba(255,84,104,.3);
border: 2px solid rgb(255,84,104);
top: 30px;
left: 30px;
}
Absolute positioning
Absolute positioning is used to completely remove an element from
normal flow, and place it using offsets from the edges of a
containing block.
Going back to our original non-positioned example, we could add the
following CSS rule to implement absolute positioning:
.positioned {
position: absolute;
top: 30px;
left: 30px;
}
Here we give our middle paragraph a position
value of
absolute
, and the same top
and
left
properties as before. Adding this code, however,
will give the following result:
.positioned {
position: absolute;
background: rgba(255,84,104,.3);
border: 2px solid rgb(255,84,104);
top: 30px;
left: 30px;
}
This is very different! The positioned element has now been
completely separated from the rest of the page layout and sits over
the top of it. The other two paragraphs now sit together as if their
positioned sibling doesn't exist. The top
and
left
properties have a different effect on absolutely
positioned elements than they do on relatively positioned elements.
In this case the offsets have been calculated from the top and left
of the page. It is possible to change the parent element that
becomes this container and we will take a look at that in the lesson
on positioning.
Fixed positioning
Fixed positioning removes our element from document flow in the same
way as absolute positioning. However, instead of the offsets being
applied from the container, they are applied from the viewport. As
the item remains fixed in relation to the viewport we can create
effects such as a menu which remains fixed as the page scrolls
beneath it.
For this example our HTML is three paragraphs of text, in order that
we can cause the page to scroll, and a box to which we will give
position: fixed
.
<h1>Fixed positioning</h1>
<div class="positioned">Fixed</div>
<p>Paragraph 1.</p>
<p>Paragraph 2.</p>
<p>Paragraph 3.</p>
.positioned {
position: fixed;
top: 30px;
left: 30px;
}
Sticky positioning
Sticky positioning is the final positioning method that we have at
our disposal. It mixes the default static positioning with fixed
positioning. When an item has position: sticky
it will
scroll in normal flow until it hits offsets from the viewport that
we have defined. At that point it becomes "stuck" as if it had
position: fixed
applied.
.positioned {
position: sticky;
top: 30px;
left: 30px;
}
Note: to find more out about positioning, see our
Positioning
article.