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
top
, right
, bottom
, and left
valuesThe 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.
The Hexlet support team or other students will answer you.
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.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.