CSS Grid

CSS Grid is a powerful layout system that lets you arrange elements on your web page like tiles on a grid, offering precise control over placement and responsiveness across different screen sizes. Think of it as virtual graph paper for building flexible and visually appealing web layouts.

  • Use the display: grid property on a container element to establish a grid layout.
  • Define the grid structure using properties like grid-template-columns and grid-template-rows to create rows and columns, forming a grid-like framework.
  • Place individual child elements within the grid using either:
    • Grid Lines: Reference specific grid lines using grid-column and grid-row properties.
    • Grid Areas: Define named grid areas and place elements within them using grid-area.
  • Flexibility: Create a wide range of layouts, from simple to intricate, with precise control over element placement.
  • Responsiveness: Automatically adapt layouts to different screen sizes using media queries and grid template functions.
  • Gaps and Gutters: Manage spacing between grid items using grid-gap or grid-column-gap and grid-row-gap properties.
  • Alignment: Easily align grid items both horizontally and vertically.
  • Template Areas: Define named grid areas for intuitive element placement.
  • Repeating Patterns: Create repeating patterns in the grid structure for efficiency.

CSS Grid with HTML:

1. Simple Grid with Two Columns:

HTML:

<div class="grid-container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
</div>

CSS:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr; /* Two equal columns */
}

2. Grid with Three Rows and Two Columns:

HTML:

<div class="grid-container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
  <div class="item">Item 4</div>
  <div class="item">Item 5</div>
  <div class="item">Item 6</div>
</div>

CSS:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr; /* Two columns */
  grid-template-rows: auto auto auto; /* Three rows */
}

3. Grid with Named Grid Areas:

HTML:

<div class="grid-container">
  <div class="item header">Header</div>
  <div class="item sidebar">Sidebar</div>
  <div class="item content">Main Content</div>
  <div class="item footer">Footer</div>
</div>

CSS:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 3fr; /* Two columns with width ratio 1:3 */
  grid-template-rows: auto 1fr auto; /* Three rows */
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
}

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

4. Grid with Item Placement:

CSS:

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* Four equal columns */
}

.item-1 { grid-column: 1 / 3; } /* Spans two columns */
.item-2 { grid-row: 2; grid-column: 3; } /* Places in second row, third column */

Benefits of Using CSS Grid:

  • Precise Layout Control: Achieve complex layouts with greater ease and flexibility compared to traditional methods like floats or tables.
  • Responsive Design: Build layouts that seamlessly adapt to different screen sizes, ensuring optimal user experience across devices.
  • Improved Code Readability: Often results in cleaner and more maintainable code compared to older layout techniques.

Key Points:

  • display: grid: Enables the grid layout on a container element.
  • grid-template-columns: Defines the column structure.
  • grid-template-rows: Defines the row structure.
  • grid-template-areas: Assigns named grid areas to items.
  • grid-column and grid-row: Position items within the grid.
  • CSS grid is a powerful layout tool for creating complex and responsive layouts.