The CSS Grid Layout Module has revolutionized the way websites are built. It offers tools that allow for advanced layouts without the tricky hacks and inventive solutions of the past.
In this introduction to Grid, we’ll walk through the basics of how Grid layout works, and we’ll look at lots of simple examples of how to use it in practice.
Key Takeaways
-
Introduction to CSS Grid Layout: The article provides a foundational understanding of CSS Grid as a powerful layout system in CSS, explaining how to transform elements into a grid framework of rows and columns for structured content placement. It covers the basics of turning an element into a grid container and its direct children into grid items.
-
Grid Structure and Item Placement: Essential features of CSS Grid are explored, including defining grid structures using properties like grid-template-columns, grid-template-rows, and grid-template-areas. The tutorial demonstrates spacing grid items with the gap property and organizing them into specific rows and columns for a precise layout.
-
Responsive Design with CSS Grid: The adaptability of CSS Grid for responsive web design is highlighted, showcasing how media queries can be used to alter grid layouts for different screen sizes. Advanced techniques such as overlapping grid items and creating responsive layouts without media queries are also discussed, emphasizing the versatility of CSS Grid in modern web design.
Getting Started with Grid Layout
A grid is a framework of columns and rows into which we can place content. (It’s a bit like a table layout, but on steroids!)
Getting started with Grid is as simple as doing this:
<span><span>.container</span> { </span> <span>display: grid; </span><span>} </span>
Now, all of the direct children of the .container element will be “grid items”. To start with, they’ll just appear as a bunch of rows in a single column, as shown in the demo below.
See the Pen
CSS Grid Basics: display: grid by SitePoint (@SitePoint)
on CodePen.
In the example above, we have a
<span><span><span><div</span> class<span>="container"</span>></span> </span> <span><span><span><header</span>></span>header<span><span></header</span>></span> </span> <span><span><span><aside</span>></span>aside<span><span></aside</span>></span> </span> <span><span><span><main</span>></span>main<span><span></main</span>></span> </span> <span><span><span><footer</span>></span>footer<span><span></footer</span>></span> </span><span><span><span></div</span>></span> </span>
So far, we haven’t achieved much, as we would get the same result without display: grid.
Further reading:
- In the demo above, the container is centered in the viewport. Read more about centering elements with Grid.
Setting a Gap Between Grid Items
Let’s first space our divs apart a bit with the gap property:
<span><span>.container</span> { </span> <span>display: grid; </span><span>} </span>
See the Pen
CSS Grid Basics: gap by SitePoint (@SitePoint)
on CodePen.
The gap property inserts space between the elements vertically and horizontally, as we’ll see shortly. (We can set different horizontal and vertical gaps if we need to.)
Further reading:
- Read more about the gap property.
Setting Up Grid Columns
Currently, we have an “implicit” grid — meaning that the browser is just figuring out a grid for itself, as we haven’t specified any rows or columns yet. By default, we just get one column and four rows — one for each grid item. Let’s now specify some columns:
<span><span><span><div</span> class<span>="container"</span>></span> </span> <span><span><span><header</span>></span>header<span><span></header</span>></span> </span> <span><span><span><aside</span>></span>aside<span><span></aside</span>></span> </span> <span><span><span><main</span>></span>main<span><span></main</span>></span> </span> <span><span><span><footer</span>></span>footer<span><span></footer</span>></span> </span><span><span><span></div</span>></span> </span>
With grid-template-columns, we’re specifying that we want four columns each with a width of 1fr, or one fraction of the available space. (Instead of 1fr 1fr 1fr 1fr we could write repeat(4, 1fr) using the very handy repeat() function.)
Now our grid items are laid out in one row, as shown below.
See the Pen
CSS Grid Basics: grid-template-columns by SitePoint (@SitePoint)
on CodePen.
Further reading:
- Further information on grid-template-columns.
- Learn more about the flexible length (fr) unit.
- How to use Grid’s repeat() function.
Organizing Grid Items into Rows and Columns
Now let’s organize our grid items into rows and columns, as we might see them on a standard web page layout.
Firstly, we’ll specify three rows with the grid-template-rows property:
<span><span>.container</span> { </span> <span>display: grid; </span> <span>gap: 10px; </span><span>} </span>
If we add that line to the Pen above, not much will happen yet, as we haven’t specified how we want our grid items to fit into these rows and columns. (Again note that auto auto auto could be written as repeat(3, auto) using the repeat() function.)
Further reading:
- Further information on grid-template-rows.
- How to use Grid’s repeat() function.
Placing Grid Items on the Grid
Our browser’s developer tools come in very handy for understanding CSS Grid layout. If we inspect our code so far, we can see the horizontal and vertical grid lines that define our grid, as pictured below.
There are five vertical grid lines and four horizontal grid lines, all of them numbered. We can use these grid lines to specify how we want our grid items laid out.
Let’s first set the
<span><span>.container</span> { </span> <span>display: grid; </span><span>} </span>
This tells the
Let’s do something similar to the
<span><span><span><div</span> class<span>="container"</span>></span> </span> <span><span><span><header</span>></span>header<span><span></header</span>></span> </span> <span><span><span><aside</span>></span>aside<span><span></aside</span>></span> </span> <span><span><span><main</span>></span>main<span><span></main</span>></span> </span> <span><span><span><footer</span>></span>footer<span><span></footer</span>></span> </span><span><span><span></div</span>></span> </span>
The only difference here is that we’ve set the
Now let’s position the
<span><span>.container</span> { </span> <span>display: grid; </span> <span>gap: 10px; </span><span>} </span>
The result is shown in the Pen below.
See the Pen
CSS Grid Basics: Placing items with numbered lines by SitePoint (@SitePoint)
on CodePen.
We now have a flexible and responsive layout without hacky floats, overflows and other nightmares of the past. If we add content to our grid items, they’ll expand to contain that content, and the side-by-side columns will always have equal height. (For those working with CSS in the noughties, achieving equal-height columns was a nightmare!)
Useful things to know about numbered grid lines
If you look again at the image above, you’ll see that, along the bottom, the vertical lines are also named with negative numbers. Each grid line has a positive and a negative number. This has lots of uses, such as when there are lots of grid lines and we don’t necessarily know how many there will be. We could set our
Grid also has a span keyword, which we can use to tell an element to span a number of rows or columns. Another option for our
Try out these variations in the Pen above.
Further reading:
- Learn more about grid lines.
- The direction in which grid lines are numbered is affected by our layout’s writing mode.
- Learn more about grid-column.
- Read about the span keyword in the Grid specification.
Placing Grid Items Using Named Grid Lines
We’ve seen how to organize elements on the grid using numbered grid lines. But we can also give names to some or all of our grid lines and reference those instead — which can be a bit more intuitive and save us from counting grid lines.
Let’s update our layout to include some named lines:
<span><span>.container</span> { </span> <span>display: grid; </span><span>} </span>
In the code above, we’ve named just three vertical grid lines. The names go in square brackets beside our column widths, representing our column lines.
We can now place some of our elements on the grid like so:
<span><span><span><div</span> class<span>="container"</span>></span> </span> <span><span><span><header</span>></span>header<span><span></header</span>></span> </span> <span><span><span><aside</span>></span>aside<span><span></aside</span>></span> </span> <span><span><span><main</span>></span>main<span><span></main</span>></span> </span> <span><span><span><footer</span>></span>footer<span><span></footer</span>></span> </span><span><span><span></div</span>></span> </span>
We can see this code in practice in the demo below.
See the Pen
CSS Grid Basics: Placing items with named lines by SitePoint (@SitePoint)
on CodePen.
Further reading:
- Learn more about named grid lines.
- Read about named lines in the Grid specification.
Placing Grid Items Using Named Grid Areas
One of the most interesting and “designer-friendly” features of Grid layout is the ability to name grid areas — that is, one or more cells in the grid — in a simple and intuitive way, and then use those names to lay out our grid items. It works like this:
<span><span>.container</span> { </span> <span>display: grid; </span> <span>gap: 10px; </span><span>} </span>
Using grid-template-areas, we’ve drawn up a simple text grid specifying how we want elements laid out. Now we just need to apply these area names to our elements:
<span><span>.container</span> { </span> <span>display: grid; </span> <span>gap: 10px; </span> <span>grid-template-columns: 1fr 1fr 1fr 1fr; </span><span>} </span>
So, the
We can see this in action in the Pen below.
See the Pen
CSS Grid Basics: Placing items using names areas by SitePoint (@SitePoint)
on CodePen.
This code is a lot simpler than what we had earlier — whether using numbered or named lines. Using named grid areas like this is almost embarrassingly simple — like using a WYSIWYG editor instead of writing real code. But be assured, it’s not cheating! It’s just super cool.
Using line numbers and named lines with grid areas
It’s worth noting that we can also use line numbers and/or named lines to define grid areas. For example, instead of using the grid-template-areas property, we could just do something like this:
<span><span>.container</span> { </span> <span>display: grid; </span><span>} </span>
The pattern is row-start / column-start / row-end / column-end. You can check out a demo of this on CodePen. Personally, I find it really hard to remember this pattern of rows and columns, but the great thing about Grid is that there are lots of ways to do the same thing.
Changing the layout of grid items
In days of old, if we decided at some point to change the layout of our page elements, it would likely have led to a lot of code refactoring. For example, what if we wanted to extend the
<span><span><span><div</span> class<span>="container"</span>></span> </span> <span><span><span><header</span>></span>header<span><span></header</span>></span> </span> <span><span><span><aside</span>></span>aside<span><span></aside</span>></span> </span> <span><span><span><main</span>></span>main<span><span></main</span>></span> </span> <span><span><span><footer</span>></span>footer<span><span></footer</span>></span> </span><span><span><span></div</span>></span> </span>
We’ve just removed a grid cell from footer and assigned it to aside, leading to what we see in the Pen below.
See the Pen
CSS Grid Basics: Placing items using named areas 2 by SitePoint (@SitePoint)
on CodePen.
We might even decide that we want an empty cell somewhere. We do that by just using one or more periods, like so:
<span><span>.container</span> { </span> <span>display: grid; </span> <span>gap: 10px; </span><span>} </span>
See if you can predict the outcome of this, and then check out this CodePen demo.
Further reading:
- Learn more about grid-template-areas.
- Learn more about the grid-area property.
Using Media Queries with Grid Layout
We often want a different layout on small screens — such as stacking our grid elements in a single column. An easy way to do this is to reorder our grid areas via a media query:
<span><span>.container</span> { </span> <span>display: grid; </span> <span>gap: 10px; </span> <span>grid-template-columns: 1fr 1fr 1fr 1fr; </span><span>} </span>
We’ve now specified just a single column, and set the order of the elements in that column.
See the Pen
CSS Grid Basics: Using media queries by SitePoint (@SitePoint)
on CodePen.
Press the 0.5x button at the bottom the Pen above to see how the layout responds (or view the Pen on CodePen and widen and narrow the viewport).
Changing the display order of grid items
We’re at a good point now to see how easy it is to change the display order of elements in Grid layout. In the example above, our source order is
We can reorder grid elements even if not using named elements. By default, grid items are placed according to their HTML source order. They also have a default order of 0. We can use this order property to change the visual arrangement of elements. The lower the order value, the sooner an element appears. Even negative integers can be used, so if our
To order our elements as shown above, we could set the order value of
A word of caution, though: reordered elements can be a nightmare for accessibility, with the focus jumping around the screen unpredictably, so use it sparingly.
Further reading:
- The official specification for the order property.
- The order property explained on MDN, including a section on accessibility concerns.
Responsive Grid Layout without Media Queries
We saw above that we can make our layout responsive to different screen sizes. Firstly, by setting column widths to flexible units like fr, the layout can grow and shrink as needed. Secondly, we can use media queries to reorganize the layout at certain breakpoints.
Grid layout has tools that allow for layout reflow without the use of media queries. However, we can’t achieve this with the layout we’ve been working with above. It only works for simpler layouts where each column shares the same width.
Consider the following HTML:
<span><span>.container</span> { </span> <span>display: grid; </span><span>} </span>
Let’s sit those divs side-by-side on wide screens, and stack on small screens:
<span><span><span><div</span> class<span>="container"</span>></span> </span> <span><span><span><header</span>></span>header<span><span></header</span>></span> </span> <span><span><span><aside</span>></span>aside<span><span></aside</span>></span> </span> <span><span><span><main</span>></span>main<span><span></main</span>></span> </span> <span><span><span><footer</span>></span>footer<span><span></footer</span>></span> </span><span><span><span></div</span>></span> </span>
You can see the result in the Pen below.
See the Pen
CSS Grid Basics: responsive grid without media queries by SitePoint (@SitePoint)
on CodePen.
(Again, press the 0.5x button at the bottom the Pen above to see how the layout responds.)
That code is a bit more advanced, and we explain it in detail in How to Use the CSS Grid repeat() Function. The main purpose of showing it here is to give a sense of what’s possible with Grid layout.
Further reading:
- Read more about the auto-fit keyword.
- Read more about using the minmax() function with the min() function.
Overlapping Elements in a Grid
Once we’ve created a grid layout, we can do more than just allocate each grid item to its own separate grid area. We can easily set grid items to share the same grid areas, in part or in full. This allows us to create beautiful, creative layouts — with overlapping elements, and without any tricky code.
Let’s create a simple grid containing an image and some text that partly covers the image. Here’s the HTML:
<span><span>.container</span> { </span> <span>display: grid; </span><span>} </span>
Now we’ll assign some rows and columns that are partly shared between the div and the image:
<span><span><span><div</span> class<span>="container"</span>></span> </span> <span><span><span><header</span>></span>header<span><span></header</span>></span> </span> <span><span><span><aside</span>></span>aside<span><span></aside</span>></span> </span> <span><span><span><main</span>></span>main<span><span></main</span>></span> </span> <span><span><span><footer</span>></span>footer<span><span></footer</span>></span> </span><span><span><span></div</span>></span> </span>
The result is shown in the following CodePen demo.
See the Pen
CSS Grid Basics: Layered grid elements by SitePoint (@SitePoint)
on CodePen.
The div appears above the image simply because it comes after the image in the source order. We can change which element appears over the other by applying z-index. For example, try setting a z-index of 2 to the image in the Pen above; it will now cover part of the div.
Further reading:
- To understand the positioning of the image in the demo above, check out How to Use CSS object-fit and object-position.
- Learn more about the z-index property.
Wrapping Up
This article is intended just as a basic introduction to what CSS Grid layout can do. The hope is that it will provide a springboard for further learning and discovery. And there’s a huge amount you can learn about Grid.
Grid vs Flexbox
An eternal question is whether we should use Grid or Flexbox. It’s true that there is some overlap between what these two layout tools can do. Often, where there is overlap, it’s worth doing a few tests to see which has the better toolkit in each particular scenario.
As a general rule, though, remember this:
- Flexbox is mainly designed for laying out elements in a single direction (even if those elements wrap across lines).
- Grid is designed for laying out elements in two directions, so that they’re aligned both horizontally and vertically.
For this reason, Grid is generally a better option for whole-page layouts, while Flexbox is better for laying out things like menus.
For a more in-depth comparison of Grid and Flexbox, check out Flexbox or CSS Grid? How to Make Layout Decisions that Make Sense.
Browser support for Grid
When we first published this article — back in 2016 — Grid was fairly new to browsers, and support wasn’t universal. Nowadays, all the major browsers support Grid. There will still be a few older browsers floating around that don’t support it, but in many cases, those browsers will still display the content well enough. One nice catch-all approach is to start with a single-column layout for mobile devices that can serve as a fallback for browsers that don’t support Grid layout. The Grid styles can be added in via media queries for browsers that do support them — to be displayed on wider screens.
You can check out the latest browser support for Grid on caniuse.
Learning resources for Grid
Finally, let’s end with some further learning resources. Lots of amazing people have provided tutorials, videos, books and more on Grid. Here are just a few:
- CSS Master, 3rd Edition, by Tiffany Brown, is a great introduction to CSS, with in-depth guidance on how to use Grid and other layout methods.
- The MDN Grid reference.
- The Grid by Example site of Rachel Andrew. (Actually, Rachel Andrew has lots of incredible articles, tutorials, videos and even a book on Grid layout, and is a foremost expert on it. Googling “Rachel Andrew Grid” will bring up tons of great material for learning Grid.)
- The Layout Land YouTube series by Jen Simmons. (Jen is another name worth googling.)
- Kevin Powell presents lots of fantastic Grid tutorials on YouTube that are worth checking out.
- CSS-Tricks provides A Complete Guide to CSS Grid, which is a really handy resource.
FAQs About CSS Grid Layout
What is CSS Grid Layout?
CSS Grid Layout is a layout system in CSS that allows you to create complex two-dimensional grid-based layouts for web pages. It provides a more flexible and efficient way to design and position elements in a grid.
How do I enable CSS Grid on my webpage?
To enable CSS Grid, you can use the display: grid; property in your CSS code. This will make the selected container a grid container.
What are the main components of CSS Grid?
The main components of CSS Grid are the grid container and grid items. The container is defined using the display: grid; property, and the items are the elements placed inside the container.
How do I define rows and columns in a CSS Grid?
You can define rows and columns by setting properties like grid-template-rows, grid-template-columns, or using the shorthand property grid-template.
Can I have different column widths and row heights in a CSS Grid?
Yes, you can specify different column widths and row heights by using different values in your grid-template definition, such as fixed lengths, percentages, or the fr unit for flexible sizing.
Can I nest grids within grids?
Yes, you can nest grids within grids, creating complex layouts by making a grid item itself a grid container.
Is CSS Grid responsive? Can I make grids adapt to different screen sizes?
Yes, CSS Grid can be responsive. You can use media queries, percentages, or relative units to adapt your grid layout to different screen sizes and devices.
The above is the detailed content of A Beginner's Guide to CSS Grid Layout. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

The choice of CSS units depends on design requirements and responsive requirements. 1.px is used for fixed size, suitable for precise control but lack of elasticity; 2.em is a relative unit, which is easily caused by the influence of the parent element, while rem is more stable based on the root element and is suitable for global scaling; 3.vw/vh is based on the viewport size, suitable for responsive design, but attention should be paid to the performance under extreme screens; 4. When choosing, it should be determined based on whether responsive adjustments, element hierarchy relationships and viewport dependence. Reasonable use can improve layout flexibility and maintenance.
