CSS Grid Container
A grid container holds one or more grid items arranged in columns and rows.
All direct child elements of a grid container automatically become grid items.
An element becomes a grid container when its display property is set to grid or inline-grid.
Example
Prompt
Create an HTML page with a grid container holding 5 numbered items (1 through 5). Use
display: grid on the container.
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid Container</title>
<style>
.container {
display: grid;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.container > div {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h1>Grid Container</h1>
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
</body>
</html>
Display Grid Property
A <div> element becomes a grid container when its display property is set to grid or inline-grid.
Block-level grid
Use display: grid to make a block-level grid container.
Prompt
Create an HTML page with a div container using
display: grid that holds 5 numbered child items (1 through 5). Style each item with a background color and padding to show the block-level grid behavior.
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Grid</title>
<style>
.container {
display: grid;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.container > div {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h1>Display Grid</h1>
<p>A block-level grid container:</p>
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
</body>
</html>
Inline-level grid
Use display: inline-grid to make an inline-level grid container.
Prompt
Create an HTML page with a div container using
display: inline-grid that holds 5 numbered child items (1 through 5). Style each item with a background color and padding to show the inline-level grid behavior.
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Inline Grid</title>
<style>
.container {
display: inline-grid;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.container > div {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h1>Display Inline Grid</h1>
<p>An inline-level grid container:</p>
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
</body>
</html>
CSS Grid Container Properties
Here is a list of all CSS properties that can be used on a grid container:
align-content— vertically aligns the grid inside the container.align-items— aligns items in the grid.column-gap— sets the gap between columns.display— sets the element as a grid container.gap— a shorthand forrow-gapandcolumn-gap.grid— a shorthand for multiple grid properties.grid-auto-columns— sets the size of auto-generated columns.grid-auto-flow— controls how auto-placed items are inserted.grid-auto-rows— sets the size of auto-generated rows.grid-template— a shorthand for defining rows, columns, and areas.grid-template-areas— defines named grid areas.grid-template-columns— sets the number and size of columns.grid-template-rows— sets the number and size of rows.justify-content— horizontally aligns the grid inside the container.justify-items— aligns items along the row axis.place-content— a shorthand foralign-contentandjustify-content.place-items— a shorthand foralign-itemsandjustify-items.row-gap— sets the gap between rows.