01A. Enabling CSS Grid
01A. Enabling CSS Grid ๊ด๋ จ
![](https://res.cloudinary.com/dwppkb069/image/upload/v1684239486/css-grid-tutorial_dfsfgn.png)
Let's enable CSS Grid in a container element and explore the difference between grid
and inline-grid
.
Turning on Grid
First, let's look at a simple container with 10 colored boxes inside it:
![screenshot](https://res.cloudinary.com/dwppkb069/image/upload/v1684351360/tutorials/images-01_Enabling_Grid.mp4/01_enabling_grid_4_00-18520-so-this-is-what-it-looks-like-when-i-have-no-grid-involved_esurth.png)
<div id="container">
<div class="box" style="background-color: #FF5722;">FIRST</div>
...
<div class="box" style="background-color: #3F51B5;">TENTH</div>
</div>
With no grid involved, the container is centered with a black border and each colored box is an individual div
. Each div
goes all the way across the screen, and the height is dictated by the text inside.
![screenshot rainbow](https://res.cloudinary.com/dwppkb069/image/upload/v1684351359/tutorials/images-01_Enabling_Grid.mp4/01_enabling_grid_7_00-27240-and-then-each-of-these-little-rainbow-boxes_ook46y.png)
Here's the starting point of the CSS:
#container {
margin: 0 auto;
width: 90%;
height: 1000px;
border: 4px solid black;
}
To turn on grid, set the display
property of the container element to grid
:
#container {
/* styles from above */
display: grid;
}
Here's what the result looks like:
![empty space screenshot](https://res.cloudinary.com/dwppkb069/image/upload/v1684351359/tutorials/images-01_Enabling_Grid.mp4/01_enabling_grid_16_01-31480-so-there-isnt-any-empty-space_rvs25p.png)
The elements go all the way across the container, but they're taller. Each element grows to take up as much available space as possible.
Inline Grid
There's a second grid-related option for the display property: inline-grid
.
#container {
/* other styles */
display: inline-grid;
}
At first, it looks the same as what we saw before.
However, the difference is that the overall container now behaves as an inline element.
For example, a span
element and the container will share the same space.
Update the HTML to include a span
with "I AM INLINE!!":
<span>I AM INLINE!!</span>
<div id="container">
<!-- colored boxes -->
</div>
And notice that the container is inline:
![grid container](https://res.cloudinary.com/dwppkb069/image/upload/v1684351362/tutorials/images-01_Enabling_Grid.mp4/01_enabling_grid_32_02-17600-our-span-and-this-whole-container-are-both-inline_o4ecba.png)
Switching the CSS back to display: grid
, the container behaves as a block element and doesn't share space like an inline element:
![enabling grid](https://res.cloudinary.com/dwppkb069/image/upload/v1684351361/tutorials/images-01_Enabling_Grid.mp4/01_enabling_grid_35_02-31880-because-it-is-not-an-inline-element_cp3ygi.png)
Recap
You have two choices to turn on the grid system in a container:
display: grid
: The container behaves as a block element.display: inline-grid
: The container behaves as an inline element.