How can we have an <img>
or <canvas>
that stretches/shrinks to fill the remaining space in a display: flex
type of layout? It is straightforward to make a <div>
fill the remaining space, but an <img>
has its own idea of size that stops layout working properly. For example, suppose we have:
- a parent
<div>
withdisplay: flex
, - a child
<h1>
withflex: 0 0 auto
, and - a child
<img>
withflex: 1 1 0px
.
As the window is resized, the <img>
(or <canvas>
) sometimes fits exactly but sometimes overflows the parent depending on the size or aspect ratio of the window.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Test</title>
<style>
body {
width: 100vw;
height: 100vh;
box-sizing: border-box;
display: flex;
flex-flow: column nowrap;
margin: 0px;
border: 4px dashed lightcoral;
}
h1 {
flex: 0 0 auto;
margin: 0px;
border: 4px solid chocolate;
}
div {
flex: 1 1 0px;
border: 4px dotted darkorchid;
}
canvas {
width: 100%;
height: 100%;
box-sizing: border-box;
border: 4px solid lightseagreen;
}
</style>
</head>
<body>
<h1>Heading</h1>
<div><canvas width="400" height="200"/></div>
<script>
var c = document.getElementsByTagName('canvas').item(0);
var x = c.getContext('2d');
x.ellipse(100, 100, 50, 50, 0, 0, 360);
x.fill();
</script>
</body>
</html>