Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I style every other row of a CSS Grid? [duplicate]

Tags:

css

css-grid

I have an indefinite amount of div elements (pieces of content / grid items). I want them to layout in a defined number of columns of CSS Grid. The CSS Grid lays them out like that easily. But now as the elements are in place I'd like to style the <div>s in every other row of the resultant grid in some way.

Think of it as styling every other row of table to a darker color.

This question can be generalised to asking: can you style an arbitrary row/ column of a CSS Grid?

Proposed situation:

<div class="content-grid">
    <div class=""content-grid__item></div>
    <div class=""content-grid__item></div>
    <div class=""content-grid__item></div>
    ...

</div>

The css for it:

.content-grid {
    display: grid;
    grid-template-columns: 77px 77px 77px;
}

.content-grid__item {
    background-color: red;
}

And the preferable solution in of the ideal world:

// pseudocode
.content-grid:nth-row(odd) .content-grid__item {
    background-color: darkred;
}
like image 921
wiktus239 Avatar asked Sep 27 '17 15:09

wiktus239


People also ask

How can you add some margin between the different rows of a CSS Grid?

The grid-row-gap property in CSS is used to define the size of the gap between the grid elements. The user can specify the width of the gap separating the rows by providing value to the grid-row-gap.

What is 1fr in CSS Grid?

Each column is equal. 1FR=25% of the available space.

How do you split grid in CSS?

If you have a grid element that should span two columns, you can do grid-column: span 2 , or grid-row: span 2 for rows.


1 Answers

You can't...

CSS Grid rows are not DOM elements and so cannot be selected by CSS.

like image 73
Paulie_D Avatar answered Nov 25 '22 02:11

Paulie_D