I am currently working on a component that will have variable content, and I want the image to respond like a background image by taking the height of its container instead of defining the height itself. Even when using object-fit: cover
, the image still affects the height of its parent element. I would prefer the text to determine the height of the parent rather than the image. Unfortunately, I cannot use a background image because the image is populated via a wysiwyg editor and I do not have control over where the uploaded image is loaded (it must be an actual image). Thank you for any assistance you can provide.
CODE:
.container {
display: flex;
flex-direction: row;
padding: 20px;
background: lightblue;
width: 80%;
}
.img-item {
align-self: stretch;
flex-shrink: 2;
}
.img-item img {
object-fit: cover;
height: 100%;
width: 100%;
}
.text-item {
flex-shrink: 3;
padding: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie-edge" />
<title>Static Template</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container>
<div class="img-item>
<img src="https://i.imgur.com/2bvab7y.jpg" alt="" />
</div>
<div class="text-item>
<h1>
Heading one
</h1>
<p>Hello world. You're the best.</p>
</div>
</div>
</body>
</html>