我有一个有点大的图像,为了不占据更宽设备的所有屏幕,我希望当达到某个高度限制时,从顶部和底部看起来都是裁剪的,例如600px.这在更小、更窄的屏幕上不是问题,因为图像按比例缩小,不会覆盖我的Web应用程序的其他部分.提前谢谢!
这是我目前得到的:
Html:
<div class="image-container">
<img src="../../assets/home_image1.jpg" alt="bg image of airplane" class="background-image" />
<div class="text-overlay">
<h1>Universal Tours</h1>
</div>
</div>
Css:以下内容:
.image-container {
position: relative;
text-align: center;
}
.background-image {
width: 100%;
height: auto;
display: block;
}
.text-overlay {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.5); /* Optional background color */
padding: 10px;
color: #fff; /* Text color */
border-radius: 5px; /* Optional border-radius */
}
我试过高度限制,但它会在保持宽高比的情况下缩小图像,这会在图像周围留下空白,看起来不是很好.