What is the difference between position: absolute and position: fixed in CSS?

Prepare for the uCertify CIW Advanced HTML5 and CSS3 Specialist Exam. Dive into essential topics with flashcards and multiple-choice questions. Enhance your understanding with hints and explanations for each question. Pass your exam confidently!

Multiple Choice

What is the difference between position: absolute and position: fixed in CSS?

Explanation:
The key idea here is what reference frame each type of positioning uses and how that affects scrolling. An element with position: absolute is taken out of the normal document flow and is positioned relative to the nearest ancestor that has a position value other than static. If there is such an ancestor, the element’s position is calculated from that ancestor’s padding box. If there isn’t, it’s positioned relative to the page’s root container. In contrast, an element with position: fixed is also taken out of the normal flow, but its containing block is the viewport—the visible part of the page—so it stays in the same place on the screen even as you scroll. That distinction explains why the correct description says absolute is positioned relative to the nearest positioned ancestor, while fixed is relative to the viewport and remains in place during scrolling. The other statements misstate the reference frame or the behavior after scrolling, so they don’t fit how these two positioning modes actually work.

The key idea here is what reference frame each type of positioning uses and how that affects scrolling. An element with position: absolute is taken out of the normal document flow and is positioned relative to the nearest ancestor that has a position value other than static. If there is such an ancestor, the element’s position is calculated from that ancestor’s padding box. If there isn’t, it’s positioned relative to the page’s root container. In contrast, an element with position: fixed is also taken out of the normal flow, but its containing block is the viewport—the visible part of the page—so it stays in the same place on the screen even as you scroll.

That distinction explains why the correct description says absolute is positioned relative to the nearest positioned ancestor, while fixed is relative to the viewport and remains in place during scrolling. The other statements misstate the reference frame or the behavior after scrolling, so they don’t fit how these two positioning modes actually work.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy