Everything You Need To Know About CSS Grid Layout

To get the updated version, click here!

By using CSS Grid Layout you can create complex and responsive web layouts for all the devices with ease. In this post, I will guide you through the CSS grid and its properties.

Let’s get started!

What is CSS Grid?

CSS Grid is a powerful layout system that allows you to create complex and responsive web layouts with ease. By using the CSS grid, you can place and align elements precisely within a two-dimensional grid structure.

In simple terms, by using CSS Grid you can organize and align web page elements (like text, images, and buttons) in rows and columns, making it easier to create beautiful and responsive web layouts, that look great on all devices (mobile, tablet and desktop).

Some Important Grid Terminologies

Before diving deep into grid layout you should first know about some grid terminologies.

Grid Container

The element which has the display: grid  property on it, is called grid container.

Grid Item

The direct child element of a grid container is called the grid item.

For example:

CSS:

Here, the <div> element with the class ‘container’ is a grid container because it has the display: grid; property.

The second <div> element with the class ‘item’ is a grid item because it is the direct child of the grid container.

Now, will the <div> element with the class ‘box’ be considered a grid item or not?

The answer is no. It will not be considered a grid item because it is not a direct child element of the grid container.

For an element to be a grid item it must be the direct child of the grid container.

CSS Grid Layout

Grid Line

The lines that separate the rows and columns of the grid are grid lines.

CSS Grid Layout

Grid Cell

A grid cell is a single unit of the grid, which is created by the intersection of a row and a column.

CSS Grid Layout

Now you might be thinking what is the difference between a grid cell and a grid item?

Let me clear this.

A grid cell is a structural component, representing the intersection of rows and columns, while a grid item is a content or element that is positioned within one or more grid cells. Grid cells define the grid’s structure, and grid items are the elements you place within that structure.

Grid Track

The space between two adjacent grid lines is a grid track. It can be horizontal(row) or vertical(column).

CSS Grid Layout

Grid Area

A rectangular group of grid cells surrounded by four grid lines is called a grid area. A grid area can contain any number of grid cells.

CSS Grid Layout

Gutter

The space between rows and columns in a grid is called gutter.

CSS Grid Layout

The fr Unit

Fr is a fractional unit and is used to distribute available space proportionally among grid items. 1fr is for 1 part of the available space.

Grid Properties

We have the following properties for the grid container and grid items.

For all the properties I will use the following HTML and CSS structure.

HTML:

CSS:

Properties For Grid Container

The following properties are related to the grid container itself which allows you to define the structure and behavior of the grid layout.

display

To create a grid layout, you can apply the display: grid; property to an HTML element. This element becomes the grid container, and its direct children elements are treated as grid items.

grid-template-columns

This property is used to define the size and number of grid columns in the grid layout.

Example: 1

This will create two columns one of 100px width and the second of 200px width.

CSS Grid Layout

Example: 2

This will create three columns one of 100px width, the second of 150px width and the third column relative to the fraction of available space in the grid container.

CSS Grid Layout

Example: 3

If you want your grid columns to have the same width then you can use the value ‘auto’.

The auto keyword lets the browser determine the size of a track based on its content and available space.

This will create four columns of the same width.

CSS Grid Layout

Example: 4

Output:

CSS Grid Layout

grid-template-rows

This property is used to define the size and number of grid rows in the grid layout.

Example: 1

This will create the first row of 100px height, the second row of 200px height and the third row of 300px height.

CSS Grid Layout

Example: 2

This will create three rows one of 100px height, the second is relative to the fraction of available space in the grid container and the third is of 150px height.

This will be a grid container of 3 rows and 3 columns.

CSS Grid Layout

Example: 3

Let’s create a grid layout of 4 rows and 4 columns.

CSS Grid Layout

grid-template

This property is a shorthand property for defining both the grid-template-rows and grid-template-columns in a single declaration.

Basic syntax:

For example:

CSS Grid Layout

grid-auto-columns

This property is used to set a default size for grid columns. grid-template-columns property overrides grid-auto-columns property.

CSS Grid Layout

grid-auto-rows

This property is used to set a default size for grid rows. grid-template-rows property overrides grid-auto-rows property.

CSS Grid Layout

column-gap

This property is used to define the gap between the columns of the grid container.

CSS Grid Layout

row-gap

This property is used to define the gap between the rows of the grid container.

