I'm trying to have a flex item div to have the same dimensions as its contained image, but it seems the div doesn't know about the image's scaled size and always has the same width as the full sized image, resulting in a visible border on the right side.
I understand that if I remove the .flex-top
div everything works as expected, because the .flex-bottom
div and its parents have an height of 100%. Once the top div is back, however, it still keeps the same width, as if flex-grow had no effect on its computed height.
I'm pretty sure the problem lies in .flex-bottom
's height 100% but can't think of a workaround.
* {
box-sizing: border-box;
}
html {
height: 100%;
}
body {
margin: 0;
height: 100%;
display: flex;
flex-direction: column;
background: white;
}
.flex-container {
display: flex;
flex-direction: column;
flex-grow: 1;
min-height: 0;
height: 100%;
background: white;
}
.flex-top {
flex: 2;
background: salmon;
}
.flex-bottom {
display: flex;
flex: 1;
min-height: 0;
align-self: center;
background: seagreen;
}
img {
height: 100%;
width: auto;
}
<body>
<div class="flex-container">
<div class="flex-top">
<p>Hello, I take 2/3 of the page</p>
</div>
<div class="flex-bottom">
<img src="https://picsum.photos/500" />
</div>
</div>
</body>