Quick guide to CSS Grid

tlakomy

Tomasz Łakomy

Posted on December 27, 2023

Quick guide to CSS Grid

Grid Container

First, you designate an element as a grid container. This element will hold the grid items (your content). You can do this by setting the element's display property to grid or inline-grid.

.container {
  display: grid;
}
Enter fullscreen mode Exit fullscreen mode
/* Or for an inline grid */
.inline-container {
  display: inline-grid;
}
Enter fullscreen mode Exit fullscreen mode

Defining Rows and Columns

You define the rows and columns in the grid container using grid-template-rows and grid-template-columns.

.container {
  display: grid;
  grid-template-columns: 100px 200px auto;
  grid-template-rows: 50px 100px;
}
Enter fullscreen mode Exit fullscreen mode

Grid Gap

Grid gap is the space between each row and column. Use grid-gap, grid-row-gap, and grid-column-gap.

.container {
  display: grid;
  grid-gap: 10px; /* Space between rows and columns */
}
Enter fullscreen mode Exit fullscreen mode

Placing Items

You can place grid items in specific locations using grid-column-start, grid-column-end, grid-row-start, and grid-row-end.

.item {
  grid-column-start: 1;
  grid-column-end: 3;
  grid-row-start: 2;
  grid-row-end: 4;
}
Enter fullscreen mode Exit fullscreen mode

Shorthand Properties

There are shorthand properties for quicker definitions.

.item {
  grid-column: 1 / 3; /* Start at line 1 and end at line 3 */
  grid-row: 2 / 4;
}
Enter fullscreen mode Exit fullscreen mode

Fractional Units (fr)

The fr unit allows you to distribute available space in fractions.

.container {
  grid-template-columns: 1fr 2fr 1fr; /* 2nd column is twice as wide as the others */
}
Enter fullscreen mode Exit fullscreen mode

Repeat Function

To avoid repetition, use repeat().

.container {
  grid-template-columns: repeat(3, 1fr); /* Creates 3 columns of equal width */
}
Enter fullscreen mode Exit fullscreen mode

auto-fill and auto-fit

auto-fill and auto-fit are used with repeat() to automatically place as many items as possible.

.container {
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
}
Enter fullscreen mode Exit fullscreen mode

Grid Areas

Define areas in your grid and assign items to these areas.

.container {
  grid-template-areas: 
    "header header header"
    "sidebar content content"
    "footer footer footer";
}

.header {
  grid-area: header;
}
Enter fullscreen mode Exit fullscreen mode

Responsive Design

You can create responsive designs by combining CSS Grid with media queries.

.container {
  grid-template-columns: 1fr;
}

@media (min-width: 600px) {
  .container {
    grid-template-columns: 1fr 1fr;
  }
}
Enter fullscreen mode Exit fullscreen mode

Practice

The best way to learn CSS Grid is through practice. Try creating a basic layout with a header, sidebar, main content area, and footer. Experiment with different configurations and see how the elements rearrange themselves.

💖 💪 🙅 🚩
tlakomy
Tomasz Łakomy

Posted on December 27, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related