CSS Grid Layout

gap

This property is a shorthand for defining both row-gap and column-gap in a single declaration.

Syntax:

The values are space-separated.

Example:

CSS Grid Layout

justify-items

This property is used to control the horizontal alignment (along the row axis) of grid items.

It has the following values:

  • start
  • end
  • center
  • stretch

start

This aligns the items to the start of the grid container.

CSS Grid Layout

end

This aligns the items to the end of the grid container.

CSS Grid Layout

center

This centers the items horizontally within the grid container.

CSS Grid Layout

stretch

This stretches the items to fill the entire width of the grid container. This is the default value.

CSS Grid Layout

align-items

This property is used to control the vertical alignment (along the column axis) of grid items.

It has the following values:

  • start
  • end
  • center
  • stretch

start

This aligns the items to the top of the grid container.

CSS Grid Layout

end

This aligns the items to the bottom of the grid container.

CSS Grid Layout

center

This centers the items vertically within the grid container.

CSS Grid Layout

stretch

This stretches the items to fill the entire height of the grid container. This is the default value.

CSS Grid Layout

place-items

This property is a shorthand for setting both align-items and justify-items in a single declaration.

Basic syntax:

The values are space-separated.

Example: 1

CSS Grid Layout

Example: 2

CSS Grid Layout

Example: 3

CSS Grid Layout

Example: 4

CSS Grid Layout

You can try different combinations as well with different values of align-items and justify-items.

justify-content

This property is used to control the horizontal alignment (along the row axis) of all the grid content if the total grid size is smaller than the container.

It has the following values:

  • start
  • end
  • center
  • space-around
  • space-between
  • space-evenly

start

Items are aligned to the start of the container.

CSS Grid Layout

end

Items are aligned to the end of the container.

CSS Grid Layout

center

Items are aligned to the center of the container.

CSS Grid Layout

space-between

Items are evenly distributed with equal space between them and the first item at the start and the last item at the end.

CSS Grid Layout

space-around

Items are evenly distributed with equal space around them.

CSS Grid Layout

space-evenly

Items are evenly distributed with equal space around and between them.

CSS Grid Layout

align-content

This property is used to control the vertical alignment (along the column axis) of all the grid content if the total grid size is smaller than the container.

It has the following values:

  • start
  • end
  • center
  • space-around
  • space-between
  • space-evenly

start

Rows are aligned to the start of the container.

CSS Grid Layout

end

Rows are aligned to the end of the container.

CSS Grid Layout

center

Rows are aligned to the center of the container.

CSS Grid Layout

space-between

Rows are evenly distributed with equal space between them and the first row at the start and the last row at the end.

CSS Grid Layout

space-around

Rows are evenly distributed with equal space around them.

CSS Grid Layout

space-evenly

Rows are evenly distributed with equal space around and between them.

CSS Grid Layout

place-content

This property is a shorthand for setting both align-content and justify-content in a single declaration.

Basic syntax:

The values are space-separated.

Example: 1

CSS Grid Layout

Example: 2

CSS Grid Layout

You can try different combinations as well with different values of align-content and justify-content.

grid-auto-flow

This property is used to determine how additional grid items will be placed within the grid container when they are not explicitly positioned.

But, what does it mean by explicitly positioned?

Explicitly positioned items are the items that you define using the grid-template-columns or grid-template-rows property.

We have the following values for the grid-auto-flow property:

grid-auto-flow: column;

This value arranges grid items in columns, occupying each column before proceeding to the next one.

You can understand it more clearly by adding and removing the grid-auto-flow property.

grid-auto-flow: row; (default)

This value arranges grid items in rows, occupying each row before proceeding to the next one.

grid-auto-flow: column dense;

This value arranges grid items in columns, but it can leave empty spaces within the grid when smaller items are encountered subsequently.

grid-auto-flow: row dense;

This value arranges grid items in rows, but it can leave empty spaces within the grid when smaller items are encountered subsequently.

Properties For Grid Items

The following properties are used to control the placement and alignment of individual grid items within a grid container.

justify-self

This property is used to control the horizontal alignment of a specific grid item within its grid cell. It overrides the horizontal alignment set for the entire grid container.

It has the following values:

  • start
  • end
  • center
  • stretch

start

This aligns the item to the start of its grid cell.

CSS Grid Layout

end

This aligns the item to the end of its grid cell.

