I have the HTML and CSS as follows
.flex {
font: 14px Arial;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.flex>div {
flex: 1 1 auto;
width: 50%;
box-sizing: border-box;
border: 1px solid black;
text-align: center;
line-height: 20px;
padding: 5px;
}
<div class="flex">
<div>{data['player1-pre']}</div>
<div>{data['player2-pre']}</div>
<div>{data['player1-post']}</div>
<div>{data['player2-post']}</div>
</div>
I would like to add labels on the grid lines like this
Is there any way to achieve this? Thanks!
2
Answers
I feel like I brute forced it, but here’s something:
Lot’s of flex displays and a bit of absolute positioning to get the text on the gridlines in place.
You can use three grids to achieve this; one for the items, one for vertical labels, one for horizontal labels:
…of which the latter two will overlay the first.
Here’s a visualization using ASCII art (
1
means1fr
, etc.):We can clearly see that
#vertical
has more than one row, and#horizontal
one column, comparing to#actual
. Let--rows
and--columns
be the number of rows and columns of#actual
, here’s our CSS skeleton:To prevent vertical and horizontal labels from overlapping grid lines, use
isolation: isolate
on.item
andmix-blend-mode: multiply
on.text
:Note that, since we are skipping the first and last row of
#vertical
, along with the first and last column of#horizontal
, we need to fix the padding of both those.grid
s and their corresponding items. The math behind this is left as an exercise for the reader:Try it (randomized content and dimension):