Loading...
Loading...

CSS Grid

CSS Grid is a powerful layout system that provides a two-dimensional grid-based layout, allowing developers to create complex responsive web designs with ease. This tutorial will guide you through the basics of CSS Grid and how to implement it in your projects.

1. What is CSS Grid?

CSS Grid Layout is a layout method that allows you to arrange elements in rows and columns. It offers a more efficient way to design responsive layouts compared to traditional CSS methods, like flexbox and floats.

2. Creating a Basic Grid

CSS Grid Layout offers a robust two-dimensional layout system that allows you to design intricate grid-based layouts. With it, you can specify both rows and columns, as well as position items anywhere within the grid.


.container {
    display: grid;                     /* Activate grid layout */
    grid-template-columns:1fr, 1fr, 1fr; /* Create three equal columns */
    grid-gap: 10px;                   /* Space between grid items */
}

.item {
    height: 200px;                    /* Fixed height for each grid item */
    background-color: lightblue;      /* Item background color */
}

<div class="container">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
</div>
Preview :
Item 1
Item 2
Item 3

This section illustrates a CSS Grid Layout. The .container uses display: grid to form a grid container. The property grid-template-columns creates three equal-width columns using repeat(3, 1fr), ensuring an even distribution of space. The grid-gap property adds a 10px margin between grid items, while each .item maintains a consistent height and background color.

Version Comparison:

With repeat(): repeat(3, 1fr) — shorter and ideal for many columns.
Without repeat(): 1fr 1fr 1fr — same result but more typing.
Both mean: “Make 3 equal columns.”

Important Grid Properties Explained

display: grid; — Enables the grid layout on the container.

grid-template-columns — Defines the number of columns and their widths (e.g. 200px 1fr 1fr;).

grid-template-rows — Defines the number of rows and their heights (e.g. 100px auto 50px;).

gap or grid-gap — Sets space between columns and rows (e.g. gap: 10px;).

grid-column — Determines how many columns an element spans (e.g. grid-column: 1 / 3;).

grid-row — Determines how many rows an element spans (e.g. grid-row: 1 / 2;).

grid-template-areas — Names layout areas to organize them with labels (e.g. "header header" "sidebar main" "footer footer";).

justify-items — Controls horizontal alignment inside grid cells (e.g. justify-items: center;).

align-items — Controls vertical alignment inside grid cells (e.g. align-items: center;).

justify-content — Aligns the whole grid horizontally within its container (e.g. justify-content: space-around;).

align-content — Aligns the whole grid vertically within its container (e.g. align-content: space-between;).

7. Conclusion

CSS Grid is an essential tool for modern web development, enabling developers to create complex layouts without the need for extensive code. By mastering CSS Grid, you can significantly improve your web design skills and create responsive, efficient layouts.

0 Interaction
1.9K Views
Views
45 Likes
×
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

$ Allow cookies on this site ? (y/n)

top-home