我是CSS和html的新手,现在我正在做我的英雄部分,但当我向英雄部分添加一个图像时,该图像的加载远远超出了标题限制。如何才能使页面中的所有内容都与页眉对齐,而不会越过“边界”。
发布于 2021-03-27 09:20:23
您可以将width和margin: 0 auto;设置为容器。此外,您还应该设置object-fit: cover来调整图像的大小。
.container {
width: 300px;
margin: 0 auto;
}
img {
object-fit: cover;
width: 100%;
height: 250px;
}
body {
background-color: #ecf0f1;
}
.menu {
height: 50px;
background-color: #e67e22;
text-align: center;
color: #ffffff;
line-height: 50px;
font-size: 30px;
font-family: sans-serif;
}<body>
<div class="container menu">
Menü
</div>
<div class="container">
<img src="https://loremflickr.com/cache/resized/65535_50917877588_9db3484f10_c_640_360_nofilter.jpg">
</div>
</body>
如果这个解决方案不起作用,我们可以找到一个更好的解决方案,如果你分享你的代码。
发布于 2021-03-27 11:18:54
img {
object-fit: cover;
width: 100%;
height: 300px;
}
/* if you want to cover the background by image then
add `background-size:cover;` */
}
https://stackoverflow.com/questions/66826014
复制相似问题