gpt4 book ai didi

html - 填充背景图像而不是在屏幕调整大小时拉伸(stretch)

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:35 24 4
gpt4 key购买 nike

我有一张适应屏幕高度且全宽的背景图片。问题是,当我调整宽度时,图像会拉伸(stretch)而不是填充。此外,应该位于其下方的内容会完全覆盖图像。

到目前为止,这是我的代码。

HTML:

<div id="bg">
<img src="http://placehold.it/2560x1000" class="img" alt="" />
</div>
<div id="content">
<!-- some content -->
</div>

CSS:

#bg {
width: 100%;
height: 100%;
position: absolute;
left: 0px;
top: 0px;
z-index: -1;

}

.img {
background-position: center center;
background-size: contain;
height: 100%;
width: 100%;
position: relative;
}

这里有一个 fiddle :http://jsfiddle.net/anm5sqft/

最佳答案

你使用了“background-position”和“background-size”但是你没有背景,因为你的图像不是背景,它是图像。

试试这个:

HTML

<div id="bg">
</div>

CSS

#bg {
height: 100vh;
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
background-image: url("http://placehold.it/2560x1000");
}

body{
margin: 0;
}

Example with JSFiddle

关于html - 填充背景图像而不是在屏幕调整大小时拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33732800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com