CSS Grid Examples

Practical implementations of CSS Grid for modern web layouts

What are CSS Grid Examples?

CSS Grid Examples showcase practical implementations of CSS Grid Layout for real-world web design challenges. These examples demonstrate how to use Grid features to create responsive, maintainable, and visually appealing layouts.

Types of Grid Examples:

  • Basic Grids - Simple grid structures
  • Named Grids - Using named lines and areas
  • Responsive Layouts - Adaptive grid designs
  • Card Grids - Content card layouts
  • Dashboard Layouts - Admin panel designs
  • Form Layouts - Structured form designs
  • Gallery Layouts - Image and media grids
  • Pricing Tables - Responsive pricing grids

CSS Grid Examples Reference

ExampleDescriptionKey Features
Basic GridSimple grid with fixed columns and rowsgrid-template-columns, grid-template-rows
Named LinesGrid with named lines for precise placementNamed grid lines, line-based placement
Grid AreasLayout using named template areasgrid-template-areas, grid-area
Responsive GalleryAdaptive image gallery with auto placementauto-fill, minmax(), grid-auto-rows
DashboardAdmin dashboard with nested gridsNested grids, template areas
Pricing TableResponsive pricing comparison gridauto-fit, card layout, hover effects
Form LayoutStructured form with aligned labels and inputsNamed grid lines, alignment
Card GridResponsive content card layoutNested grids, auto-fit, card design

CSS Grid Examples in Action

Example Code

/* CSS Grid Examples */

/* Basic Grid Example */
.basic-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 100px 200px;
  gap: 20px;
}

/* Grid with Named Lines */
.named-lines-grid {
  display: grid;
  grid-template-columns: [start] 100px [content-start] 1fr [content-end] 100px [end];
  grid-template-rows: [header] 80px [main] 1fr [footer] 60px;
  gap: 15px;
}

.header { grid-column: start / end; grid-row: header; }
.sidebar { grid-column: start / content-start; grid-row: main; }
.content { grid-column: content-start / content-end; grid-row: main; }
.footer { grid-column: start / end; grid-row: footer; }

/* Grid with Areas */
.areas-grid {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
  grid-template-rows: 80px 1fr 60px;
  grid-template-columns: 200px 1fr 1fr;
  gap: 15px;
  min-height: 100vh;
}

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

/* Responsive Image Gallery */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-auto-rows: 200px;
  gap: 15px;
}

.gallery-item {
  overflow: hidden;
  border-radius: 8px;
  position: relative;
}

.gallery-item:nth-child(3n) {
  grid-row: span 2;
}

.gallery-item:nth-child(5n) {
  grid-column: span 2;
}

/* Dashboard Layout */
.dashboard-grid {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 70px 1fr 40px;
  grid-template-areas:
    "sidebar header"
    "sidebar main"
    "sidebar footer";
  min-height: 100vh;
}

.dashboard-header { grid-area: header; }
.dashboard-sidebar { grid-area: sidebar; }
.dashboard-main { 
  grid-area: main;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  padding: 20px;
}
.dashboard-footer { grid-area: footer; }

/* Pricing Grid */
.pricing-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 25px;
  padding: 30px;
}

.pricing-card {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  padding: 25px;
  text-align: center;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.pricing-card:hover {
  transform: translateY(-5px);
}

/* Form Layout Grid */
.form-grid {
  display: grid;
  grid-template-columns: [label] auto [input] 1fr [help] auto;
  gap: 15px;
  align-items: center;
}

.form-label {
  grid-column: label;
  text-align: right;
  padding-right: 10px;
  font-weight: 500;
}

.form-input {
  grid-column: input;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.form-help {
  grid-column: help;
  padding-left: 10px;
  color: #666;
  font-size: 0.9em;
}

/* Card Grid with Content */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 25px;
  padding: 20px;
}

.card {
  display: grid;
  grid-template-rows: 200px auto auto 1fr auto;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.card:hover {
  transform: translateY(-5px);
}

.card-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.card-content {
  padding: 20px;
}

/* Calendar Grid */
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  background-color: #e0e0e0;
  border: 1px solid #e0e0e0;
}

.calendar-header {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background-color: #f5f5f5;
  font-weight: bold;
  text-align: center;
  padding: 10px;
}

.calendar-day {
  background-color: white;
  min-height: 100px;
  padding: 8px;
  position: relative;
}

.calendar-day.other-month {
  background-color: #f9f9f9;
  color: #ccc;
}

/* Masonry Layout */
.masonry-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-auto-rows: 10px;
  gap: 15px;
}

