I try to fit a square into a rectangle. I used max-width
, max-height
and aspect-ratio
, but it works only in portrait mode and fails in landscape mode. This shows how Firefox renders it:
This is the example.
function portrait() {
document.body.style.width = "10em";
document.body.style.height = "14em";
}
function landscape() {
document.body.style.width = "14em";
document.body.style.height = "10em";
}
document.getElementById("button0").addEventListener('click', portrait);
document.getElementById("button1").addEventListener('click', landscape);
portrait();
html, body {
margin: 0;
}
#page {
height: 100%;
box-sizing: border-box;
border: 1ex solid blue;
display: grid;
grid-template-rows: auto min-content;
}
.square {
max-width: 100%;
max-height: 100%;
aspect-ratio: 1 / 1;
}
canvas {
box-sizing: border-box;
border: 1ex solid red;
display: block;
width: 100%;
height: 100%;
}
.buttons {
box-sizing: border-box;
border: 1ex solid green;
width: 100%;
}
<div id="page">
<div class="square">
<canvas width="100" height="100">
</canvas>
</div>
<div class="buttons">
<input id="button0" type="button" value="Works"/>
<input id="button1" type="button" value="Fails"/>
</div>
</div>
How to fit the red and green boxes into the blue box?
2
Answers
I’ve managed to get it to work using
container-type
. There are nifty container unit called cqmin and cqmax. cqmin is the minimum of the inline and block container size which is handy when the aspect ratio changes. By declaring the square as a container and using thesize
value rather thaninline-size
and setting the width of the canvas to be 100cqmin it fills the height or the width of the grid item, depending on what is the smaller container size (because thesize
value looks at both the block and inline lengths) Thankfully container queries seem to be nearly universally supported.Try using orientation and
@container
rule!MDN Documentation
Note: If you want to do it by real page size change the
@container
to@media