css grid cheat sheet for beginners and pros

Author

Reads 1.3K

Abstract image featuring a grid of glowing pink dots on a black background creating a vibrant pattern.
Credit: pexels.com, Abstract image featuring a grid of glowing pink dots on a black background creating a vibrant pattern.

CSS Grid is a game-changer for web development, allowing you to create complex layouts with ease.

With CSS Grid, you can define a grid container and its grid items, which can be placed anywhere within the grid.

One of the most powerful features of CSS Grid is the ability to use grid templates, which are defined using the grid-template-columns and grid-template-rows properties.

Grid templates can be used to create repeating patterns, such as a grid of images or a grid of text blocks.

To create a grid with multiple rows and columns, you can use the grid-template-columns and grid-template-rows properties, respectively.

In CSS Grid, you can also use the grid-auto-columns and grid-auto-rows properties to define the implicit grid, which is the grid that is automatically created by the browser when it is not explicitly defined.

CSS Grid Basics

As of March 2017, most browsers shipped native, unprefixed support for CSS Grid, making it a great time to start building with grid.

Credit: youtube.com, CSS Grid in 100 Seconds

You can define a container element as a grid with the display: grid property, which is the first step to getting started with CSS Grid.

To set up a grid, you'll also need to set the column and row sizes with grid-template-columns and grid-template-rows properties, and then place its child elements into the grid with grid-column and grid-row properties.

The source order of the grid items doesn't matter, which makes it super easy to rearrange your grid with media queries.

Understanding CSS Grid

CSS Grid is a two-dimensional layout system rooted in Cascading Style Sheets (CSS).

It enables developers to construct complex layouts with ease.

As of March 2017, most browsers shipped native, unprefixed support for CSS Grid, making it a great time to start building with grid.

You can define a container element as a grid with display: grid.

Grid-template-columns and grid-template-rows are used to set the column and row sizes.

Credit: youtube.com, CSS Grid in 100 Seconds

Grid-column and grid-row are used to place child elements into the grid.

The source order of the grid items doesn't matter, making it super easy to rearrange your grid with media queries.

Here are the advantages of using CSS Grid:

  • Streamlining the development of responsive and adaptable web designs with features such as grid template columns repeat
  • Offering greater control over layout, as it allows you to define the structure of your grid using grid lines
  • Allowing you to create intricate layouts without relying on external frameworks or complex hacks

Row

The row in CSS Grid is a crucial aspect of creating complex layouts. It's defined by the grid-template-rows property, which sets the size and number of rows in the grid container.

You can use the grid-row property to position grid items within the row, specifying the starting and ending row values separated by a /. This shorthand property is a convenient way to set both grid-row-start and grid-row-end properties at once.

By using the grid-row property, you can control the positioning and sizing of elements within the row, making it easier to create intricate and adaptable layouts.

The grid-auto-flow property also affects how grid items are placed within the row. If you set grid-auto-flow to row (which is the default), the auto-placement algorithm will fill in each row in turn, adding new rows as necessary. This means that if you only specify spots for two grid items, the remaining items will flow across the available rows.

Here's a quick summary of the grid-auto-flow values that control row placement:

CSS Grid Properties

Credit: youtube.com, CSS Grid in 4 Minutes (+ Cheatsheet)

CSS Grid Properties are the building blocks of a grid layout, and they're what make it so powerful. You can use properties like `grid-template-columns` and `grid-template-rows` to specify the track list for the columns and rows, giving you precise control over your layout's structure.

The `grid-gap` property is a shorthand for `grid-row-gap` and `grid-column-gap`, and it allows you to create space between rows and columns within the grid. It's a convenient way to add some breathing room between your grid items.

You can also use the `fr` unit to set the size of a track as a fraction of the space available within the grid container. This is a flexible sizing unit that lets you create responsive and adaptive designs that cater to different screen sizes and resolutions.

Column

