Positioning and stacking contexts Flashcards

1
Q

What is the containing block of an element and why does it matter?

A

The containing block of an element is the area that containts the element. Most often, the containing block is the content area of an element’s nearest block-level ancestor.

The containing block of an element matters because the size and position of an element are often impacted by its containing block.

Percentage values that are applied to the width, height, padding, margin, and offset properties (top, bottom, right, left) of an absolutely positioned element are computed from the element’s containing block.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you identify the containing block of an element?

A

The process for identifying the containing block depends entirely on the value of the element’s position property:

  1. If the position property is static, relative, or sticky, the containing block is formed by the edge of the content box of the nearest ancestor element that establishes a formatting context (such as a table container, flex container, grid container, or the block container itself).
  2. If the position property is absolute, the containing block is formed by the edge of the padding box of the nearest ancestor element that has a position value other than static (i.e. fixed, absolute, relative, or sticky).
  3. If the position property is fixed, the containing block is established by the viewport (in the case of continuous media) or the page area (in the case of paged media).
  4. If the position property is absolute or fixed, the containing block may also be formed by the edge of the padding box of the nearest ancestor element that has the following:
    ~~~
    I. A transform or perspective or fiter value other than none
    II. A will-change value of transform or perspective or filter
    III. A contain value of layout, paint, strict or content (e.g. contain: paint;)
    IV. A container-type value other than normal
    V. A backdrop-filter other than none (e.g. backdrop-filter: blur(10px);)
    ~~~

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an stacking context?

A

The stacking context is a three-dimensional conceptualization of HTML elements along an imaginary z-axis relative to the user, who is assumed to be facing the viewport or the webpage. HTML elements occupy this space in priority order based on element attributes.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When is an stacking context formed?

A

A stacking context is formed, anywhere in the document, by any element in the following scenarios:

  • Root element of the document (<html>).
  • Element with a position value of absolute, fixed or sticky
  • Element with position: relative and z-index value other than auto.
  • A child of a flex container or a grid container, with z-index value other than auto.
  • Element with an opacity value less than 1.
  • Element with a mix-blend-mode value other than normal.
  • Element with any of the following properties with value other than none:
    transform, filter, backdrop-filter, perspective, clip-path, mask / mask-image / mask-border.
  • Element with an isolation: isolate.
  • Element with a will-change value specifying any property that would create a stacking context on non-initial value.
  • Element with a contain value of layout, or paint, or a composite value that includes either of them (i.e. contain: strict, contain: content).

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does position CSS property do?

A

The position CSS property sets how an element is positioned in a document. The top, right, bottom, and left properties determine the final location of positioned elements.

Examples:

position: static;
position: relative;
position: absolute;
position: fixed;
position: sticky;

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does position: static do to an element?

A
  • The element is positioned according to the normal flow of the document. The top, right, bottom, left, and z-index properties have no effect.
  • static is the default value.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does position: relative do to an element?

A
  • The element is positioned according to the normal flow of the document, and then offset relative to itself based on the values of top, right, bottom, left. The offset does not affect the position of any other elements; thus, the space given for the element in the page layout is the same as if position were static.
  • This value creates a new stacking context when the value of z-index is not auto.
  • Has no effect on table-*-group, table-row, table-column, table-cell, and table-caption elements.

NOTE: Unlike fixed and absolute positioning, you cannot use top, right, bottom and left to change the size of a relatively positioned element. Those values will only shift the position of the element up or down, left or right. You can use top or bottom, but not both together (bottom will be ignored); likewise, you can use left or right, but not both (right will be ignored).

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does position: absolute do to an element?

A
  • The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to its closest positioned ancestor, if any; otherwise, it is placed relative to the initial containing block. Its final position is determined by the values of top, right, bottom, left.
  • This value creates a new stacking context when the value of z-index is not auto.
  • The margins of absolutely positioned boxes do not collapse with other margins.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does position: fixed do to an element?

A
  • The element is removed from the normal document flow, and no space is created for the element in the page layout.
  • It is positioned relative to the initial containing block established by the viewport, except when one of its ancestors has a transform, perspective, or filter property set to something other than none, or the will-change property is set to transform, perspective or filter, in which case that ancestor behaves as the containing block.
  • Its final position is determined by the values of top, right, bottom and left.
  • This value always creates a new stacking context. In printed documents, the element is placed in the same position on every page.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does position: sticky do to an element?

A
  • The element is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor), including table-related elements, based on the values of top, right, bottom and left. The offset does not affect the position of any other elements.
  • This value always creates a new stacking context.
  • Note that a sticky element “sticks” to its nearest ancestor that has a “scrolling mechanism” (created when overflow is hidden, scroll, auto, or overlay), even if that ancestor isn’t the nearest actually scrolling ancestor.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a positioned element?

A

A positioned element is an element whose computed ñposition value is either relative, absolute, fixed, or sticky' (In other words, it’s anything except static.)

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the problem with fixed or stiky content?

