The topic of positioning doesn't just end with relative and absolute. They're usually sufficient for most situations that may arise; they're very powerful and flexible tools.
But there are situations where these two types of positioning aren't the best option. Imagine we need to make a website header that moves as the user scrolls down the page. Neither relative nor absolute positioning will work here for one simple reason. The block won't move as you scroll. It will stay where it was originally positioned in the document.
We use fixed positioning to solve this problem. It's quite similar to absolute positioning
- an element is also pulled from the normal document flow, and other elements take its place
- we can control the positioning with the
top
,right
,bottom
, andleft
values
The distinctive feature of fixed positioning is that the element will be positioned relative to the browser viewport (visible area). I.e., if you give it the coordinates 0 on the top border and 0 on the left border, the element will always be in that position, whether the user is scrolling through the page now or not.
<style>
.fixed {
position: fixed;
top: 0;
left: 0;
}
</style>
<div class="fixed">Block with fixed positioning. Its coordinates relative to the browser viewport will always be the same.</div>
Try scrolling through the text, and you'll see that the left area stays in place. This often creates a menu that the user can always see.
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.