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

Nested Grids


CSS Subgrid allows a nested grid to align with its parent grid's tracks. This powerful feature simplifies complex layouts by enabling inner grid items to participate in the outer grid's sizing and alignment, creating more consistent and manageable designs.


Example 1: Basic Subgrid Adoption

/* This code demonstrates a basic subgrid implementation. */
.parent-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.nested-item {
  grid-column: 2 / 4; /* Span across two columns of the parent grid */
  display: grid;
  grid-template-columns: subgrid; /* Inherit the parent's track sizing */
  gap: 5px; /* Can have its own gap */
}

.sub-item {
  background-color: #f0f0f0;
}

Explanation

The .nested-item is a grid container itself but by setting grid-template-columns to subgrid, it doesn't define its own column tracks. Instead, it adopts the tracks of its parent, .parent-grid, allowing its children, .sub-item, to align perfectly within the parent's grid structure.


Example 2: Spanning Subgrid

/* This example shows a subgrid spanning specific parent tracks. */
.main-grid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: auto 1fr;
  gap: 15px;
}

.header-area {
  grid-column: 1 / -1; /* Span all columns */
  display: grid;
  grid-template-columns: subgrid;
}

.logo {
  grid-column: 1; /* Aligns to the first track of the parent grid */
  background-color: lightblue;
}

.nav {
  grid-column: 2 / 4; /* Aligns to the second and third tracks of the parent */
  background-color: lightgreen;
}

Explanation

Here, .header-area spans all columns of .main-grid. By using subgrid, its children, .logo and .nav, can be placed on the parent's grid tracks as if they were direct children of .main-grid, ensuring precise alignment across the header.


Example 3: Row and Column Subgrid

/* This code illustrates using subgrid for both rows and columns. */
.container-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 100px);
  gap: 10px;
}

.card {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
  display: grid;
  grid-template-columns: subgrid;
  grid-template-rows: subgrid; /* Inherit parent's row tracks as well */
}

.card-title {
  grid-column: 1;
  grid-row: 1;
  background-color: #ffcccb;
}

.card-body {
  grid-column: 2;
  grid-row: 1 / 3; /* Spans both inherited rows */
  background-color: #add8e6;
}

Explanation

The .card element adopts both the column and row tracks from .container-grid. This allows elements inside the card, like .card-title and .card-body, to align with the overall page grid both horizontally and vertically.


Example 4: Subgrid for Form Layout

/* This demonstrates using subgrid for clean form alignment. */
.form-grid {
  display: grid;
  grid-template-columns: 100px 1fr;
  gap: 20px;
}

.form-row {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: subgrid; /* Aligns all labels and inputs */
  gap: 10px; /* Overrides parent gap */
}

.form-row label {
  text-align: right;
  background-color: #eee;
}

Explanation

Each .form-row becomes a subgrid of the main .form-grid. This ensures that all <label> and <input> elements across different rows align perfectly into the two columns defined by the parent, creating a visually consistent form.


Example 5: Aligning Nested Content

/* This shows how subgrid helps align deeply nested items. */
.page-layout {
  display: grid;
  grid-template-columns: 200px 1fr;
}

.content {
  grid-column: 2;
  display: grid;
  grid-template-columns: subgrid;
}

.article-header {
  grid-column: 1 / -1; /* Spans the subgridded column */
  display: grid;
  grid-template-columns: subgrid;
}

.article-title {
  grid-column: 1;
  background-color: #f9f9f9;
}

Explanation

This example features a nested subgrid. The .content inherits tracks from .page-layout, and .article-header in turn inherits from .content. This allows .article-title to align with the grid track originally defined two levels up in .page-layout, demonstrating subgrid's power in complex alignments.