I'm trying to make a grid of resizable squares with some text inside them. Here's the code:
/* Dirty quick CSS reset */
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-flow: column;
flex: 1;
background: aliceblue;
}
.row {
display: flex;
flex: 1;
}
.square {
border: 1px solid black;
width: 14.2857%; /* 100% / 7 */
font-size: 18px;
padding: 8px;
/* square-width - font-size - padding-top */
padding-bottom: calc(14.2857% - 18px - 8px);
}
<div class="container">
<div class="row">
<div class="square">1</div>
<div class="square">2</div>
<div class="square">3</div>
<div class="square">4</div>
<div class="square">5</div>
<div class="square">6</div>
<div class="square">7</div>
</div>
</div>
As we can see, there's a row of squares that adapt to the size of the window. The problem is that if we inspect them, we see that they aren't totally squares (they are about 3px taller than wide). It gets worse if we increase the font-size
, and as far as I know, the maths is correct.
What's going on here? Why am I getting those extra pixels?