I’m learning CSS grid layout. Here is the result that I want: Holy Grail Correct Result
Here is the result I’m getting: Incorrect Result
html {
width: 100%; /* 100% width of parent (root) element */
height: 100vh; /* 100% height of viewport */
background: rgb(0, 0, 0, 0.1); /* 10% opacity black (very light gray) */
font-size: 1.0em; /* root font size */
font-family: Arial, sans-serif;
}
body {
margin: 0; /* No margin at edge of content */
height: 100%; /* 100% height of parent (html) element */
padding: 0;
}
.header {
font-size: 1.5rem;
background-color: #4c7d7e;
color: white;
grid-area: hd;
}
.footer {
font-size: 0.75rem;
background-color: #4c7d7e;
color: white;
grid-area: ft;
}
.aside-left, .aside-right {
font-size: 0.85rem;
background-color: #6a7d8c;
color: black;
}
.main {
color: black;
grid-area: main;
background-color: #7c959d;
}
.aside-left {
grid-area: al;
}
.aside-right {
grid-area: ar;
}
.container {
display: grid;
width: 100%;
height: 100%;
grid-template: 1fr 3fr 1fr / 1fr 2fr 1fr;
grid-template-areas:
"hd hd hd "
"al main ar"
"ft ft ft";
}
<div class="container">
<header class="header">
Header
</header>
<main>
<aside class="aside-left">
Left Column
</aside>
<div class="main">
Main Content
</div>
<aside class="aside-right">
Right Column
</aside>
</main>
<footer class="footer">
Footer
</footer>
</div>
I’ve google repeatedly searching for grid-template-columns, grid-template-rows, etc. and have found nothing to tell me what I’m doing wrong.
Any hints would be greatly appreciated.
2
Answers
This corrected HTML and CSS will create a grid layout with a header, main content area with left and right columns, and a footer.
The corrected CSS uses the grid-template-areas property to define the layout and assigns grid areas to the respective elements. The grid-template-rows and grid-template-columns properties define the sizing of the rows and columns within the grid.
The corrected HTML restructures the main content area to include the left and right columns within the main element.
You need to correct the HTML structure and ensure the
grid areas
are properly assigned to the correct elements.grid areas
are directly inside the container, and not nested inside other elements like<main>
.grid areas
for each element usinggrid-area
to place them correctly in thegrid
layout.