[英]Div with background-image and height auto
I am trying to display a div
within a background-image
. 我试图在
background-image
显示div
。 I have tried to do it in the same way that I would do it using an img
but it does not work: 我尝试以与使用
img
相同的方式执行此操作,但它不起作用:
WITHIN IMG 在IMG内
HTML HTML
<img class="scale" src="../../../assets/images/van-landing-2.JPG">
CSS CSS
.scale {
max-width: 100%;
height: auto;
width: auto\9;
/* ie8 */
}
This is perfectly working. 这非常有效。 It shows an image that takes 100% of my screen and the height that should have to be proportional.
它显示的图像占据了我的屏幕的100%,高度应该是成比例的。
Photo 照片
Nevertheless, when I try the same using a div it does not work. 然而,当我尝试使用div时,它不起作用。 It displays nothing to cause it does not set a specific height, so It shows nothing.
它没有显示任何导致它没有设置特定高度,因此它什么也没显示。
WITHIN DIV 在DIV之内
HTML HTML
<div class="i-van"></div>
CSS CSS
.i-van {
background-image: url("../../../assets/images/van-landing.JPG");
max-width: 100%;
height: auto;
width: auto\9;
/* ie8 */
}
Photo 照片
How could I do it? 我怎么能这样做? I have tried using
min-height
and it shows it but just the minimum height. 我尝试过使用
min-height
并显示它但只是最小高度。 I would like to show it all. 我想展示一切。
The background-image
property is used to add a background to an element. background-image
属性用于向元素添加背景。 This means that the content with in that element is what dictates its size. 这意味着该元素中的内容决定了它的大小。 Additionally,
height:auto
is interpreted by the elements content. 另外,
height:auto
由元素内容解释。
What you can try is to use height:100%
providing that the parent elements also have defined height values. 您可以尝试使用
height:100%
前提是父元素也有定义的高度值。 This will stretch the element to the tallest height and scale your background image accordingly. 这会将元素拉伸到最高的高度并相应地缩放背景图像。
If you are looking to display the image at the exact size or aspect ratio of the image itself, then the element will need to be defined with the exact width
and height
of the image. 如果您希望以图像本身的精确尺寸或纵横比显示图像,则需要使用图像的精确
width
和height
来定义元素。
From a semantics perspective however, you should decide if the image you are displaying is part of the content of your page or a decorative part of the design. 但是,从语义角度来看,您应该确定您显示的图像是页面内容的一部分还是设计的装饰部分。 In general, you should use a
<img />
tag for images that are content and background-image
for images that are not. 通常,对于不是
background-image
内容和background-image
图像,应使用<img />
标记。
You can learn more about background-image here 您可以在此处了解有关背景图像的更多信息
You have to declare a valid height for your background div. 您必须声明背景div的有效高度。 As Maneesh has said
height:auto
takes the element content's height. 正如Maneesh所说的
height:auto
取元素内容的高度。
Check the snippets! 检查片段! :)
:)
CODE WITH FLEXBOX 使用FLEXBOX的代码
body { margin: 0; } .i-van { display: flex; align-items: center; justify-content: center; background-image: url(https://www.planwallpaper.com/static/images/3865967-wallpaper-full-hd_XNgM7er.jpg); background-size: cover; background-position: center; max-width: 100%; height: 100vh; } #div-inside { font-size: 100px; color: white; }
<div class="i-van"> <div id="div-inside"> HELLO </div> </div>
CODE WITH POSITION ABSOLUTE 具有位置绝对的代码
body { margin: 0; } .i-van { position: relative; background-image: url(https://www.planwallpaper.com/static/images/3865967-wallpaper-full-hd_XNgM7er.jpg); background-size: cover; background-position: center; max-width: 100%; height: 100vh; } #div-inside { position: absolute; top: 50%; /* position the top edge of the element at the middle of the parent */ left: 50%; /* position the left edge of the element at the middle of the parent */ transform: translate(-50%, -50%); /* This is a shorthand of translateX(-50%) and translateY(-50%) */ font-size: 100px; color: white; }
<div class="i-van"> <div id="div-inside"> HELLO </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.