繁体   English   中英

缩放 div 以适合背景图像

[英]Scale div to fit background image

我有一个带有背景图像的 div,我想扩展 100% 的宽度并自动缩放 div 以适应图像所需的高度。 目前,除非我将 div 的高度设置为 100%,否则它不会缩放 div 高度,但它只是拉伸到屏幕的整个高度,而我希望它缩放到图像的高度。

这是html:

<div id="mainHeaderWrapper">


</div><!--end mainHeaderWrapper-->
<br class="clear" />;

这是CSS:

    #mainHeaderWrapper{


     background: url(http://localhost/site/gallery/bg1.jpg);
     width: 100%;
     height: auto;
     -webkit-background-size: cover;
     -moz-background-size: cover;
     -o-background-size: cover;
     background-size: cover; 
     background-size: 100% 100%;

     background-repeat: no-repeat;
     background-position: center center; 

 }

 .clear { clear: both; }

感谢您的任何帮助

让透明图像决定DIV尺寸。
在那个 div 中放置相同的图像,CSS opacity: 0

jsBin 演示

<div id="mainHeaderWrapper">
   <img src="path/to/image.jpg"><!-- I'm invisible! -->
</div>

将该图像设置为

#mainHeaderWrapper {
    background: no-repeat url(path/to/image.jpg) 50% / 100%;
}
#mainHeaderWrapper img {
    vertical-align: top;
    width: 100%; /* max width */
    opacity: 0;  /* make it transparent */
}

这样 DIV 的高度将由包含的不可见图像决定,并且将background-image设置为中心、完整50% / 100% )它将匹配该图像的比例。

需要该 DIV 中的一些内容吗?

jsBin 演示由于包含图像,您将需要一个额外的子元素,将设置为position: absolute作为覆盖元素

<div id="mainHeaderWrapper">
   <img src="path/to/image.jpg"><!-- I'm invisible! -->
   <div>Some content...</div>
</div>
#mainHeaderWrapper{
    position: relative;
    background: no-repeat url(path/to/image.jpg) 50% / 100%;
}
#mainHeaderWrapper > img{
    vertical-align: top;
    width: 100%; /* max width */
    opacity: 0;  /* make it transparent */
}
#mainHeaderWrapper > div{
    position: absolute;
    top: 0;
    width: 100%;
    height: 100%;
}

如果您知道图像的比例,请使用百分比填充来定义容器的高度。 设置height:0并将垂直填充设置为宽度的百分比。

这种方法的关键是基于百分比的垂直填充始终与宽度相关。

根据盒子模型(w3.org)

百分比是根据生成的框的包含块的宽度计算的,即使对于“padding-top”和“padding-bottom”也是如此。

下面的图片是400px X 200px,所以高宽比例为1:2, padding-top设置为50%;

 #mainHeaderWrapper { width: 100%; height: 0; padding-top: 50%; background-image: url('https://dummyimage.com/400x200/'); background-size: 100% auto; background-repeat: no-repeat; }
 <div id="mainHeaderWrapper"></div> stuff below the image

在另一个示例中,图像为 300 像素 X 100 像素。 高度是宽度的 1/3,所以padding-top设置为 33.33%:

 #mainHeaderWrapper { width: 100%; height: 0; padding-top:33.33%; background-image: url('https://dummyimage.com/300x100/'); background-size: 100% auto; background-repeat: no-repeat; }
 <div id="mainHeaderWrapper"></div> stuff below the image


编辑:

按照Paulie_D的提示,div中的其他内容必须绝对定位,如下所示。 我建议也使用百分比来定位这些元素。

 #mainHeaderWrapper { width: 100%; height: 0; padding-top: 33.33%; background-image: url('https://dummyimage.com/300x100/'); background-size: 100% auto; background-repeat: no-repeat; } div#inner_content { position: absolute; top: 0; width: 100%; margin-top: 10%; color: #FFF; text-align: center; font-size: 20px; }
 <div id="mainHeaderWrapper"> <div id="inner_content">Hello World</div> </div> stuff below the image

这可以在不使用虚拟图像的情况下完成。 例如,我将使用我刚刚使用的图像的尺寸。

我的图像尺寸为 2880x1410。 简化尺寸 -> 96/47(我使用了这个简单的比率计算器http://andrew.hedges.name/experiments/aspect_ratio/ )。 得到简化的比率后,将高度和宽度代入方程:

高度:计算((100vw * W)/ H); 所以我的会读:高度:calc((100vw * 47) / 96);

也无需担心 div 的内容(除非它们不合适)

 body{ margin: 0; padding: 0} #box1{ background: url(http://lorempixel.com/400/200/food/); background-size: cover; background-attachment: fixed; margin: 0; width: 100%; height: 100vh; display: table; } h1{ color: #ffffff; font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, "sans-serif"; font-size: 38px; text-align: center; font-weight: normal; background: rgba(0,0,0,0.3); display: table-cell; vertical-align: middle}
 <div id="box1"> <h1>Code Bluster BILU </h1> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM