When using grid-template-areas in CSS grid, I wanted to have a header spanning the full width. Yes I can define the template-grid-columns with repeat( 12, 1fr) but what I was looking for is a way to use it instead of having to write the name of the column 12 times. See below.
grid-template-areas:
"article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero"
"header header header header header header header header header header header header"
"main main main main main main main main main aside aside aside"
"footer footer footer footer footer footer footer footer footer footer footer footer";
The first row that has 'article-hero written twelve times, is there a way to use repeat so that I can do repeat(12,article-hero)
instead of writing it out 12 times?
CSS Demo: repeat()This function can be used in the CSS Grid properties grid-template-columns and grid-template-rows .
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); This way, we get a repeating number of columns (thanks to repeat() ) that will fill all of the available space of the grid (thanks to auto-fit ) while never shrinking narrower than 250px and each taking up an equal fraction of space (thanks to minmax() ).
CSS Grid Template Area BasicsA CSS grid template area makes a visual representation of the grid using both columns and rows. This makes for a faster design process than when line-based placement or grid-column and grid-row values are used.
The grid-template-areas property specifies areas within the grid layout. You can name grid items by using the grid-area property, and then reference to the name in the grid-template-areas property. Each area is defined by apostrophes. Use a period sign to refer to a grid item with no name.
When you are using grid-template-areas
you have to take the trouble to write the grid areas out - you don't have repeat here (Its actually very similar to ASCII art
).
.wrapper {
display: grid;
grid-template-areas: "article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero article-hero"
"header header header header header header header header header header header header"
"main main main main main main main main main aside aside aside"
"footer footer footer footer footer footer footer footer footer footer footer footer";
height: 100vh;
}
.wrapper>* {
border: 1px solid cadetblue;
}
article {
grid-area: article-hero;
}
header {
grid-area: header;
}
main {
grid-area: main;
}
footer {
grid-area: footer;
}
aside {
grid-area: aside;
}
<div class="wrapper">
<article>hero</article>
<header>header</header>
<main>main</main>
<aside>aside</aside>
<footer>footer</footer>
</div>
A few things to note when using grid-template-areas
:
They should be rectangular - see an example here
.
The number of columns must be equal in each string of your grid-template-areas
property - else they would be invalid - see an example here
.
Position Items Using Grid Lines
You can switch to line-based-placements
here with a 12-column grid (using grid-template-columns: repeat(12, 1fr)
) and using grid-column
property - see demo below:
.wrapper {
display: grid;
grid-template-columns: repeat(12, 1fr);
height: 100vh;
}
.wrapper>* {
border: 1px solid cadetblue;
grid-column: span 12;
}
.wrapper main {
grid-column: span 9;
}
.wrapper aside {
grid-column: span 3;
}
<div class="wrapper">
<article>hero</article>
<header>header</header>
<main>main</main>
<aside>aside</aside>
<footer>footer</footer>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With