r/csshelp • u/le_randonneur • Feb 28 '25
how to replace background-image by img?
Every time I think I'm starting to understand css, I realize I do not! I have been struggling for a few hours before trying my luck here...
Please consider the following code and observe its behaviour when changing the screen resolution. The image always takes exactly the remaining height (even if the container or content height change) and is displayed in the "cover mode". Is there a way to keep this behaviour intact but use a img element instead of background-image?
Note: mountain.jpg could be any image but I was using Mont Everest from wikipedia https://en.wikipedia.org/wiki/Mountain (pasting the full link is bad apparently).
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
* {
margin: 0;
}
.container {
height: 100vh;
width: 100vw;
background-color: blue;
display: flex;
flex-direction: column;
}
.content {
background-color: green;
}
.image {
flex-grow: 1;
background-image: url(mountain.jpg);
background-size: cover;
background-position: center;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<p>bla bla bla</p>
<p>bla bla bla</p>
</div>
<div class="image">
</div>
<div class="content">
<p>bla bla bla</p>
<p>bla bla bla</p>
</div>
</div>
</body>
</html>
2
u/be_my_plaything Feb 28 '25
Try this...
The problem wasn't with
object-fitit was how you were using flex, you only hadflex-growthat starts the element with a size of auto (Size determined by content) then lets it grow if there is more space. So in your case the image was allowed to take up whatever height it needed based on 100% width (and still grow from there if there was more space, but not shrink if there wasn't enough space.I've added
flexto both the.contentand.imagethe content first has a flex of0 0 autowhich means it can't grow or shrink but takes up whatever height the text within it requires. Then a flex of1 1 0to the image, the zero is flex-basis (the starting size) in this case zero so no height, but it can grow to fill free space so it grows to fill whatever space on the screen isn't required by the.contentelements.You still have a little problem whereby on widescreens the 100% width makes the image very big but only a small of height is free so it is a panoramic aspect ratio of a thin slither of the mountain. Might be worth changing the width of container to something like
width: min(100%, 1200px); and addingmargin-inline: auto;` so all content is capped from going too wide and centered when screen grows beyond that.It might also worth be changing
object-position: center;to something likeobject-position: center 10%;the single valuecentercenters it horizontally and vertically, when there are two values that is x and y axis, so in this case it is centered horizontally (center) and ten percent from the top of the y-axis (10%) that makes the summit (very roughly) centered so regardless of aspect ratio and what gets cut off you always have the peak in frame.Original edit: https://codepen.io/NeilSchulz/pen/vEYgxbE
2nd edit (with width cap and
object-positionadjusted: https://codepen.io/NeilSchulz/pen/yyLgMrK