Register to get access to free programming courses with interactive exercises

Fixed positioning CSS: Positioning

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, and left 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.


Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

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.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months
Layout Designer icon
Profession
New Under development beginner
Layout with the latest CSS standards
start anytime 5 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions

Toto Image

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.