There are four parts to this answer. The first three help explain the fourth, which covers the reason for the extra column. If you're only interested in the answer, skip to the end.
Contents:
- More than meets the eye: There's also an extra row!
- The
grid-area
property.
- The
grid-template-areas
property.
- The placement of unreferenced grid areas.
1. More than meets the eye: There's also an extra row!
You've only partially defined the problem. Yes, there's an extra column. But there's also an extra row.
Because you haven't defined a height on the grid container, the height defaults to auto
– the height of the content (more details). So any rows with no content simply collapse and are invisible.
This issue doesn't exist with width because, in this case, you're using a block-level container (created by display: grid
), which is designed to consume the full width of its parent, by default (more details).
So that's why you're not seeing the extra row. If you give the container some height, the row will appear.
body {
display: grid;
grid-template-areas:
"header"
"footer";
height: 150px; /* new */
}
body {
display: grid;
grid-template-areas:
"header"
"footer";
height: 150px; /* new */
}
header {
grid-area: header;
background: aqua;
}
main {
grid-area: main;
background: darkorange;
}
footer {
grid-area: footer;
background: lightgreen;
}
<header>Header</header>
<main>Main</main>
<footer>Footer</footer>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…