CSS Grid

CSS Grid is a game-changer in the world of web development. It offers a powerful and intuitive way to create complex layouts with precision and ease. In this comprehensive guide, we’ll delve into the world of CSS Grid, covering its core concepts, properties, and usage, with code examples to illustrate each concept.

Understanding CSS Grid

CSS Grid is a two-dimensional layout system that allows you to design web layouts in both rows and columns. Unlike its predecessor, CSS Floats, Grid simplifies the layout process, making it more intuitive and responsive.

 

The Grid Container and Grid Items

In CSS Grid, you work with two fundamental components:

  • Grid Container: This is the parent element that holds the grid. You apply display: grid; to it.

  • Grid Items: These are the child elements of the grid container that are placed within the grid cells.

 
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
 

.item {
grid-column: 2 / 4;
grid-row: 1 / 3;
}

 

In this code, .container becomes a grid container with three equally sized columns. .item is a grid item that spans from the second to the fourth column and from the first to the third row.

 

Creating the Grid Structure

Grids are defined by setting the number and size of columns and rows within the grid container. The grid-template-columns and grid-template-rows properties help create the grid structure.

 
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: 100px 200px;
}

Here, the .container creates a grid with two columns, the first being one fraction unit wide and the second two fraction units wide, along with two rows, one 100 pixels tall and the other 200 pixels tall.

 

Placing Grid Items

You can place grid items within specific grid cells using the grid-column and grid-row properties.

 
.item {
grid-column: 1 / 3;
grid-row: 1 / 2;
}

In this example, the .item spans from the first to the third column and the first to the second row.

 

Grid Gaps

Grid gaps are the spaces between grid cells. You can control horizontal and vertical gaps using the grid-column-gap and grid-row-gap properties or the shorthand grid-gap.

 
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;
}

Here, the .container grid has a 10-pixel gap between both columns and rows.

 

Grid Auto Rows and Columns

CSS Grid allows for flexible layouts by using grid-auto-rows and grid-auto-columns. These properties define the size of rows and columns that aren’t explicitly defined in the grid template.

 
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-auto-rows: 100px;
}

In this code, rows are set to be 100 pixels tall, but the columns are still defined explicitly.

 

Grid Lines

Grid lines are the lines separating grid cells and can be named or numbered. You can use them to position grid items precisely.

 
.item {
grid-column: 1 / span 2;
grid-row: 2 / span 1;
}

This code positions .item to span two columns starting from the first column line and one row starting from the second row line.

 

Grid Template Areas

Grid template areas allow you to define layout patterns visually. You assign names to areas within the grid and place items accordingly.

 
.container {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}

.header {
grid-area: header;
}

Here, .container defines a grid with named areas, and .header is placed in the “header” area.

 

Responsive Grids

CSS Grid is excellent for responsive design. You can change the grid structure based on screen size using media queries.

 
.container {
display: grid;
grid-template-columns: 1fr 2fr;
}

@media (max-width: 600px) {
.container {
grid-template-columns: 1fr;
}
}

In this example, when the screen width is 600 pixels or less, the grid switches to a single column layout.

 

CSS Grid in Real Projects

CSS Grid is widely used in real-world projects to create flexible and responsive layouts. It simplifies the process of designing complex structures like magazine-style layouts, image galleries, and product grids.

CSS Grid is a revolutionary tool that empowers web designers and developers to craft intricate layouts with precision and ease. Whether you’re creating a simple grid or tackling complex responsive designs, CSS Grid is a valuable addition to your web development toolkit. Its flexibility, simplicity, and versatility make it a crucial technology for shaping the future of web layouts.

Build something ULTIMATE!

About Us

Learn about HTML, CSS, SASS, Javascript, jQuery, PHP, SQL, WordPress. From basics to tips and tricks.

Connect With us

© 2023 Ultimate WebDev

This website uses cookies to improve your experience. By browsing this website, you agree to our cookies. Accept Read More