I have a game website where there is a header, a chessboard, and footer. I need to fit all three within the view port. The chessboard should shrink to make this happen.
The shrinking is what I’m having a problem with. I have been able to get width responsiveness, but not to a restrictive height.
The chessboard should remain square and take up unused space or shrink to prevent overflow.
I am doing something along the lines of the following, but the chessboard ends up overflowing the height of the parent.
.parent {
height: 100vh;
background-color: grey;
display: flex;
flex-direction: column;
}
.header {
height: 10px;
background-color: blue;
}
.child {
background-color: red;
flex: 1 1 auto;
}
.chessboard {
width: 100%;
max-height: 100%;
aspect-ratio: 1/1;
background-color: purple;
margin: auto;
}
.footer {
height: 10px;
background-color: green;
}
<div class="parent">
<div class="header">
</div>
<div class="child">
<div class="chessboard">
</div>
</div>
<div class="footer">
</div>
</div>
Any help is appreciated.
3
Answers
You have
aspect-ratio
that say to the chessboard to have the same height as width, so if your width is 3000px your height will be the same. You have optionsI would just use a grid with the board in the middle.
I gave the "child" a different color just to show it was there. Notice how the board stays inside of that but the markup forces us to add an aspect ratio to it also.
EDIT: to let the padding to the heavy lifting here. A lot of credit to this idea here: https://css-tricks.com/aspect-ratios-grid-items/#aa-scenario-1-just-the-element-inside-needs-to-have-an-aspect-ratio
The final answer depends on some clarifications on your part. I asked you in a comment to the question. But I will give a universal answer.
I will briefly explain what is done in the example below.
I created a
.chessboard-wrapper
that has a<canvas>
in it.<canvas>
scales as you need and hasaspect-ratio: 1;
.The
.chessboard
itself hasposition: absolute;
and will take the dimensions of the father.