I'd like to calculate the number of rows in a grid template using calc()
, but trying to get the repeat
count with division isn't working:
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr;
margin-bottom: 10px;
background: rgba(0, 0, 0, 0.2);
}
.grid>div {
background: tomato;
width: 20px;
text-align: center;
margin: auto;
}
.grid.no-calc {
grid-template-rows: repeat(3, 30px);
}
.grid.addition {
grid-template-rows: repeat(calc(1 + 2), 30px);
}
.grid.subtraction {
grid-template-rows: repeat(calc(4 - 1), 30px);
}
.grid.multiplication {
grid-template-rows: repeat(calc(3 * 1), 30px);
}
.grid.division {
grid-template-rows: repeat(calc(6 / 2), 30px);
}
<p>Top 4 have their row heights set correctly</p>
<div class="grid no-calc">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<div class="grid addition">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<div class="grid subtraction">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<div class="grid multiplication">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<p>Division doesn't work in setting row height</p>
<div class="grid division">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
See Question&Answers more detail:os