The column property in CSS Grid is a crucial aspect of creating responsive and adaptable layouts.

You can use the grid-column property as a shorthand for grid-column-start and grid-column-end, specifying the starting and ending column lines for a grid item. If no end column line value is declared, the item will span 1 track by default.

Credit: youtube.com, Learn CSS Grid - A 13 Minute Deep Dive

Grid-template-columns is used to specify the number of columns of the grid and the widths of each column, with the number of width values determining the number of columns. Each width value can be either in pixels (px) or percentages (%).

Grid-column-start and grid-column-end determine a grid item's location within the grid by referring to specific grid lines. You can use the span keyword to indicate how many grid tracks an item will span.

The fr unit allows you to set the size of a track as a fraction of the free space in the grid container. For example, setting each item to one third the width of the grid container would look like this: 1fr 1fr 1fr.

Here are some examples of column properties:

  • grid-column: 1 / 3 (specifies the starting and ending column lines for a grid item)
  • grid-column-start: 2 (specifies the starting column line for a grid item)
  • grid-column-end: span 2 (specifies the ending column line for a grid item, spanning 2 tracks)
  • grid-template-columns: 1fr 2fr 1fr (specifies the number and widths of columns in the grid)

By mastering the column properties, you can create complex and adaptable layouts that cater to various screen sizes and resolutions.

Justify

Justify is a crucial aspect of CSS Grid, allowing you to control how grid items are aligned along the inline (row) axis.

Credit: youtube.com, CSS Grid Tutorial #6 - Aligning & Justifying Items

The justify-items property is used on a grid container to determine how the grid items are spread out along a row by setting the default justify-self property for all child boxes.

You can align grid items to the left side of the grid area by setting the value start, or to the center of the grid area by setting the value center.

The justify-self property is employed to align a specific grid item within its grid cell along the row (inline) axis, allowing you to override the default justification set by justify-items for individual items.

Here are the possible values for the justify-self property:

  • start – aligns the grid item to be flush with the start edge of the cell
  • end – aligns the grid item to be flush with the end edge of the cell
  • center – aligns the grid item in the center of the cell
  • stretch – fills the whole width of the cell (this is the default)

By mastering the art of justifying grid items horizontally, you can create visually appealing and well-structured layouts that cater to various screen sizes and resolutions.

The Fr Unit

The fr unit is a flexible sizing unit that allows for dynamic layouts that adapt to different screen sizes and resolutions. It represents a fraction of the available space within a grid container.

Credit: youtube.com, The FR unit — Fractional units in CSS grid

You can use the fr unit to set the size of a track as a fraction of the space available within the grid container. For example, 1fr means one-third of the available space.

The fr unit can be used in conjunction with other units like pixels or percentages to create even more dynamic layouts that cater to various devices and screen sizes. This is especially useful when creating intricate grid layouts that need to adapt to different screen sizes and resolutions.

The fr unit is a key component of CSS Grid, allowing you to create flexible and responsive grid layouts with ease. It's a powerful tool that can help you create polished and professional designs.

Here's a breakdown of how the fr unit works:

  • 1fr is equivalent to 33.33%
  • 2fr is equivalent to 66.67%

The fr unit is also friendly in combination with other units, such as the min() function and the max() function. This allows for even more flexibility in your layout design.

The free space is calculated after any non-flexible items, so you need to consider this when using the fr unit in your grid layouts. For example, if you have a 50px non-flexible item, the total amount of free space available to the fr units doesn't include this.

By utilizing the fr unit in your grid layouts, you can easily create responsive and adaptive designs that cater to the needs of your users.

Supported Browsers

Credit: youtube.com, How to Support All Browsers with Your CSS

CSS Grid Properties are supported by several modern browsers. Google Chrome 57.0 and later versions support CSS Grid.

Microsoft Edge 16.0 and later versions also support CSS Grid. Mozilla Firefox 52.0 and later versions support CSS Grid. Safari 10.1 and later versions support CSS Grid.

