How it works?
The z-index property defines the order of the elements on the z-axis. Basically, it works on elements with a position value other than static.
What is stack context?
It's a parent that contains one or multiple elements. Those elements will be stacked according to the source order rules, and their z-index values only have meaning within their parent.
# Agenda
Conclusion
Any sufficiently advanced technology is indistinguishable from magic.
Quiz
There are not many questions, there are few answers.
# Agenda
The z-index
property defines the order of the elements on the z-axis. Basically, it works on elements with a position value other than static
.
Example 1
The z-index
CSS property sets the z-order of a positioned element and its descendants or flex and grid items. Overlapping elements with a larger z-index cover those with a smaller one.
Example 1
Elements with a position value other than static
.
Elements with the static
position value.
Example 2
Example 3
z-index: -9999
to it. Why? Because the red block is a part of the wrapper stacking context.Example 4
Example 5
Example 1
Example 1 and 6
# POSITION
The default position for an element is position: static
, which doesn’t have any effect on the element.
Example 2
# POSITION
An element with position: relative
. The top
and bottom
properties specify the vertical offset from its normal position. The left
and right
properties specify the horizontal offset from its normal position.
Example 2
# POSITION
An element with position: absolute
. The top
and bottom
properties specify the vertical offset from its containing block. The left
and right
properties specify the horizontal offset from its containing block. For absolute positioning, the containing block is the closest positioned parent (An element with a position other than static)
Example 2
# POSITION
An element with position: fixed
. The top
and bottom
properties specify the vertical offset from its containing block. The left
and right
properties specify the horizontal offset from its containing block. The containing block for fixed positioned elements is the viewport, except when an element’s parent has one of the following properties applied to it: transform
, filter
or perspective
.
Example 2
# POSITION
Those properties represents the direction of where the element should be positioned, in case it has position: relative
. They also represent the boundaries of the containing block, if the element has position: absolute
.
Example 2
# POSITION
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.
Example 7
# STICKY
Sticky Item — is the element that we defined with the position: sticky
styles. The element will float when the viewport position matches the position definition, for example: top: 0px
.
Sticky Container — is the HTML element which wraps the sticky item. This is the maximum area that the sticky item can float in.
Example 7
position: sticky
you’re automatically defining the parent element as a sticky container!# STICKY
# STICKY
Relative (or Static) — the stickily positioned element is similar to the relative and static positions because it keeps the natural gap in the DOM (stays in the flow).
Fixed — when the item sticks, it behaves exactly like position: fixed
, floating in the same position of the view-port, removed from the flow.
Absolute — at the end of the sticking area, the element stops and stacks on top of the other element, much like an absolutely positioned element behaves inside position: relative
container.