CSS Grid is a powerful layout system that allows you to create complex web designs with ease. It provides a two-dimensional grid structure, enabling you to control both rows and columns.

📌 Basic Concepts

  • Grid Container: The parent element that becomes a grid. Use display: grid; to enable it.
  • Grid Items: Child elements placed within the grid. They automatically align to the grid lines.
  • Grid Lines: The lines that form the grid structure. You can specify them using grid-template-rows and grid-template-columns.

📌 Tip: Use grid-gap (now gap) to add spacing between items. For example: gap: 10px;

🛠️ Layout Techniques

  1. Auto-fit: Automatically adjust the number of columns to fit the container.
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    
  2. Fr Units: Define flexible space that adjusts based on available area.
    grid-template-rows: 1fr 2fr 1fr;
    
  3. Grid Areas: Define regions for specific items.
    grid-template-areas: 
      "header header"
      "nav main"
      "footer footer";
    

📚 Best Practices

  • Responsive Design: Use auto-fit and minmax for adaptive layouts.
  • Accessibility: Ensure proper alignment and spacing for readability.
  • Performance: Avoid overusing nested grids; keep it simple where possible.

🌐 Expand Your Knowledge

For advanced examples and real-world use cases, check out our CSS Grid Examples Tutorial.

css_grid_layout

📌 Common Properties

Property Description
grid-template-columns Defines the number and size of columns
grid-template-rows Defines the number and size of rows
grid-auto-rows Defines the size of automatically generated rows
grid-column Positions an item within columns
grid-row Positions an item within rows
grid_template_areas

🛠️ Hands-On Practice

Try creating a grid layout using the following steps:

  1. Set the container with display: grid;
  2. Define columns and rows using grid-template-columns and grid-template-rows
  3. Place items using grid-column and grid-row

For more interactive examples, visit our CSS Grid Playground.

grid_row_layout