Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Basic

CSS Grid Layout Module

Posted in CSS Tutorial
Updated on Sep 03, 2024
By Mari Selvan
πŸ‘οΈ 8 - Views
⏳ 4 mins
πŸ’¬ 0
CSS Grid Layout Module

Photo Credit to CodeToFun

πŸ™‹ Introduction

The CSS Grid Layout Module is a powerful tool that allows you to create complex web layouts with ease.

Unlike traditional layout methods like floats and flexbox, CSS Grid provides a two-dimensional system, enabling you to design layouts with both rows and columns.

This module is ideal for creating responsive, grid-based layouts that adjust seamlessly across different screen sizes.

❓ What Is CSS Grid Layout?

CSS Grid Layout is a layout system optimized for web pages. It allows you to create complex grid-based designs using rows and columns, giving you fine control over the placement of elements.

Unlike other layout methods, CSS Grid works with both fixed and flexible track sizes, making it versatile and easy to use for various types of layouts.

πŸ—οΈ Key Features of CSS Grid

  • Two-Dimensional Layout: Control both rows and columns simultaneously.
  • Flexible Sizing: Define grid tracks with flexible or fixed dimensions.
  • Alignment Control: Align items precisely within the grid, both horizontally and vertically.
  • Grid Areas: Group grid cells into a single area that can span multiple rows and columns.

πŸ‘©β€πŸ« Basic Concepts

  • Grid Container: The parent element that defines a grid context for its children.
  • Grid Item: The children of the grid container, which are placed on the grid.
  • Grid Tracks: Rows and columns that make up the grid.
  • Grid Lines: The dividing lines between grid tracks.
  • Grid Area: A rectangular space defined by four grid lines.

πŸ”² Defining a Grid

To create a grid, you need to define a grid container and specify the number of rows and columns using the display: grid property. Here’s an example:

CSS
Copied
Copy To Clipboard
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 100px 200px;
  gap: 10px;
}

This code creates a grid with three equal-width columns and two rows of 100px and 200px height, with a 10px gap between them.

πŸ›οΈ Placing Items on the Grid

Grid items can be placed explicitly on the grid using properties like grid-column and grid-row. This allows you to position items precisely within the grid layout.

CSS
Copied
Copy To Clipboard
.item1 {
  grid-column: 1 / 3;
  grid-row: 1 / 2;
}

.item2 {
  grid-column: 2 / 4;
  grid-row: 2 / 3;
}

πŸ“Š Grid Lines and Areas

Grid lines are the invisible lines that define the boundaries of grid tracks. You can reference these lines when placing grid items. Grid areas allow you to group multiple grid cells into a single unit.

CSS
Copied
Copy To Clipboard
.container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-areas:
    "header header header header"
    "sidebar main main main"
    "footer footer footer footer";
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.main {
  grid-area: main;
}

.footer {
  grid-area: footer;
}

🎨 Responsive Grid Design

CSS Grid is inherently responsive, but you can enhance its responsiveness by using media queries and the auto-fit and auto-fill keywords for flexible track sizes. This ensures that your grid layout adapts to different screen sizes.

CSS
Copied
Copy To Clipboard
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
}

⚠️ Common Pitfalls

  • Overlapping Items: Be cautious when positioning items, as grid lines can cause items to overlap if not carefully managed.
  • Complexity: The flexibility of CSS Grid can lead to overly complex layouts. Start simple and build up as needed.
  • Browser Compatibility: Although widely supported, some older browsers may not fully support all CSS Grid features. Always test your layout across different browsers.

πŸ“ Example

Here’s a complete example of a responsive grid layout using CSS Grid:

HTML
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example</title>
  <style>
    .container {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
      gap: 10px;
    }
    .item {
      background-color: #ddd;
      padding: 20px;
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="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>
</body>
</html>

πŸŽ‰ Conclusion

The CSS Grid Layout Module offers unparalleled control and flexibility for designing web layouts. By mastering its concepts and features, you can create complex, responsive layouts with ease. Whether you're building simple page structures or intricate designs, CSS Grid is a powerful tool that should be in every web developer's toolkit.

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy