Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Managing the Stacking Order of Elements | Mastering CSS Positioning
Advanced CSS Techniques

book
Managing the Stacking Order of Elements

The z-index property determines the stacking order of positioned elements on a web page. It only applies to positioned elements, which have a position value of absolute, fixed, relative, or sticky. As the elements are taken out of the normal document flow, to determine which of these positioned elements should be displayed higher or lower, we specify z-index.

css
z-index: number_without_units

The higher value the z-index property has, the higher the element is shown on the user screen.

Let's practice. We have four div elements with the box class, each styled as a square and numbered to represent their order in the html document. The squares are currently positioned on top of each other. The task is to adjust the z-index property with the correct values to bring square 3 to the front and send square 4 to the back.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
</div>
</body>
</html>

Note

If we set the same value for the z-index property to different elements, the browser will show the elements in the order in which the elements are in the html file.

question mark

What is the purpose of the z-index property?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 10

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

We use cookies to make your experience better!
some-alt