如何使一个div根据设置的背景自动调整大小,而无需为它设置特定的高度(或最小高度)?
如何使一个div根据设置的背景自动调整大小,而无需为它设置特定的高度(或最小高度)?
有一种非常好的方法可以让背景图片像 img
元素一样工作,从而自动调整其 height
。您需要知道图像的宽高比。将容器的 height
设置为 0,并将 padding-top
按百分比设置为图像比例。
效果如下:
div {
background-image: url('http://www.pets4homes.co.uk/images/articles/1111/large/feline-influenza-all-about-cat-flu-5239fffd61ddf.jpg');
background-size: contain;
background-repeat: no-repeat;
width: 100%;
height: 0;
padding-top: 66.64%; /* (img-height / img-width * container-width) */
/* (853 / 1280 * 100) */
}
你刚刚获得了一个带有自动高度的背景图片,其工作方式类似于img元素。这是一个可工作的原型(你可以调整大小并检查div高度):http://jsfiddle.net/8m9ur5qj/
@mixin div-same-size-as-background-img($url) { background-image: url($url); background-size: contain; background-repeat: no-repeat; width: 100%; height: 0; padding-top: percentage(image-height($url) / image-width($url)); }
- Benjamin K.position:relative
应用于某个div,然后在其中添加另一个div,并设置其为position:absolute; top:0; bottom:0; left:0; right:0;
。 - malifa另一个,可能效率较低的解决方案是在 img
元素下包含图像并将其设置为 visibility: hidden;
。然后将周围 div 的 background-image
设置为与图像相同。
这将使周围的div大小与 img
元素中的图像相同,但以背景形式显示。
<div style="background-image: url(http://your-image.jpg);">
<img src="http://your-image.jpg" style="visibility: hidden;" />
</div>
img
,为什么不直接 不 隐藏实际的 <img>
,也不用考虑 background-image
呢?这种做法有什么优势呢? - Mark Amery使用CSS无法自动调整背景图像的大小。
你可以通过在服务器上测量背景图像,然后将这些属性应用于div来绕过此问题,正如其他人所提到的那样。
您也可以通过编写一些JavaScript,根据图像大小(一旦图像已下载)调整div的大小来解决此问题 - 这基本上是相同的方法。
如果您需要使您的div适应图像大小,我想问为什么不在您的div内部放置一个<img>
标签?
img
的 src
,但是你可以通过媒体查询更改用作背景图像的文件。 - Scott Marcus这个回答与其他回答类似,但对大多数应用程序来说是最好的。您通常需要事先知道图像大小。这将使您能够添加叠加文本、标题等,而无需使用负填充或绝对定位图像。关键是将填充%设置为与图像宽高比相匹配,如下面的示例所示。我使用了此答案,基本上只是添加了一个图像背景。
.wrapper {
width: 100%;
/* whatever width you want */
display: inline-block;
position: relative;
background-size: contain;
background: url('https://upload.wikimedia.org/wikipedia/en/thumb/6/67/Wiki-llama.jpg/1600px-Wiki-llama.jpg') top center no-repeat;
margin: 0 auto;
}
.wrapper:after {
padding-top: 75%;
/* this llama image is 800x600 so set the padding top % to match 600/800 = .75 */
display: block;
content: '';
}
.main {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
color: black;
text-align: center;
margin-top: 5%;
}
<div class="wrapper">
<div class="main">
This is where your overlay content goes, titles, text, buttons, etc.
</div>
</div>
我看了一些解决方案,它们很棒,但我觉得我找到了一个令人惊讶的简单方法。
首先,我们需要从背景图像中获取比例。我们只需通过一个维度除以另一个维度。然后我们得到类似于66.4%的东西。
当我们有了图像比率之后,我们可以通过将比率乘以视口宽度来简单地计算div的高度:
height: calc(0.664 * 100vw);
对我来说,它很有效,可以正确设置div的高度,并在窗口调整大小时进行更改。
也许这可以帮助您,虽然不完全是一个背景,但您可以得到灵感:
<style>
div {
float: left;
position: relative;
}
div img {
position: relative;
}
div div {
position: absolute;
top:0;
left:0;
}
</style>
<div>
<img src="http://antwrp.gsfc.nasa.gov/apod/image/0903/omegacen_davis.jpg" />
<div>Hi there</div>
</div>
float: left;
破坏了他的定位。 - Isaac我很确定这段话不会被看到,但如果你的问题和我的一样,这是我的解决方案:
.imaged-container{
background-image:url('<%= asset_path("landing-page.png") %> ');
background-repeat: no-repeat;
background-size: 100%;
height: 65vw;
}
我想让一个div在图片中央,这可以让我实现这个效果。
其他答案忽略的一个纯CSS解决方案。
"content:"属性主要用于向元素插入文本内容,但也可以用于插入图像内容。
.my-div:before {
content: url("image.png");
}
这将导致div的高度调整为图像的实际像素大小。要同时调整宽度,请添加以下内容:.my-div {
display: inline-block;
}
最近引入的CSS aspect-ratio
属性(约2020-2021年)是一种很好的方法,可以避免填充黑客,并且受到所有主流浏览器的支持。
由于我们需要事先知道图像的宽高比,在许多用例中,您将能够预先确定图像尺寸比率(但并非始终适用于用户生成的内容),因此您可以在必要时硬编码单个样式或内联css。
aspect-ratio
将根据提供的比例计算高度(或如果指定了高度,则计算宽度),当宽度被指定时。
div {
/*common ratio, like an 800*600px image */
aspect-ratio: 3 / 2;
/* computed height will be 133.33px, which is width/aspect-ratio */
width: 200px;
/* red BG for debugging so any image bleed is shown*/
background: red;
background-image: url('https://images.unsplash.com/photo-1631163190830-8770a0ad4aa9?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=200&q=80');
}
<div></div>