Place responsive blocks in flex columns
P粉545682500
2023-09-03 19:51:51
<p>I have a gaming website that has a header, board, and footer. I need all three to fit inside the viewport. The board should be smaller to achieve this. </p>
<p>Minification is the problem I'm having. I've been able to get the width responsive but not the restrictive height. </p>
<p>The board should remain square and occupy unused space or shrink to prevent overflow. </p>
<p>I'm doing something along the lines of below, but the board ends up overflowing the parent's height. </p>
<p>In my actual code, the height of the title is unknown due to line wrapping. Footers have varying amounts of text. Inside the chessboard there are a series of divs that represent the rows and a series of child elements that represent the squares of the chessboard. A square has an aspect ratio</p>
<p>
<pre class="brush:css;toolbar:false;">.parent {
height: 100vh;
background-color: gray;
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;
}</pre>
<pre class="brush:html;toolbar:false;"><div class="parent">
<div class="header">
</div>
<div class="child">
<div class="chessboard">
</div>
</div>
<div class="footer">
</div>
</div></pre>
</p>
<p>Thanks for any help. </p>
You have
aspect-ratio
telling the board to have the same height as the width, so if your width is 3000px, your height will be the same. You have a choiceThe final answer depends on some clarification on your part. I asked you in a comment on this question. But I will give a general answer.
I will briefly explain what is done in the example below.
I created a
.chessboard-wrapper
with ain it.
Scale as needed, with
aspect-ratio: 1;
..chessboard
itself hasposition:absolute;
and will take the dimensions of the parent.