Opera 44.0 also supports CSS Grid. Note that older versions of these browsers may not fully support all CSS Grid properties. Always ensure compatibility by checking for browser updates or using fallback CSS techniques for older browser versions.

Here's a quick rundown of the supported browsers:

Grid Item Properties

Grid items are the children of the grid container, meaning they're the direct descendants. This is important to understand because it affects how they behave and interact with the grid.

A grid item can be any element that's placed inside the grid container. This can include item elements, but it's not limited to them.

Item

An item in a grid system is essentially a child of the grid container, and it's considered a grid item. Direct descendants of the grid container are grid items, but not their sub-items.

Credit: youtube.com, 47. CSS - Grid Item Properties

The properties for grid items are quite extensive, and they include things like grid-column-start, grid-column-end, grid-row-start, grid-row-end, grid-column, grid-row, grid-area, justify-self, align-self, and place-self.

You can't use float, display: inline-block, display: table-cell, vertical-align, or column-* properties on a grid item, as they have no effect.

Grid items can overlap each other, and you can use z-index to control their stacking order.

Here are some properties that define how grid items are laid out:

Grid items can also have gaps between them, and you can control this using properties like grid-column-gap, grid-row-gap, and grid-gap.

Align

Aligning grid items is a crucial aspect of creating visually appealing and well-structured layouts. The align-items property is used on a grid container to determine how the grid items are spread out along the column.

By default, the value of align-items is stretch, which fills the whole height of the cell. However, you can also use start, end, center, or baseline to align items accordingly. For example, using align-items: start will align items to be flush with the start edge of their cell.

Credit: youtube.com, The how and why: align-items & align-content in flex and grid

The align-self property, on the other hand, allows you to override the default alignment set by align-items for individual grid items. This means you can set a specific alignment for each grid item, giving you more granular control over your layout.

Here are the possible values for align-self: start, end, center, and stretch. These values work similarly to align-items, but are applied to individual grid items instead of the entire grid container.

You can also use modifier keywords like safe and unsafe with align-items and align-self properties. The safe keyword means "try to align like this, but not if it means aligning an item such that it moves into inaccessible overflow area", while unsafe will allow moving content into inaccessible areas.

By using align-items and align-self properties, you can create a wide range of layouts that cater to various screen sizes and resolutions. Whether you need to align grid items at the top, middle, or bottom of your grid container, these properties provide the flexibility and control you need to create a polished and professional design.

Sizing Units: Fr and Minmax

Credit: youtube.com, MinMax in CSS Grid — 3/3 Flexibility

The fr unit is a flexible sizing unit that represents a fraction of the available space within a grid container. It's a great way to create dynamic layouts that adapt to different screen sizes and resolutions.

You can use the fr unit in combination with other units like pixels or percentages to create even more dynamic layouts that cater to various devices and screen sizes. This is much more friendly than using percentage values, which can be too firm.

The minmax() function allows you to set a range of sizes for grid tracks, providing greater flexibility in your layout. By using the minmax() function in conjunction with the fr unit and other sizing units, you can create intricate grid layouts that cater to various screen sizes and resolutions.

Here's a quick rundown of the benefits of using the fr unit:

  • Creates flexible grid layouts that adapt to different screen sizes and resolutions.
  • Can be used in conjunction with other units like pixels or percentages.
  • Allows for dynamic layouts that cater to various devices and screen sizes.

The fr unit is a powerful tool for creating responsive and adaptive designs that cater to the needs of your users. By utilizing the fr unit in your grid layouts, you can easily create polished and professional designs that look great and function seamlessly across all devices.

Ann Predovic

Lead Writer

Ann Predovic is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for research, she has established herself as a go-to expert in various fields, including technology and software. Her writing career has taken her down a path of exploring complex topics, making them accessible to a broad audience.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.