CSS Grid – Complete Guide In 10 Minutes
CSS Grid is an amazing way of creating website layouts for web development. It’s been around for a few years but it’s finally getting more popular with increasing proper browser support. Learning CSS Grid is not always easy, in this blog post, we’re going to take a look at CSS Grid and how it works. We’ll also look at some of the various properties and values that can be used with CSS Grid. With this 10-minute guide, you will be well on your way to understanding the basics.
Introduction to CSS Grid
As Layout systems have been constantly evolving, CSS has come a long way too. With the release of CSS Grid, we finally have a powerful tool for creating two-dimensional layouts. Nowadays designers and developers are using various layout systems like Flexbox or even pure CSS to create amazing responsive websites. But each of these methods has its own limitations when it comes to certain tasks. A CSS Grid can come in handy in this situation!
CSS Grid Architecture
There are two ways to use CSS grid layouts: implicit and explicit. With an implicit grid, you simply define the number of columns you want, and the browser will create the grid automatically. With an explicit grid, you define both the number of columns and the number of rows. This gives you more control over the layout, but it can be more complicated to set up.
It is a two-dimensional layout system. This means that it can handle both columns and rows. However, unlike traditional CSS layouts, which are mostly one-dimensional, CSS Grid is designed to work with both dimensions at the same time.
It is a container-based layout system. This means that it applies to elements that are children of a container element. The container element defines the grid and the child elements are placed into the grid cells.
It is a responsive layout system. This means that it can adapt to different screen sizes and resolutions. CSS Grid is also flexible, meaning it can be used for a wide variety of layouts, from simple to complex.
It is constructed on top of the existing CSS box model. This implies that it can be used with any existing CSS codebase. However, it also has some new features that make it unique.
Features that make CSS Grid unique
- Provides the ability to place items on the grid using line-based positioning. This makes it possible to create very complex layouts without having to use traditional floats or absolute positioning.
- Provides the ability to span columns and rows. In other words, you can have items that span multiple columns or rows.
- Provides the ability to place items at specific locations by using line numbers, and names, or by targeting a specific area of the grid. An algorithm is also included to control the placement of items not explicitly placed on the grid.
- Provides the ability to control how the items align once placed inside the grid area, and how the grid is aligned overall.
- Provides the ability to create a grid using fixed track sizes by using pixels – which sets the grid to the specified pixel which fits the layout you desire. You can also specify flexible track sizes by using percentage terms or the
fr
unit.
All these features when used properly sum up in creating responsive layouts that display well on any screen size.
Benefits of using CSS Grid
There are many benefits to using CSS Grid when building web pages. One of the main benefits is that it makes it much easier to create complex layouts. With CSS Grid, you can create layouts with multiple columns and rows, and you can easily control the size and position of each element on the page.
Another benefit of CSS Grid is that it helps to keep your code clean and organized. With traditional CSS, it can be easy for your code to become messy and difficult to read. However, with CSS Grid, all of your styles are applied to a grid, which makes your code much easier to read and understand.
Creating a Grid Layout
You need to start by defining a container element and assigning it a class name. This element will hold all of your content. Inside the container, you will define a series of child elements, each of which will occupy a certain area of the grid. You can control the size and position of these elements using a variety of properties. After that, add the following CSS code to your stylesheet:
.container {
display: grid;
}
Code language: CSS (css)
This will create a grid layout with one column containing all the child elements.
Grid Parent Properties
The grid parent element is the element on which the display: grid property is applied. It can be any type of element. There are the properties that can be applied to the grid parent element:
- grid-template-columns: This property defines the number of columns and the width of each column.
- grid-template-rows: This property defines the number of rows and the height of each row.
- grid-gap: This property defines the space between the columns and rows.
Grid Child Properties
The child properties in CSS Grid are used to define the size, position, and other aspects of grid items. These are a few main child properties that can be applied to the grid element:
- grid-column: This property is used to specify the size and position of a column in the grid. The syntax for this property is “
grid-column:
“. - grid-row: This property is used to specify the size and position of a row in the grid. The syntax for this property is “
grid-row:
“. - grid-area: This property is used to specify the size and position of an area in the grid. The syntax for this property is “
grid-area:
“.
Working with Columns and Rows
Grid lets you specify the number of columns and rows in your layout, and then place elements within those columns and rows.
You can control the width of columns and rows using the grid-template-columns
and grid-template-rows
properties. For example, you could use the following code to create a three-column layout:
.container {
display: grid;
grid-template-columns: 100px 200px 300px;
}
Code language: CSS (css)
You can also use percentages or fractions to control column widths. For example, the following code would create three columns, with the first column being twice as wide as the second column, and the third column being three times as wide:
.container {
display: grid;
grid-template-columns: 50% 33.33% 25%;
}
Code language: CSS (css)
Once you’ve specified the number of columns and rows in your layout, you can use the grid-column and grid-row properties to place elements within those columns and rows. For example, if you have a three-column layout, you could use the following code to place an element in the first column:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 columns, each 1/3 width of the container */
grid-column: 1;
}
Code language: CSS (css)
Conclusion
This concludes our CSS Grid guide. If you’re looking for a way to improve your CSS designing skills, then CSS Grid is definitely worth considering. We hope that you found this guide helpful and informative and that you now have a better understanding of how to use CSS Grid in your web development projects.
Thanks for reading! If you enjoyed this blog, please share it with your friends! If you have any questions or comments, please feel free to reach out to me. I’m always happy to help in any way I can.
Check out codedamn’s interactive course on CSS basics which includes an in-depth guide on CSS Grids.
Happy coding! 🙂
Sharing is caring
Did you like what Mridul Anand Singh wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: