Register to get access to free programming courses with interactive exercises

Relative and absolute positioning CSS: Positioning

In the lesson about absolute positioning, we said that the element is positioned relative to the page corner. This behavior is not always convenient for several reasons:

  • Suppose we want to position element relative to the nearest positioned ancestor and not the page itself
  • Or when you change the page width, an element with absolute positioning will also change since its coordinates are counted from the edge

Let's imagine a situation where we want to create a block that can be closed by clicking on the cross in the upper-right corner. At this stage, we're only concerned with being able to create this cross without complicating the layout. This is where absolute positioning comes in handy. Using it and the top and right rules, we can say that the cross will always be in the top-right corner.

The only question left is how we can position it relative to the block. An element with absolute positioning will be positioned relative to the page edge only when none of the parent elements have any other positioning. I.e., if the parent block has relative or absolute positioning, a child element with absolute positioning will be positioned relative to it, not to the page edge.

In this case, the task of positioning the element to close the block becomes simple – we only need to specify the relative positioning of the whole block, and the close button will be automatically positioned relative to this block.


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.