.masonry-item {
  border-radius: 8px;
  overflow: hidden;
  background-color: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.masonry-item.small { grid-row-end: span 20; }
.masonry-item.medium { grid-row-end: span 30; }
.masonry-item.large { grid-row-end: span 40; }

/* Overlapping Elements */
.overlap-grid {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
}

.overlap-item {
  grid-column: 1 / -1;
  grid-row: 1 / -1;
}

.overlap-content {
  z-index: 2;
  padding: 20px;
  color: white;
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.8);
}

.overlap-background {
  z-index: 1;
  background-size: cover;
  background-position: center;
  filter: brightness(0.7);
}

/* Asymmetric Grid */
.asymmetric-grid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: repeat(3, 150px);
  gap: 20px;
}

.asymmetric-item:nth-child(1) {
  grid-column: 1 / 3;
  grid-row: 1;
}

.asymmetric-item:nth-child(2) {
  grid-column: 3;
  grid-row: 1 / 3;
}

.asymmetric-item:nth-child(3) {
  grid-column: 1;
  grid-row: 2 / 4;
}

.asymmetric-item:nth-child(4) {
  grid-column: 2 / 4;
  grid-row: 3;
}

/* Grid with Animation */
.animated-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.animated-item {
  transition: all 0.3s ease;
  transform-origin: center;
}

.animated-item:hover {
  transform: scale(1.05);
  z-index: 2;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

/* Nested Grid */
.nested-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto;
  gap: 20px;
}

.nested-item {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  gap: 10px;
  padding: 15px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
}

.nested-icon {
  grid-row: 1 / -1;
  align-self: center;
  font-size: 24px;
  margin-right: 10px;
}

/* Grid with Aspect Ratio */
.aspect-ratio-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.aspect-ratio-item {
  aspect-ratio: 16/9;
  position: relative;
  overflow: hidden;
  border-radius: 8px;
}

.aspect-ratio-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  background: linear-gradient(45deg, rgba(0,0,0,0.7), rgba(0,0,0,0.3));
}

/* CSS Grid with Variables */
:root {
  --grid-cols: 1;
  --grid-rows: 1;
  --grid-gap: 20px;
}

.variable-grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-cols), 1fr);
  grid-template-rows: repeat(var(--grid-rows), auto);
  gap: var(--grid-gap);
}

@media (min-width: 768px) {
  .variable-grid {
    --grid-cols: 2;
    --grid-gap: 25px;
  }
}

@media (min-width: 1024px) {
  .variable-grid {
    --grid-cols: 3;
  }
}

/* Grid with Subgrid */
.subgrid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.subgrid-item {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 3;
  gap: 10px;
}

.subgrid-content {
  padding: 10px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
}

Grid Example Techniques

Basic Grid Structure

The foundation of all CSS Grid layouts with explicit columns and rows.

Responsive Gallery

Adaptive grid that adjusts to different screen sizes with auto-placement.

Dashboard Layout

Complex layout with sidebar and nested grid for widgets.

Card Grid Layout

Responsive card layout with consistent design and nested content.

Best Practices for Grid Examples

Implementation Tips

  • Start with mobile-first responsive approach
  • Use semantic HTML with grid for layout
  • Combine grid with flexbox for component layouts
  • Use CSS variables for consistent spacing
  • Test across various screen sizes and devices
  • Use named areas and lines for maintainability

Common Mistakes to Avoid

  • Overcomplicating grid structures unnecessarily
  • Not providing fallbacks for older browsers
  • Forgetting to test with real content
  • Using too many grid properties when simpler solutions exist
  • Ignoring accessibility when reordering content visually
  • Not considering performance with complex nested grids

Browser Support & Accessibility

  • CSS Grid is supported in all modern browsers
  • Use feature queries (@supports) for progressive enhancement
  • Ensure proper reading order when using grid for visual reordering
  • Test with screen readers and keyboard navigation
  • Consider using logical properties for RTL support
  • Provide appropriate focus indicators for interactive grid items

Ready to Try It Yourself?

Experiment with CSS Grid Examples in our interactive editor. See your changes in real-time and build your understanding through practice.

< PreviousNext >