To control which overlapping webpage element appears on top, use the CSS z-index property with positioned elements. The z-index assigns a stacking order where higher values appear above lower ones, but only works on elements with position set to relative, absolute, fixed, or sticky. Stacking contexts also affect layering—child elements are confined within their parent’s context, meaning a high z-index inside one context may still appear behind elements in another context. Practical tips include using small incremental values (like 10, 20, 30), avoiding unnecessary stacking, checking for conflicts with third-party libraries, and testing across browsers. If z-index doesn’t work, verify that elements are properly positioned and check for conflicting stacking contexts.
When elements on a webpage overlap, how do you control which one shows up on top? That’s where the z-index
property in CSS comes into play. It might seem straightforward at first, but it can get tricky if you're not careful with how you set up your HTML structure and positioning.

Let’s break down what you need to know about using z-index
.

What z-index
Actually Does
The z-index
property controls the stacking order of elements that are positioned — meaning they have a position value of relative
, absolute
, fixed
, or sticky
. Elements with a higher z-index
value will appear in front of those with a lower one.
For example:

.box { position: relative; z-index: 2; }
This .box
element will sit above any other positioned element with a z-index
less than 2. But here's the catch: z-index
only works on elements that are explicitly positioned. If you try to apply it to a static element (the default), it won’t do anything.
So remember:
- Use
position
beforez-index
- Values can be negative, zero, or positive integers
- Browsers render higher numbers on top
How Stacking Context Works
One of the most confusing parts of z-index
is understanding stacking contexts. A stacking context is like a layer group — once you create one (by giving an element a z-index
, for example), all its child elements are stacked within that context.
What this means in practice:
- A child element with a high
z-index
inside one stacking context may still appear behind another element outside of it, even if that element has a lowerz-index
- So if a parent has
z-index: 10
, and a sibling parent hasz-index: 5
, then no matter how high you set thez-index
of the first parent's child, it won't go beyond the second parent's children
You don’t always need to worry about stacking contexts, but when things aren’t stacking the way you expect, this is often why.
Practical Tips for Using z-index
Here are some real-world tips to keep your layers under control:
- Use small values: You don’t need to start at 99999. Start from 1 and increase as needed. It keeps things organized.
- Group related values: Like using 10, 20, 30 instead of 1, 2, 3 — gives you room to insert something in between later.
-
Avoid unnecessary stacking: Only use
z-index
when you really need to change the stacking order. -
Be aware of third-party libraries: UI kits or frameworks might already use their own
z-index
values, so check for conflicts.
Also, make sure to test across browsers. While modern browsers handle z-index
pretty consistently, older ones (especially IE) can behave differently.
When z-index
Doesn’t Work
If your z-index
isn’t working, check these common issues:
- Is the element actually positioned?
- Is there a parent with a lower
z-index
creating a new stacking context? - Are both elements part of different stacking contexts?
A good trick is to temporarily set a background color or outline on overlapping elements so you can visually see what’s happening.
That should cover the basics. z-index
seems simple until it doesn’t work the way you expect — but once you understand stacking contexts and positioning, it becomes much more predictable.
The above is the detailed content of CSS tutorial explaining the z-index property. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

CSScounterscanautomaticallynumbersectionsandlists.1)Usecounter-resettoinitialize,counter-incrementtoincrease,andcounter()orcounters()todisplayvalues.2)CombinewithJavaScriptfordynamiccontenttoensureaccurateupdates.

In CSS, selector and attribute names are case-sensitive, while values, named colors, URLs, and custom attributes are case-sensitive. 1. The selector and attribute names are case-insensitive, such as background-color and background-Color are the same. 2. The hexadecimal color in the value is case-sensitive, but the named color is case-sensitive, such as red and Red is invalid. 3. URLs are case sensitive and may cause file loading problems. 4. Custom properties (variables) are case sensitive, and you need to pay attention to the consistency of case when using them.

CSSselectorsandpropertynamesarecase-insensitive,whilevaluescanbecase-sensitivedependingoncontext.1)Selectorslike'div'and'DIV'areequivalent.2)Propertiessuchas'background-color'and'BACKGROUND-COLOR'aretreatedthesame.3)Valueslikecolornamesarecase-insens
