Explanation of CSS Grid Layout

picture[1]-Explanation of CSS Grid Layout - PANDA-PANDA

CSS Grid Layout enables the creation of complex grid structures, allowing for flexible page layouts. By defining grid containers and grid items, you can precisely control the positioning and size of elements.

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 20px;
}

.grid-item {
  grid-column: span 2;
  grid-row: span 1;
}

CSS Grid Layout is a powerful two-dimensional grid-based layout system that allows you to create complex and responsive web layouts. It enables you to divide a web page into rows and columns, placing content into specific grid areas.

Basic Concepts:

1. Grid Container and Grid Items:

  • Grid Container: The parent element that contains grid items. Apply display: grid; to create a grid container.
  • Grid Items: The direct children of the grid container. These items will be placed within the grid.

2. Grid Lines and Tracks:

  • Grid Lines: Horizontal and vertical lines that form the grid. They can be referred to by their line numbers (start counting from 1) or by using named lines.
  • Grid Tracks: Spaces between the grid lines, forming rows and columns.

How to Create a Grid:

1. Define a Grid Container:

.container {
  display: grid;
  /* Optional grid properties */
  grid-template-rows: 100px 200px; /* Define row sizes */
  grid-template-columns: 1fr 2fr; /* Define column sizes */
  gap: 10px; /* Define gaps between grid items */
}

2. Place Items into the Grid:

<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>

3. Define Items’ Placement in the Grid:

.item {
  /* Define item placement */
  grid-row: 1 / span 1; /* Place in row line 1, span 1 row */
  grid-column: 1 / span 1; /* Place in column line 1, span 1 column */
}

Important Properties:

1. grid-template-rows and grid-template-columns:

  • Define the sizes of rows and columns within the grid.

2. grid-gap or gap:

  • Set the gaps (spacing) between rows and columns.

3. grid-row and grid-column:

  • Determine the placement of grid items by specifying the start and end positions for rows and columns.

4. grid-template-areas:

  • Assign names to specific grid areas, simplifying the layout process.

5. align-items, justify-items, align-content, justify-content:

  • Align and justify items or content within the grid both vertically and horizontally.

Example:

.container {
  display: grid;
  grid-template-rows: 100px 200px;
  grid-template-columns: 1fr 2fr;
  gap: 10px;
}

.item {
  /* Example item placement */
  grid-row: 1 / span 1;
  grid-column: 1 / span 1;
}
<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <!-- Add more items -->
</div>

The above example demonstrates a grid with two rows, two columns, and items placed in specific grid areas.

CSS Grid Layout provides a flexible and robust way to create responsive designs. It allows for easy rearrangement of content and is well-supported across modern browsers.

THE END
Like6 Share
comment
 avatar
Your valuable insights are welcome!
Submit
 avatar

Nickname

cancel
NicknameSmiliesCodeImage

    No comments