Which of the following best describes the function of the z-index property in…
2023
Which of the following best describes the function of the z-index property in CSS?
- A.
It determines the horizontal position of an element.
- B.
It specifies the vertical position of an element.
- C.
It controls the stacking order of elements along the z-axis.
- D.
It adjusts the opacity of an element.
- E.
It sets the font size of an element.
Attempted by 5 students.
Show answer & explanation
Correct answer: C
Concept
In CSS, the box model positions elements in two flat directions (horizontal and vertical), but overlapping elements also need a front-to-back order. The z-index property defines exactly this: it sets the stacking order of an element along the z-axis (the axis pointing out of the screen toward the viewer). A higher z-index value renders an element in front of one with a lower value when they overlap.
Application
z-index applies to elements that participate in stacking — classically a positioned element (position set to relative, absolute, fixed, or sticky), and in modern CSS also flex and grid items — and it compares siblings within the same stacking context. Reading the choices for what z-index actually does:
Stacking order of overlapping elements along the z-axis (front-to-back) — this is precisely the role of z-index.
Horizontal placement is governed by properties such as left, right, margin, or a layout system like flexbox/grid — not z-index.
Vertical placement is governed by top, bottom, margin, or the normal document flow — not z-index.
Transparency / see-through rendering is the job of the opacity property — not z-index.
Text size is set by the font-size property — not z-index.
Cross-check
Picture two absolutely-positioned boxes occupying the same region: give one z-index: 1 and the other z-index: 2, and the box valued 2 is drawn on top. Changing left/top would slide the boxes around the page, opacity would make one fade, and font-size would resize its text — none of those alter which box sits in front. Only the z-axis stacking-order description matches the property.