Skip to main content

Grid Layout Examples

Grid Layout Examples

CSS Grid is a powerful tool for creating a variety of layouts. In this section, we explore several real-world examples, including a responsive blog layout, a product gallery, and a dashboard interface, along with additional examples for further inspiration.


Responsive Blog Layout

Creating a responsive blog layout using CSS Grid ensures that your content is well-structured and adapts to different screen sizes.

Example: Responsive Blog Layout

.blog-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-gap: 20px;
}
.blog-post {
  background: #f9f9f9;
  padding: 20px;  
  border: 1px solid grey;
  border-radius: 8px;
}
<div class="blog-container">
  <article class="blog-post">
    <h2>Blog Post 1</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </article>
  <article class="blog-post">
    <h2>Blog Post 2</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </article>
  <article class="blog-post">
    <h2>Blog Post 3</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </article>
  <article class="blog-post">
    <h2>Blog Post 4</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </article>
</div>

This layout uses repeat(auto-fit, minmax(300px, 1fr)) to create a responsive grid that adjusts the number of columns based on the available width.


Product Gallery

A product gallery layout showcases products in a grid format, often with varying sizes and aspects. This example demonstrates a basic setup for a product gallery.

Example: Product Gallery

.gallery-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-gap: 15px;
}
.gallery-item {
  background: #eee;
  padding: 10px;
  border-radius: 5px;
  border: 1px solid grey;
  text-align: center;
}
<div class="gallery-container">
  <div class="gallery-item">
    <img src="product1.jpg" alt="Product 1" />
    <p>Product 1</p>
  </div>
  <div class="gallery-item">
    <img src="product2.jpg" alt="Product 2" />
    <p>Product 2</p>
  </div>
  <div class="gallery-item">
    <img src="product3.jpg" alt="Product 3" />
    <p>Product 3</p>
  </div>
</div>

This layout uses repeat(auto-fill, minmax(200px, 1fr)) to automatically adjust the number of columns based on the container width.


Dashboard Interface

Dashboard interfaces often require complex grid layouts to display multiple widgets or sections. Here’s an example of a simple dashboard layout.

Example: Dashboard Interface

.dashboard-container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-template-rows: auto 1fr;
  grid-gap: 20px;
  grid-template-areas:
    "header header"
    "sidebar content";
}
.header {
grid-area: header;
background: #333;
color: #fff;
padding: 20px;
}

.sidebar {
grid-area: sidebar;
background: #f4f4f4;
padding: 20px;
}

.content {
grid-area: content;
background: #fff;
padding: 20px;
}
<div class="dashboard-container">
  <div class="header">Header</div>
  <div class="sidebar">Sidebar</div>
  <div class="content">Main Content</div>
</div>

This layout uses named grid areas to allocate specific regions of the dashboard to different components.


Grid Layout Examples

Example 4: Portfolio Layout

A portfolio layout typically showcases various projects or works. Here’s how you can create a flexible portfolio grid.

.portfolio-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-gap: 20px;
}
.portfolio-item {
background: #ddd;
padding: 15px;
border-radius: 8px;
text-align: center;
}
<div class="portfolio-container">
  <div class="portfolio-item">
    <h3>Project 1</h3>
    <p>Description of Project 1</p>
  </div>
  <div class="portfolio-item">
    <h3>Project 2</h3>
    <p>Description of Project 2</p>
  </div>
  <div class="portfolio-item">
    <h3>Project 3</h3>
    <p>Description of Project 3</p>
  </div>
</div>

Example 5: Team Section

A team section can be laid out in a grid format to display team members’ profiles.

.team-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  grid-gap: 20px;
}
.team-member {
background: #f4f4f4;
padding: 15px;
border-radius: 10px;
text-align: center;
}
<div class="team-container">
  <div class="team-member">
    <img src="member1.jpg" alt="Member 1" />
    <p>Member 1</p>
  </div>
  <div class="team-member">
    <img src="member2.jpg" alt="Member 2" />
    <p>Member 2</p>
  </div>
  <div class="team-member">
    <img src="member3.jpg" alt="Member 3" />
    <p>Member 3</p>
  </div>
</div>

Example 6: Event Schedule

Displaying an event schedule can be efficiently managed using CSS Grid to lay out the time slots and events.

.schedule-container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-gap: 10px;
}
.schedule-item {
background: #eaeaea;
padding: 10px;
border-radius: 5px;
}
<div class="schedule-container">
  <div class="schedule-item">09:00 - 10:00</div>
  <div class="schedule-item">Opening Keynote</div>
  <div class="schedule-item">10:00 - 11:00</div>
  <div class="schedule-item">Session 1</div>
  <div class="schedule-item">11:00 - 12:00</div>
  <div class="schedule-item">Session 2</div>
</div>

Example 7: E-commerce Checkout Page

The checkout page in an e-commerce site often involves complex grid layouts for form fields, order summary, and payment details.

.checkout-container {
  display: grid;
  grid-template-columns: 2fr 1fr;
  grid-gap: 30px;
}
.order-summary {
background: #fafafa;
padding: 20px;
border-radius: 10px;
}

.payment-details {
background: #f0f0f0;
padding: 20px;
border-radius: 10px;
}
<div class="checkout-container">
  <div class="order-summary">
    <h2>Order Summary</h2>
    <p>Details of items in the cart</p>
  </div>
  <div class="payment-details">
    <h2>Payment Details</h2>
    <p>Form for entering payment information</p>
  </div>
</div>

Conclusion

CSS Grid can be used to create a wide range of layouts, from blogs and galleries to dashboards and product displays. These examples demonstrate the flexibility and power of CSS Grid in designing responsive, functional, and visually appealing web interfaces.

Comments