简体   繁体   English

Div与背景图像和高度自动

[英]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. 如果您希望以图像本身的精确尺寸或纵横比显示图像,则需要使用图像的精确widthheight来定义元素。

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取元素内容的高度。

  • They key is to specify a height in vh or px. 它们的关键是用vh或px指定高度。
  • After that you can easily place your text div inside it and set it around with either flexbox or position absolute 之后,您可以轻松地将文本div放在其中,并使用flexbox或absolute absolute设置它

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.

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