W3Newbie
  • HTML Tutorial
  • CSS Tutorial
  • Web Dev
    • VS Code Tutorial
    • Command Line Tutorial
    • Git Tutorial
    • Github Tutorial
    • Sass Tutorial
  • Courses
  • Resources
  • Tutorials
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

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.

Here’s how CSS Grid works:

1. Creating a CSS Grid Container:

  • 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.

2. Positioning Elements within the Grid:

  • 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.

3. Key Features and Advantages:

  • 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.
< Previous
Next Lesson >
  • CSS Tutorial
  • CSS Introduction
  • CSS Syntax
  • CSS Style Sheets
  • CSS Comments
  • CSS Fonts
  • CSS Sizing
  • CSS Colors
  • CSS Backgrounds
  • CSS Borders
  • CSS Outlines
  • CSS Margin
  • CSS Padding
  • CSS Box Model
  • CSS Floats
  • CSS Alignment
  • CSS Positioning
  • CSS Overflow
  • CSS Z-Index
  • CSS Opacity
  • CSS Pseudo-Classes
  • CSS Pseudo-Elements
  • CSS Buttons
  • CSS Icons
  • CSS Media Queries
  • CSS Responsive Web Design
  • CSS Navigations
  • CSS Animations
  • CSS Flexbox
  • CSS Grid
  • CSS Responsive Typography
  • CSS Mobile-First Responsive
  • CSS Fluid Layouts
  • CSS Variables

The Newbie Template Bundle

The 12 Website Bundle Pack

Subscribe on YouTube

Web Development Courses

The HTML Email Mastery Course - Build Responsive HTML Email Templates.

Latest Tutorial Posts

  • The HTML Email Mastery Course - Build Responsive HTML Email Templates.w3newbie.com
    HTML Email Mastery Course CouponOctober 12, 2020 - 8:10 pm
  • Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4w3newbie.com
    Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4April 22, 2019 - 1:48 pm
© w3newbie.com, 2026. Terms of Use. Privacy Policy.
Scroll to top Scroll to top Scroll to top