标题非常 self 描述。请记住,容器元素应该有自己的宽度和高度,如示例所示。虽然这个示例是用 flexbox 编写的,但我不介意使用其他方法,只要它是纯粹、干净的 HTML/CSS。
我觉得这应该是微不足道的,但我一直在寻找一段时间,包括 Stack Overflow 上的相关答案,我似乎找不到满意的答案。
body {
margin: 0;
padding: 0;
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
img {
margin-left: 8px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div>
<h1>Lorem Ipsum</h1>
<h2>Dolor Sit Amet, here's some extra text</h2>
</div>
<img src="https://via.placeholder.com/64" />
</body>
</html>
最佳答案
如果再添加几个div,这是可以的。
.wrapper {
display: flex;
align-items: stretch;
}
img {
height: 100%;
}
<body>
<div class="wrapper">
<div>
<h1>Lorem Ipsum</h1>
<h2>Dolor Sit Amet, here's some extra text</h2>
</div>
<div class="img-wrap">
<img src="https://via.placeholder.com/64">
</div>
</div>
</body>
关于html - <img> 如何缩放到 sibling 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57715310/