CSS Grid is a sophisticated layout framework available in browser-native CSS. It is a two-dimensional framework, which means it can manage both columns and rows, as opposed to Flexbox, which is mostly one-dimensional. Grid Layout is used by applying CSS rules to a parent element (which becomes the Grid Container) and its children (which become Grid Items).
CSS Grid allows you to create complex layouts with less code and more flexibility than other layout methods. It's particularly useful for building responsive designs, as it allows you to rearrange and resize elements to fit different screen sizes. With CSS Grid, you can align items in a grid and manipulate the space along the grid in a way that was not possible before. It's a significant step forward for CSS layout and a tool that developers should have in their toolkit.
While other layout methods like Flexbox, Floats, or Positioning can be used to create page layouts, CSS Grid is the only tool that was specifically designed for this purpose.
Flexbox is great for one-dimensional layouts, where you're primarily concerned with laying out items in a single row or column. CSS Grid, on the other hand, is excellent for two-dimensional layouts, where you need to manage both rows and columns.
Floats were never intended for creating entire layouts, and while they can be used for this purpose, they often require hacks and additional markup. Positioning also wasn't designed for layout, and while it's useful for moving one or two elements around, it's not practical for an entire layout.
CSS Grid, with its ability to define both rows and columns and align items in both dimensions, provides a more robust and flexible solution for complex layouts.
When starting with CSS Grid, it's crucial to understand the basic terminology. The CSS Grid layout revolves around two key components: the grid container and the grid items.
The grid container is the parent element on which you apply display: grid or display: inline-grid. This turns the container into a grid layout context and its direct children become grid items.
Within the grid container, we have grid lines, which are horizontal (row lines) and vertical (column lines) lines that divide the grid. The space between two adjacent row lines is a grid row, and the space between two adjacent column lines is a grid column.
A grid cell is the space between two adjacent row lines and two adjacent column lines. A grid area, on the other hand, is made up of one or more grid cells.
Setting up a CSS Grid involves defining a grid container and placing grid items within it. The grid-template-columns and grid-template-rows properties are used to define the number and size of the columns and rows in the grid.
1 .grid-container { 2 display: grid; 3 grid-template-columns: auto auto auto; 4 grid-template-rows: auto auto; 5 } 6
As previously mentioned, the grid container is the parent element that holds the grid items. The grid items are the children of the grid container and are positioned within the grid container using various CSS Grid properties.
1 <div class="grid-container"> 2 <div class="grid-item">Item 1</div> 3 <div class="grid-item">Item 2</div> 4 <div class="grid-item">Item 3</div> 5 <div class="grid-item">Item 4</div> 6 </div> 7
In this example, the grid container has four grid items. The grid-template-columns and grid-template-rows properties can be used to arrange these items into a specific number of columns and rows.
CSS Grid introduces a powerful feature known as grid template areas. Grid template areas provide a visual way to arrange your layout, making your CSS more readable and easier to understand.
To define grid template areas, you use the grid-template-areas property on your grid container. This property takes a series of strings as a value, where each string represents a row in the grid. Within these strings, you can define names for the different areas of your grid.
Here's an example:
1 .grid-container { 2 display: grid; 3 grid-template-areas: 4 "header header header" 5 "sidebar main main" 6 "footer footer footer"; 7 } 8
In the example above, we've defined three grid template areas: header, main, and footer. We've also defined a sidebar area.
Each word in the string represents a grid cell, and multiple adjacent cells with the same name become a single grid area. This allows you to control the layout of your grid container in a way that's easy to visualize and understand.
To assign a grid item to one of these areas, you use the grid-area property:
1 .header { 2 grid-area: header; 3 } 4 5 .main { 6 grid-area: main; 7 } 8 9 .sidebar { 10 grid-area: sidebar; 11 } 12 13 .footer { 14 grid-area: footer; 15 } 16
Grid template areas are a powerful tool for creating complex layouts. By defining and naming areas of your grid, you can create a layout that's easy to understand and modify.
For example, you might want to create a layout with a header and footer that span the full width of the grid, a sidebar that takes up one column, and a main area that takes up the remaining space. With grid template areas, you can easily define this layout in a way that's visually intuitive.
1 <div class="grid-container"> 2 <div class="header">Header</div> 3 <div class="main">Main</div> 4 <div class="sidebar">Sidebar</div> 5 <div class="footer">Footer</div> 6 </div> 7
In this example, the layout of the grid container is defined by the grid-template-areas property, and each grid item is placed in the specified grid area using the grid-area property. This makes it easy to understand the layout at a glance and to change the layout if needed.
Grid lines are the dividing lines that make up the structure of the CSS grid. They can be horizontal (row lines) or vertical (column lines). Grid lines are numbered starting from 1, with the row lines numbered from top to bottom and the column lines numbered from left to right.
The space between two grid lines is known as a grid track. Grid lines are fundamental to the CSS grid as they are used to position grid items.
1 .grid-container { 2 display: grid; 3 grid-template-columns: 100px 100px 100px; 4 grid-template-rows: 100px 100px; 5 } 6
In this example, the grid container has three column lines and two-row lines, creating a grid of six cells.
Grid tracks are the space between two grid lines. This space can be horizontal (row track) or vertical (column track). You can think of grid tracks as the actual columns and rows of the grid.
Grid tracks are defined by the grid-template-columns and grid-template-rows properties of the grid container. The size of a grid track can be defined in various units, such as pixels (px), percentages (%), or fractions (fr).
1 .grid-container { 2 display: grid; 3 grid-template-columns: 1fr 2fr; 4 grid-template-rows: 100px 200px; 5 } 6
In this example, the grid container has two column tracks and two-row tracks of different sizes.
Grid items can be positioned in the grid using grid lines. The grid-column-start, grid-column-end, grid-row-start, and grid-row-end properties are used to define the grid lines where a grid item starts and ends.
1 .grid-item { 2 grid-column-start: 1; 3 grid-column-end: 3; 4 grid-row-start: 1; 5 grid-row-end: 2; 6 } 7
In this example, the grid item spans from the first to the third column line, and from the first to the second row line. This makes the grid item span two column tracks and one-row track.
A grid cell is the smallest unit of a CSS grid, defined by the space between two adjacent row lines and two adjacent column lines. You can think of a grid cell as a single cell in a table. Each grid item is placed in one or more grid cells.
1 .grid-container { 2 display: grid; 3 grid-template-columns: repeat(3, 1fr); 4 grid-template-rows: repeat(2, 1fr); 5 } 6
In this example, the grid container has six grid cells, arranged in three columns and two rows.
A grid area is a rectangular area of the grid, defined by the space between two row lines and two column lines. A grid area can contain one or more grid cells and can span multiple rows and columns. Grid areas are defined using the grid-template-areas property of the grid container and can be assigned to grid items using the grid-area property.
1 .grid-container { 2 display: grid; 3 grid-template-columns: repeat(3, 1fr); 4 grid-template-rows: repeat(2, 1fr); 5 grid-template-areas: 6 "header header header" 7 "main main sidebar"; 8 } 9 10 .header { 11 grid-area: header; 12 } 13 14 .main { 15 grid-area: main; 16 } 17 18 .sidebar { 19 grid-area: sidebar; 20 } 21
In this example, the grid container has three grid areas: header, main, and sidebar. The header area spans all three columns and one row, the main area spans two columns and one row, and the sidebar area spans one column and one row.
Grid items can be positioned in the grid using grid areas. The grid-area property is used to assign a grid item to a grid area.
1 <div class="grid-container"> 2 <div class="header">Header</div> 3 <div class="main">Main</div> 4 <div class="sidebar">Sidebar</div> 5 </div> 6
In this example, each grid item is assigned to a grid area using the grid-area property. This makes it easy to control the position and size of grid items in the grid.
Grid gaps, also known as gutters, are the spaces between grid items. They provide spacing between rows and columns in a grid layout. You can define grid gaps using the grid-gap property, or its individual grid-row-gap and grid-column-gap properties for vertical and horizontal spacing respectively.
1 .grid-container { 2 display: grid; 3 grid-template-columns: repeat(3, 1fr); 4 grid-template-rows: repeat(2, 1fr); 5 grid-gap: 20px; 6 } 7
In this example, the grid container has a grid gap of 20 pixels between both rows and columns.
Grid gaps are particularly useful in responsive design. They provide consistent spacing between grid items, regardless of the size of the grid container. This makes it easier to create layouts that adapt to different screen sizes.
You can adjust the size of grid gaps based on the size of the viewport using media queries. This allows you to increase the grid-gap on larger screens and decrease it on smaller screens, ensuring that your layout looks good on all devices.
1 @media (min-width: 600px) { 2 .grid-container { 3 grid-gap: 30px; 4 } 5 } 6 7 @media (max-width: 599px) { 8 .grid-container { 9 grid-gap: 10px; 10 } 11 } 12
In this example, the grid container has a grid gap of 30 pixels on screens that are at least 600 pixels wide and a grid gap of 10 pixels on screens that are less than 600 pixels wide.
CSS Grid is a powerful tool for creating responsive layouts. By using flexible units like fractions (fr) for your grid tracks, and auto-fill or auto-fit for your grid template, you can create a grid that adapts to different screen sizes without the need for media queries.
For example, using repeat(auto-fill, minmax(200px, 1fr)) for your grid-template-columns property will create as many 200-pixel columns as can fit in the container, and distribute the remaining space evenly among the columns.
1 .grid-container { 2 display: grid; 3 grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); 4 } 5
In this example, the grid container will automatically adjust the number of columns based on the width of the viewport, ensuring a responsive layout.
While CSS Grid allows for a lot of flexibility in creating responsive layouts, sometimes you might want more control over your layout at different screen sizes. This is where media queries come in.
Media queries allow you to apply different styles at different viewport sizes. You can use media queries with CSS Grid to adjust the number and size of your grid tracks, change the order of your grid items, or even redefine your grid template areas.
1 @media (min-width: 600px) { 2 .grid-container { 3 grid-template-columns: repeat(3, 1fr); 4 grid-template-areas: 5 "header header header" 6 "sidebar main main" 7 "footer footer footer"; 8 } 9 } 10 11 @media (max-width: 599px) { 12 .grid-container { 13 grid-template-columns: 1fr; 14 grid-template-areas: 15 "header" 16 "main" 17 "sidebar" 18 "footer"; 19 } 20 } 21
In this example, the grid container has a three-column layout on screens that are at least 600 pixels wide, and a single-column layout on screens that are less than 600 pixels wide. The order of the grid items is also adjusted based on the screen size.
CSS Grid provides a set of properties for aligning and justifying items within their grid area. These properties give you control over the positioning of items along both the row and column axis.
The alignment properties work on the block axis (vertical), while the justification properties work on the inline axis (horizontal). Together, they allow you to control the position of grid items along both axes.
The justify-items property aligns grid items along the row (inline) axis. It accepts values such as start, end, center, and stretch.
The align-items property aligns grid items along the column (block) axis. It accepts the same values as justify-items.
The place-items property is a shorthand for align-items and justify-items. It first sets the align-items value and then the justify-items value.
1 .grid-container { 2 display: grid; 3 justify-items: center; 4 align-items: start; 5 } 6
In this example, grid items are centered along the row axis and aligned to the start of the column axis.
The justify-content property aligns the grid along the row (inline) axis. It's useful when the total size of your grid is less than the size of your grid container. It accepts values such as start, end, center, stretch, space-around, space-between, and space-evenly.
The align-content property aligns the grid along the column (block) axis. It accepts the same values as justify-content.
The place-content property is a shorthand for align-content and justify-content. It first sets the align-content value and then the justify-content value.
1 .grid-container { 2 display: grid; 3 justify-content: space-between; 4 align-content: center; 5 } 6
In this example, the grid is spaced evenly along the row axis and centered along the column axis.
The grid-auto-flow property controls how grid items are automatically placed in a grid. By default, grid items are placed row by row. However, you can change this behavior to column by column using grid-auto-flow: column.
You can also use grid-auto-flow: dense to backfill grid cells with smaller items, which can help to minimize empty space in your grid.
1 .grid-container { 2 display: grid; 3 grid-auto-flow: column; 4 } 5
In this example, grid items are placed column by column instead of row by row.
CSS Grid allows you to nest grids within grids, which can be useful for creating more complex layouts. To create a nested grid, you simply define a grid within a grid item.
1 <div class="grid-container"> 2 <div class="grid-item"> 3 <div class="nested-grid"> 4 <div class="nested-item">1</div> 5 <div class="nested-item">2</div> 6 </div> 7 </div> 8 </div> 9
In this example, a nested grid is created within a grid item. The nested grid has its own grid items.
The minmax() function provides a way to specify a size range for your grid tracks. It takes two arguments, a minimum size, and a maximum size, and allows the track to flexibly resize within this range based on the size of the grid container and the size of the grid items.
1 .grid-container { 2 display: grid; 3 grid-template-columns: minmax(100px, 1fr) minmax(200px, 2fr); 4 } 5
In this example, the grid container has two column tracks. The first column track will be at least 100 pixels and at most 1 fraction of the remaining space, and the second column track will be at least 200 pixels and at most 2 fractions of the remaining space.
CSS Grid is a powerful tool that has revolutionized the way we approach web layout design. With its two-dimensional system, it provides a level of flexibility and control that was not possible with previous CSS layout techniques.
Whether you're building a simple photo gallery or a complex magazine-style layout, CSS Grid offers a solution. Its wide browser support and integration with other CSS features make it a reliable and versatile tool for modern web design.
However, like any tool, it's most effective when you understand how to use it. From basic concepts like grid lines and tracks to advanced techniques like grid template areas and auto-placement, there's a lot to learn about CSS Grid.
With the wealth of resources available, from online tutorials and courses to books and articles, there's never been a better time to dive in and learn CSS Grid. As you master this powerful tool, you'll be able to create more complex, responsive, and beautiful layouts with ease.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.