Ad space (banner)
🎨HTML & CSS Lessons
Lesson 23 / 50

z-index (Specifying Stacking Order)

In this lesson you'll learn how to specify stacking order using CSS's z-index, so you can resolve trouble with overlapping elements. This is for people searching "CSS z-index usage."

When elements overlap, the larger the number in z-index, the more "on top" it's displayed. Picture it like the "topmost card" when you stack a deck of playing cards. It's used together with position.

The sample code specifies z-index: 1; on .box-a and z-index: 2; on .box-b, confirming how .box-b, with the larger number, is displayed layered on top. Also notice that if neither has position: absolute specified, the effect of z-index won't correctly appear.

A common beginner stumbling block is that z-index has no effect while position remains at its default value, static. z-index only applies to an element that has relative, absolute, fixed, or sticky specified.

Trouble where a popup window or a menu ends up hidden behind other elements is a common stumbling point in web development. Understanding how z-index works lets you calmly resolve this kind of "stacking order" problem.

HTML & CSS
OUTPUT

💡 The preview runs entirely on this page — nothing is sent externally.

Ad space (banner)
Ad space (in-article)