bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/CSS/Modern Layout
CSS•Modern Layout

CSS Naming Grid Items

Naming Grid Items with grid-area

The CSS grid-template-areas is a grid container property, and it specifies areas within the grid layout.

You can name grid items by using the CSS grid-area property, and then reference to the name in the grid-template-areas property.

Each area is defined within apostrophes. The named grid items in each area is defined inside the apostrophes, separated by a space.

Example

Formatted code
.container {
  display: grid;
  grid-template-areas: 'myHeader myHeader myHeader myHeader myHeader';
} .item1 {
grid-area: myHeader;
}

Live preview

Expected output

1 2 3 4 5 6

We can use a period sign (.) to refer to a grid item with no name:

Example

Formatted code
.container {
  display: grid;
  grid-template-areas: 'myHeader myHeader myHeader . .';
} .item1 {
grid-area: myHeader;
}

Live preview

Expected output

1 2 3 4 5 6

To define two rows (two areas), define the second row inside another set of apostrophes:

Example

Formatted code
.container {
  display: grid;
  grid-template-areas: 'myHeader myHeader . . .'
  'myHeader myHeader . . .';
} .item1 {
grid-area: myHeader;
}

Live preview

Expected output

1 2 3 4 5 6

Make a ready-to-use Webpage Template

Here, we will name all grid items to make a ready-to-use webpage template:

Example

Formatted code
.item1 { grid-area: header; } .item2 { grid-area: menu; } .item3 {
  grid-area: main; } .item4 { grid-area: right; } .item5 { grid-area: footer; } .container {
  display: grid;
  grid-template-areas: 'header header header header header header'
  'menu main main main main right'
  'menu footer footer footer footer footer';
}

Live preview

Expected output

Header Menu Main Right Footer

Previous

Responsive Web Design - Frameworks

Next

Responsive Web Design - Templates

This chapter

Overview
24

Lessons

64m

Read time

1. CSS Flexbox2. CSS Grid Layout3. Responsive Web Design4. CSS Flex Container5. CSS Grid Container6. Responsive Web Design - The Viewport7. CSS Flexbox justify-content8. CSS Grid Tracks9. Responsive Web Design - Building a Grid View10. CSS Flexbox Align11. CSS Grid Gap12. Responsive Web Design - Media Queries13. CSS Flex Items14. CSS Grid Container - Align Grid Content15. Responsive Web Design - Images16. CSS Responsive Flexbox17. CSS Grid Items18. Responsive Web Design - Frameworks19. CSS Naming Grid Items20. Responsive Web Design - Templates21. CSS Grid Items - Alignment22. CSS Grid Items - Order23. CSS 12-Column Grid Layout24. CSS @supports Rule

On this page

Naming Grid Items with grid-areaMake a ready-to-use Webpage Template