CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Z Index


The z-index property in CSS controls the vertical stacking order of elements that overlap. An element with a higher z-index will appear in front of an element with a lower one. A stacking context is formed when an element has a position value other than static and a z-index value other than auto, among other properties. This creates a new local stacking order for its descendant elements, which is then treated as a single unit in the parent's stacking context.


Example 1: Basic Stacking

/* style.css */
.box {
  position: absolute;
  width: 100px;
  height: 100px;
}

.red-box {
  background-color: red;
  z-index: 1; /* Appears on top */
}

.blue-box {
  background-color: blue;
  top: 50px;
  left: 50px;
  z-index: 0; /* Appears behind the red box */
}

Explanation The .red-box has a higher z-index than the .blue-box, so it is rendered on top. Both elements require a position property for z-index to have an effect.


Example 2: Nested Stacking Context

/* style.css */
.parent {
  position: relative;
  z-index: 1;
  background-color: lightgray;
  padding: 20px;
}

.child {
  position: absolute;
  width: 80px;
  height: 80px;
  background-color: coral;
  z-index: 999; /* High z-index but contained within parent */
}

.sibling {
    position: absolute;
    top: 0;
    left: 70px;
    width: 100px;
    height: 100px;
    background-color: darkcyan;
    z-index: 2; /* Higher than .parent */
}

Explanation Even though .child has a very high z-index, it is still rendered behind .sibling. This is because .child is within the stacking context of .parent, which has a lower z-index than .sibling.


Example 3: Opacity Creates a Stacking Context

/* style.css */
.element-one {
  position: relative;
  z-index: 2;
  background-color: lightgreen;
  width: 120px;
  height: 120px;
}

.element-two {
  position: absolute;
  top: 30px;
  left: 30px;
  width: 120px;
  height: 120px;
  background-color: mediumpurple;
  opacity: 0.9; /* Creates a new stacking context */
  z-index: 1;
}

Explanation Setting the opacity of .element-two to a value less than 1 creates a new stacking context. Even with a lower z-index, it can affect the stacking order of its children independently.


Example 4: Transform Creates a Stacking Context

/* style.css */
.container-a {
  position: absolute;
  z-index: 3;
}

.box-a {
  width: 100px;
  height: 100px;
  background-color: gold;
}

.container-b {
    position: absolute;
    top: 50px;
    left: 50px;
    transform: scale(1.1); /* Creates a new stacking context */
    z-index: 2;
}
.box-b {
    width: 100px;
    height: 100px;
    background-color: deeppink;
}

Explanation The transform property on .container-b creates a new stacking context. Despite .container-a having a higher z-index, .container-b and its child will be rendered based on this new context.


Example 5: The isolation Property

/* style.css */
.isolated-context {
  isolation: isolate; /* Creates a new stacking context without position/z-index */
  position: absolute;
  background-color: lightblue;
  padding: 25px;
}

.inner-element {
  position: absolute;
  width: 70px;
  height: 70px;
  background-color: crimson;
  z-index: -1; /* Will not go behind the body */
}

Explanation The isolation: isolate property creates a new stacking context on .isolated-context. This prevents the negatively z-indexed .inner-element from disappearing behind the parent element's own background.


Example 6: Default Stacking Order

/* style.css */
.first-element {
  position: relative;
  width: 150px;
  height: 150px;
  background-color: orange;
  /* No z-index, defaults to auto (0) */
}

.second-element {
  position: absolute;
  top: 75px;
  left: 75px;
  width: 150px;
  height: 150px;
  background-color: teal;
  /* No z-index, defaults to auto (0) */
}

Explanation When positioned elements have the same z-index (or none is specified), the element that comes later in the HTML source order will appear on top. Here, .second-element stacks on top of .first-element.


Example 7: Negative z-index

/* style.css */
.parent-container {
  position: relative;
  background-color: #eee;
  padding: 40px;
}

.foreground {
  position: relative;
  z-index: 1;
  background-color: cornflowerblue;
  padding: 20px;
  color: white;
}

.background {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 200px;
  height: 100px;
  background-color: #333;
  z-index: -1; /* Goes behind its parent's content */
}

Explanation A negative z-index places an element behind its parent. In this case, .background is positioned behind the content and background of its parent, .parent-container.