A

Scrolling elements containing fixed or sticky content can cause performance and accessibility issues.

As a user scrolls, the browser must repaint the sticky or fixed content in a new location. Depending on the content needing to be repainted, the browser performance, and the device’s processing speed, the browser may not be able to manage repaints at 60 fps, causing accessibility concerns for people with sensitivities and jank for everyone.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you solve performance issues of fixed or sticky content?

A

Adding

will-change: transform

to the positioned elements to render the element in its own layer, improving repaint speed and therefore improving performance and accessibility.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you crete a modal backdrop that fills the entire viewport?

A
.modal-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

Using position: fixed and setting each of the four sides set to 0 makes the backdrop fill the entire viewport.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the containing block of a fixed positioned element?

A

The viewport

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the containing block of an absolute postioned element?

A

Its closest positioned ancestor element.

NOTE: If none of the element’s ancestors are positioned, then the absolutely positioned element will be positioned based on something called the initial containing block. This is an area with dimensions equal to the viewport size, anchored at the top of the page.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

17
Q

What is the containing block of an absolute postioned element?

A

its closest positioned ancestor element. (An element with position value different thatn static)

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

18
Q

What is the containing block element of a positioned element?

A

It is the block element the positioned element uses to position itself using top, right, bottom and left properties.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

19
Q

What is the render tree?

A

As the browser parses HTML into the DOM, it also creates another tree structure called the render tree.

The render tree represents the visual appearance and position of each element. It’s also responsible for determining the order in which the browser will paint the elements. This order is important because elements painted later appear in front of elements painted earlier, should they happen to overlap.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

20
Q

How does the browser paints all the elements in a page?

A

The browser first paints all non-positioned elements, then it paints the positioned ones. By default, any positioned element appears in front of any non-positioned elements.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

21
Q

What does z-index CSS property do?

A

The z-index CSS property sets the z-order of a positioned element and its descendants. Overlapping elements with a larger z-index cover those with a smaller one.

In other words, elements with a higher z-index appear in front of elements with a lower z-index.

Elements with a negative z-index appear behind static elements.

Source MDN

22
Q

What values does z-index CSS property accepts?

A
  • auto - Default value - The box does not establish a new local stacking context. The stack level of the generated box in the current stacking context is 0.
  • <integer> - This <integer> is the stack level of the generated box in the current stacking context. The box also establishes a local stacking context. This means that the z-indexes of descendants are not compared to the z-indexes of elements outside this element.

Source MDN

23
Q

What gotchas does z-index CSS property have?

A
  • z-index only works on positioned elements. You cannot manipulate the stacking order of static elements.
  • Applying a z-index to a positioned element establishes a new stacking context.
  • When you add a z-index to a positioned element that element becomes the root of a new stacking context. All of its descendant elements are then part of that stacking context.
  • No element outside the stacking context can be stacked between any two elements that are inside it.
  • If an element on the page is stacked behind a stacking context, no element within that stacking context can be stacked behind that element.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

24
Q

Stacking contexts vs block formatting contexts

A
  • Stacking contexts deal with which elements are in front of other elements;
  • block formatting contexts deal with the document flow and whether or not elements will overlap.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

25
Q

In what order are stacked the elements within a stacking context?

A

In the following order:

  1. The root element of the stacking context
  2. Positioned elements with a negative z-index (along with their children)
  3. Non-positioned elements
  4. Positioned elements with a z-index of auto (and their children)
  5. Positioned elements with a positive z-index (and their children)

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

26
Q

How can you keep track of z-index values?

A

It’s easy for a stylesheet to devolve into a z-index war, with no clear order as to the priority of various components. Without clear guidance, developers’ll give it a ridiculously high z-index, like 999999. After this happens a few times, it’s anybody’s guess what the z-index for another new component should be.

To prevent this you can use custom properties. That way you can see at a glance what is supposed to appear in front of what:

--z-loading-indicator: 100;
--z-nav-menu: 200;
--z-dropdown-menu: 300;
--z-modal-backdrop: 400;
--z-modal-body: 410;

Use increments of 10 or 100, so you can insert new values in between should the need arise.

Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.

27
Q

What does sticky postioning (position: sticky;) do?

A

It’s sort of a hybrid between relative and fixed positioning: The element scrolls normally with the page until it reaches a specified point on the screen, at which point it will “lock” in place as the user continues to scroll.

The element is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor), including table-related elements, based on the values of top, right, bottom, and left. The offset does not affect the position of any other elements.

This value always creates a new stacking context. Note that a sticky element “sticks” to its nearest ancestor that has a “scrolling mechanism” (created when overflow is hidden, scroll, auto, or overlay), even if that ancestor isn’t the nearest actually scrolling ancestor.

Be aware that, It doesn’t work in IE

“position: sticky” (MDN Web Docs). Retrieved December 18, 2023.