Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Relative Position

Besides display, the layout of a page is affected by the property position. To see clearly what it does, we'll try it on a simple page with three squares for a while. We'll come back to the cat page later.

The default value of position is static. A static element stays exactly where normal flow puts it. All the elements you've seen so far were static.

An element with position: relative stays in normal flow too, but unlike a static one, it can be moved from its place with the properties top, right, bottom and left, called offsets. Each offset says how far the element moves away from that side of its original place: top: 30px moves it 30 pixels down, left: 30px moves it 30 pixels to the right.

The other elements don't notice the move. They behave as if the element were still in its original place, so its space stays empty.

Square 2 has position: relative, top: 30px and left: 30px. It has moved down and to the right from its original place, which is marked with a dashed line and stays empty. Squares 1 and 3 have not moved.

In the editor, the squares are div elements with display: inline-block, so they stand in a row. The second one has the id square2.

Instructions

  1. 1

    In the rule for #square2, add the declarations position: relative and top: 30px. The red square moves 30 pixels down.

  2. 2

    In the same rule, add left: 30px. The red square also moves 30 pixels to the right and partly covers the third square. The third square stays where it was, because for the other elements, the red square is still in its original place.

Back

5/10

  1. 1. Normal Flow and Block Elements
  2. 2. Inline Elements
  3. 3. Inline-Block Elements
  4. 4. Hiding Elements
  5. 5. Relative Position
  6. 6. Absolute Position
  7. 7. Absolute Inside Relative
  8. 8. Fixed Position
  9. 9. Sticky Position
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.