CSS Grid Layout

center

This centers the item horizontally within its grid cell.

CSS Grid Layout

stretch

This stretches the item to fill the entire width of its grid cell.

CSS Grid Layout

align-self

This property is used to control the vertical alignment of a specific grid item within its grid cell. It overrides the vertical alignment set for the entire grid container.

It has the following values:

  • start
  • end
  • center
  • stretch

start

This aligns the item to the top of its grid cell.

CSS Grid Layout

end

This aligns the item to the bottom of its grid cell.

CSS Grid Layout

center

This centers the item vertically within its grid cell.

CSS Grid Layout

stretch

This stretches the item to fill the entire height of its grid cell.

CSS Grid Layout

place-self

This property is a shorthand for setting both align-self and justify-self in a single declaration.

Basic syntax:

The values are space-separated.

Example: 1

CSS Grid Layout

Example: 2

CSS Grid Layout

You can try different combinations as well with different values of align-self and justify-self.

grid-row-start

This property is used to define where a grid item starts in a row.

Here, the third grid item will start at the row grid line 3.

CSS Grid Layout

grid-column-start

This property is used to define where a grid item starts in a column.

Here, the third grid item will start at column grid line 2.

CSS Grid Layout

grid-row-end

This property is used to define where a grid item ends in a row.

Here, the third grid item will end at the row grid line 2.

CSS Grid Layout

grid-column-end

This property is used to define where a grid item ends in a column.

Here, the third grid item will end at column grid line 3.

CSS Grid Layout

Examples for grid-row-startgrid-column-startgrid-row-end and grid-column-end

Example: 1

CSS Grid Layout

Example: 2

CSS Grid Layout

grid-row

This property is a shorthand for setting both grid-row-start and grid-row-end in a single declaration.

Basic syntax:

Example:

Here, the third item starts at row grid line 3 and ends at row grid line 4.

CSS Grid Layout

grid-column

This property is a shorthand for setting both grid-column-start and grid-column-end in a single declaration.

Basic syntax:

Example:

Here, the third item starts at column grid line 2 and ends at column grid line 4.

CSS Grid Layout

grid-area

This property is a shorthand property for grid-row-startgrid-column-startgrid-row-end and grid-column-end.

This property can also be used to assign a name to a grid item. Then these named grid items can be referenced to the grid-template-areas property of the grid container.

Basic syntax:

Example: 1

CSS Grid Layout

Example: 2

grid-template-areas

This property defines the overall structure of the grid layout by specifying the arrangement of named grid areas.

Example:

HTML:

CSS:

In this layout, we’ve organized our grid into three rows and two columns.

Using the grid-template-areas property, we’ve given meaningful names to each grid cell, defining the layout’s structure. The top row consists of two cells both named “header,” the second row contains the “sidebar” and “main” areas, and the third row repeats the “footer” area twice.

To position each grid item within this structure, we’ve used the grid-area property. This establishes a connection between the grid items and the named areas we’ve defined in grid-template-areas.

CSS Grid Layout

Functions

In CSS Grid, there are several built-in functions that you can use to define the size of grid tracks (rows and columns). These functions help your grid to adjust in different screen sizes, making your layouts more responsive.

Following are some commonly used functions in CSS Grid:

repeat() Function

This function lets you use the same pattern of track sizes for both rows and columns in a grid.

You can use the repeat() function as a value for the grid-template-columns or grid-template-rows properties.

Syntax:

The number of repetitions can be a number, auto-fill or auto-fit.

The size can be a length (px, em, rem, etc.), a percentage, a flex value, or one of the keyword values max-content, min-content, or auto.

auto-fill: It creates as many tracks as needed to fill the grid container, even if some are empty. It doesn’t collapse empty tracks, so they’re visible but empty.

auto-fit: Similar to auto-fill, it makes as many tracks as needed, but here’s the difference: If some tracks are empty, auto-fit collapses them to take up zero space, so you won’t see gaps in your layout.

Example: 1

Here, this will create 3 columns each of width 1fr and 4 rows each of height 200px.

Example: 2

minmax() Function

This function sets a range of sizes for grid tracks, so they can be both small and big within certain limits. That means it sets a minimum and maximum size.

You can use the minmax() function as a value for grid-template-columns ,grid-template-rows ,grid-auto-columns and grid-auto-rows properties.

Syntax:

