Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Layering and Z-Index in Grids | Advanced Grid Features
CSS Grid Mastery

bookLayering and Z-Index in Grids

Layering and Stacking in CSS Grid

When working with CSS Grid, you will sometimes want grid items to overlap each other. By default, grid items do not overlap, each occupies its own grid cell. However, when you intentionally place multiple items into the same grid cell or use negative margins and positioning, overlapping can occur. In these cases, understanding how the browser determines which item appears on top is essential.

CSS establishes an implicit stacking context for grid containers. This means that, unless you explicitly set a stacking order, the browser will stack overlapping items based on their order in the HTML: items that appear later in the markup will appear above earlier items. However, you can control the stacking order directly using the z-index property. Applying z-index to a grid item allows you to bring it forward or push it behind other items within the same stacking context.

The stacking context in a grid behaves as follows:

  • Each grid item is a positioned element if you set position to relative, absolute, or fixed;
  • Only positioned items can use z-index to affect stacking order;
  • Higher z-index values stack above lower ones within the same stacking context;
  • If z-index values are equal or not set, the order in the HTML determines which item is on top.
index.html

index.html

styles.css

styles.css

copy

Practical Uses for Layering in Grid Layouts

Layering grid items using overlapping and z-index is useful in various design scenarios:

  • Creating banners or callouts that float above main content;
  • Designing image galleries with hover overlays or captions;
  • Building interactive dashboards where notifications or popups appear over widgets;
  • Adding decorative elements such as shadows, badges, or highlights that sit above grid content.

Understanding and controlling stacking order ensures that interactive or visually important elements are always visible and accessible to users, regardless of their position in the markup.

question mark

Which statement best describes how stacking order is determined for overlapping grid items?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give an example of overlapping grid items using CSS?

How does stacking context work with nested grids?

What happens if I use negative margins with grid items?

Awesome!

Completion rate improved to 9.09

bookLayering and Z-Index in Grids

Swipe to show menu

Layering and Stacking in CSS Grid

When working with CSS Grid, you will sometimes want grid items to overlap each other. By default, grid items do not overlap, each occupies its own grid cell. However, when you intentionally place multiple items into the same grid cell or use negative margins and positioning, overlapping can occur. In these cases, understanding how the browser determines which item appears on top is essential.

CSS establishes an implicit stacking context for grid containers. This means that, unless you explicitly set a stacking order, the browser will stack overlapping items based on their order in the HTML: items that appear later in the markup will appear above earlier items. However, you can control the stacking order directly using the z-index property. Applying z-index to a grid item allows you to bring it forward or push it behind other items within the same stacking context.

The stacking context in a grid behaves as follows:

  • Each grid item is a positioned element if you set position to relative, absolute, or fixed;
  • Only positioned items can use z-index to affect stacking order;
  • Higher z-index values stack above lower ones within the same stacking context;
  • If z-index values are equal or not set, the order in the HTML determines which item is on top.
index.html

index.html

styles.css

styles.css

copy

Practical Uses for Layering in Grid Layouts

Layering grid items using overlapping and z-index is useful in various design scenarios:

  • Creating banners or callouts that float above main content;
  • Designing image galleries with hover overlays or captions;
  • Building interactive dashboards where notifications or popups appear over widgets;
  • Adding decorative elements such as shadows, badges, or highlights that sit above grid content.

Understanding and controlling stacking order ensures that interactive or visually important elements are always visible and accessible to users, regardless of their position in the markup.

question mark

Which statement best describes how stacking order is determined for overlapping grid items?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
some-alt