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

Grid Template Areas


The grid-template-areas property in CSS Grid Layout allows you to assign names to grid areas, creating a visual representation of your layout. This makes your CSS more readable and easier to maintain, especially for complex designs.


Example 1: Basic Website Layout

.container {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
  grid-gap: 10px;
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

Explanation

This code defines a classic website layout. The grid-template-areas property maps out the structure, and each grid item is assigned to its named area using the grid-area property.


Example 2: Two-Column Blog Layout

.blog-layout {
  display: grid;
  grid-template-areas:
    "post meta"
    "post comments";
  grid-template-columns: 3fr 1fr;
  grid-gap: 15px;
}

.post { grid-area: post; }
.meta { grid-area: meta; }
.comments { grid-area: comments; }

Explanation

Here, we create a two-column layout where the main post content spans two rows on the left, while the metadata and comments occupy the right column.


Example 3: Empty Grid Cells

.card-layout {
  display: grid;
  grid-template-areas:
    "image title"
    "image ."
    "image cta";
  grid-template-rows: auto 1fr auto;
}

.card-image { grid-area: image; }
.card-title { grid-area: title; }
.card-cta { grid-area: cta; }

Explanation

A period (.) in the grid-template-areas declaration signifies an empty or unoccupied grid cell, allowing for more complex and asymmetrical layouts.


Example 4: Responsive Layout

.responsive-container {
  display: grid;
  grid-template-areas:
    "hero"
    "content"
    "aside";
}

@media (min-width: 768px) {
  .responsive-container {
    grid-template-areas: "hero hero" "content aside";
  }
}

Explanation

This example demonstrates how to create a responsive layout. On smaller screens, the areas are stacked; on screens 768px or wider, they rearrange into a two-column format.


Example 5: Overlapping Areas

.overlapping-layout {
  display: grid;
  grid-template-areas:
    "background background"
    ". foreground";
}

.background-item { grid-area: background; z-index: 1; }
.foreground-item { grid-area: foreground; z-index: 2; }

Explanation

Grid areas can be made to overlap. You can then control the stacking order of the items within those areas using the z-index property.


Example 6: Centered Content

.centered-layout {
  display: grid;
  height: 100vh;
  grid-template-areas:
    ". . ."
    ". content ."
    ". . .";
  grid-template-columns: 1fr auto 1fr;
  grid-template-rows: 1fr auto 1fr;
}

.content-box { grid-area: content; }

Explanation

By using empty grid cells around a named area, you can easily center content both horizontally and vertically within its parent container.


Example 7: Complex Dashboard Layout

.dashboard {
  display: grid;
  grid-template-areas:
    "header header header"
    "nav main charts"
    "nav main stats";
  grid-template-columns: 200px 1fr 250px;
  grid-template-rows: auto 1fr auto;
}

.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.charts { grid-area: charts; }
.stats { grid-area: stats; }

Explanation

grid-template-areas is highly effective for organizing complex layouts like dashboards, making the structure immediately clear from the CSS itself.