Each parameter can be a length (px, em, rem, etc.), a percentage, a flex value, or one of the keyword values max-content, min-content, or auto.

If the maximum size is less than the minimum size, then the maximum will be ignored and minmax(min, max) will be treated as minimum.

Example:

Here, this will create 3 columns. The first column will remain between 100px and 200px when the screen shrinks and the remaining two columns will adjust according to the available remaining space.

fit-content() Function

This function allows you to specify the size of a grid item based on its content.

Syntax:

The size can be a length (px, em, rem, etc.), a percentage, a flex value, or one of the keyword values max-content, min-content, or auto.

Example:

Here, the first column will be sized to fit its content up to a maximum width of 100 pixels, and the second column will take up the remaining available space.

Conclusion

CSS Grid is a powerful layout system that simplifies the creation of complex and responsive web layouts. By utilizing the properties and functions associated with CSS Grid, you can easily organize and align elements within a two-dimensional grid structure.

Here are the key takeaways from this post:

  1. CSS Grid Basics: CSS Grid involves a grid container (created with display: grid;) and grid items (direct children of the container).
  2. Grid Structure: Understanding terms like grid lines, grid cells, grid tracks, grid areas, and gutters is crucial for building grid layouts.
  3. Fractional Units (fr): The fr unit distributes available space proportionally among grid items.
  4. Grid Properties: Properties like grid-template-columns and grid-template-rows define the grid structure, while grid-template is a shorthand for both.
  5. Column and Row Gaps: Use column-gaprow-gap, or gap (shorthand) to control the spacing between columns and rows.
  6. Alignment: Properties like justify-itemsalign-items, and place-items control the alignment of grid items. justify-contentalign-content, and place-content control alignment of the grid content. justify-selfalign-self, and place-self control alignment of the grid items within their grid cells.
  7. Grid Auto Flow: You can control how additional grid items are placed with grid-auto-flow.
  8. Item Placement: Individual grid items can be positioned within the grid using grid-row-startgrid-column-startgrid-row-endgrid-column-endgrid-rowgrid-columngrid-area, and grid-template-areas.
  9. Functions: Functions like repeat()minmax(), and fit-content() helps define responsive grid track sizes based on content and available space.

By mastering CSS Grid and its associated properties, you can design web layouts that are not only visually appealing but also responsive to various screen sizes, enhancing the overall user experience.

Thanks for reading.

For more content like this click here.

Check out CSS Scan, a browser extension that lets you extract the code for any CSS element of all the websites across the internet. Click here to get a 25% discount on CSS Scan.

Keep Coding!!

12 thoughts on “Everything You Need To Know About CSS Grid Layout”

  1. 🚀 Wow, blog ini seperti petualangan fantastis meluncur ke alam semesta dari keajaiban! 💫 Konten yang menegangkan di sini adalah perjalanan rollercoaster yang mendebarkan bagi pikiran, memicu kegembiraan setiap saat. 💫 Baik itu inspirasi, blog ini adalah harta karun wawasan yang menarik! #KemungkinanTanpaBatas Terjun ke dalam petualangan mendebarkan ini dari penemuan dan biarkan pikiran Anda terbang! 🚀 Jangan hanya mengeksplorasi, rasakan kegembiraan ini! #BahanBakarPikiran Pikiran Anda akan bersyukur untuk perjalanan menyenangkan ini melalui alam keajaiban yang tak berujung! ✨

    Reply
  2. I have been browsing on-line greater than three hours today, yet I never found any fascinating
    article like yours. It is lovely worth enough for me.
    Personally, if all site owners and bloggers made excellent content as you did,
    the net will probably be a lot more useful than ever before.

    Reply
  3. Simply wish to say your article is as astounding. The clearness in your post is simply great and
    i can assume you are an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming
    post. Thanks a million and please continue the rewarding work.

    Reply
  4. Woah! I’m really loving the template/theme of this blog. It’s simple,
    yet effective. A lot of times it’s very hard to get that
    “perfect balance” between usability and visual appeal.
    I must say that you’ve done a amazing job with this.
    Additionally, the blog loads super quick for me on Chrome.
    Outstanding Blog!

    Reply
  5. This is very interesting, You’re an excessively skilled
    blogger. I’ve joined your feed and sit up for in quest of more of your wonderful post.

    Also, I have shared your site in my social networks

    Reply

Leave a Comment