
The CSS grid-area property defines a grid item’s size and location in a grid layout, and is a shorthand property for the following properties:
The CSS grid-area property can also be used to assign a name to a grid item. Named grid items can then be referenced by the grid-template-areas property of the grid container.
Syntax:
grid-area: grid-row-start / grid-column-start / grid-row-end / grid-column-end | itemname;
grid-row-start – defines on which row to start displaying the item.
grid-column-start – defines on which column to start displaying the item.
grid-row-end – defines on which row-line to stop displaying the item, or how many rows to span.
grid-column-end – defines on which column-line to stop displaying the item, or how many columns to span.
itemname – defines a name for the grid item.
grid-area: 2 / 1 / span 2 / span 3;
is a shorthand for:
grid-row-start: 2;
grid-column-start:1;
grid-row-end: span 2;
grid-column-end: span 3;
Example1:
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto;
grid-gap: 10px;
background-color: #e9c46a;
padding: 10px;
}
.grid-container > div {
background-color: #f4a261;
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.item1 {
grid-area: 2 / 1 / span 2 / span 3;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
</div>
</body>
</html>
Output:
Example2:
Item1, is called “example-area” and will take up the place of all five columns:
<!DOCTYPE html>
<html>
<head>
<style>
.item1 {
grid-area: example-area;
}
.grid-container {
display: grid;
grid-template-areas: 'example-area example-area example-area example-area example-area';
grid-gap: 10px;
background-color: #e9c46a;
padding: 10px;
}
.grid-container > div {
background-color: #f4a261;
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
</div>
</body>
</html>
Output:
Example3:
Name all items, and make a ready-to-use webpage template:
<!DOCTYPE html>
<html>
<head>
<style>
.item-1 { grid-area: header; }
.item-2 { grid-area: menu; }
.item-3 { grid-area: main; }
.item-4 { grid-area: side; }
.item-5 { grid-area: footer; }
.grid-container2 {
display: grid;
grid-template-areas:
'header header header header header header'
'menu main main main side side'
'menu footer footer footer footer footer';
grid-gap: 10px;
background-color: #e9c46a;
padding: 10px;
}
.grid-container2 > div {
background-color: #f4a261;
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<div class="grid-container2">
<div class="item-1">Header</div>
<div class="item-2">Menu</div>
<div class="item-3">Main</div>
<div class="item-4">Side</div>
<div class="item-5">Footer</div>
</div>
</body>
</html>
Output:
Enjoy coding!
Read also: