简体   繁体   English

div全宽度的响应背景图像

[英]Responsive background image in div full width

I'm trying to figure out how to make a background-image in a div full width and responsive. 我试图找出如何在div全宽和响应中制作背景图像。 The background-image is expanding across the width of the page (and is responsive), but the height of the image isn't its full height. 背景图像在页面宽度上扩展(并且响应),但图像的高度不是其全高。 It seems like it's being cut-off somehow. 似乎它正以某种方式被切断。 I'm using bootstrap framework, and the reason I'm trying to do this is I want to overlay some text on the image. 我正在使用bootstrap框架,我试图这样做的原因是我想在图像上叠加一些文本。 I've tried so many different things but cant seem to figure it out, help! 我尝试了很多不同的东西,但似乎无法弄明白,帮助!

<div class="bg-image">
  <div class="container">
    <div class="row-fluid">
      <div class="span12">
        <h1>This is some text</h1>
      </div>
    </div>
  </div>
</div>


  .bg-image {
  background: url(img/image.jpg) no-repeat center top;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  max-height: 450px;
  }

Here is one way of getting the design that you want. 这是获得您想要的设计的一种方法。

Start with the following HTML: 从以下HTML开始:

<div class="container">
    <div class="row-fluid">
        <div class="span12">
            <div class="nav">nav area</div>
            <div class="bg-image">
                <img src="http://unplugged.ee/wp-content/uploads/2013/03/frank2.jpg">
                 <h1>This is centered text.</h1>
            </div>
            <div class="main">main area</div>
        </div>
    </div>
</div>

Note that the background image is now part of the regular flow of the document. 请注意,背景图像现在是文档常规流程的一部分。

Apply the following CSS: 应用以下CSS:

.bg-image {
    position: relative;
}
.bg-image img {
    display: block;
    width: 100%;
    max-width: 1200px; /* corresponds to max height of 450px */
    margin: 0 auto;
}
.bg-image h1 {
    position: absolute;
    text-align: center;
    bottom: 0;
    left: 0;
    right: 0;
    color: white;
}
.nav, .main {
    background-color: #f6f6f6;
    text-align: center;
}

How This Works 这是如何工作的

The image is set an regular flow content with a width of 100%, so it will adjust itself responsively to the width of the parent container. 图像设置为宽度为100%的常规流内容,因此它将根据父容器的宽度进行自我调整。 However, you want the height to be no more than 450px, which corresponds to the image width of 1200px, so set the maximum width of the image to 1200px. 但是,您希望高度不超过450px,这对应于1200px的图像宽度,因此将图像的最大宽度设置为1200px。 You can keep the image centered by using display: block and margin: 0 auto . 您可以使用display: blockmargin: 0 auto来保持图像居中。

The text is painted over the image by using absolute positioning. 使用绝对定位在文本上绘制文本。 In the simplest case, I stretch the h1 element to be the full width of the parent and use text-align: center to center the text. 在最简单的情况下,我将h1元素拉伸为父元素的整个宽度,并使用text-align: center center将文本居中。 Use the top or bottom offsets to place the text where it is needed. 使用顶部或底部偏移将文本放置在需要的位置。

If your banner images are going to vary in aspect ratio, you will need to adjust the maximum width value for .bg-image img dynamically using jQuery/Javascript, but otherwise, this approach has a lot to offer. 如果横幅图像的宽高比会有所不同,则需要使用jQuery / Javascript动态调整.bg-image img的最大宽度值,否则,这种方法可以提供很多。

See demo at: http://jsfiddle.net/audetwebdesign/EGgaN/ 请参阅演示: http//jsfiddle.net/audetwebdesign/EGgaN/

When you use background-size: cover the background image will automatically be stretched to cover the entire container. 当您使用background-size: cover ,背景图像将自动拉伸以覆盖整个容器。 Aspect ratio is maintained however, so you will always lose part of the image, unless the aspect ratio of the image and the element it is applied to are identical. 但是,长宽比会保持不变,因此除非图像的宽高比和应用的元素相同,否则您将始终丢失部分图像。

I see two ways you could solve this: 我看到两种方法可以解决这个问题:

  • Do not maintain the aspect ratio of the image by setting background-size: 100% 100% This will also make the image cover the entire container, but the ratio will not be maintained. 不要通过设置background-size: 100% 100% 保持图像的纵横比 background-size: 100% 100%这也会使图像覆盖整个容器,但不会保持比例。 Disadvantage is that this distorts your image, and therefore may look very weird, depending on the image. 缺点是这会扭曲您的图像,因此可能看起来非常奇怪,具体取决于图像。 With the image you are using in the fiddle, I think you could get away with it though. 随着你在小提琴中使用的图像,我认为你可以逃脱它。

  • You could also calculate and set the height of the element with javascript , based on its width, so it gets the same ratio as the image. 您还可以使用javascript根据宽度计算和设置元素的高度 ,因此它与图像的比例相同。 This calculation would have to be done on load and on resize. 此计算必须在加载和调整大小时完成。 It should be easy enough with a few lines of code (feel free to ask if you want an example). 使用几行代码应该很容易(如果你想要一个例子,可以随意询问)。 Disadvantage of this method is that your width may become very small (on mobile devices), and therfore the calculated height also, which may cause the content of the container to overflow. 这种方法的缺点是您的宽度可能变得非常小(在移动设备上),因此计算的高度也会变小,这可能导致容器的内容溢出。 This could be solved by changing the size of the content as well or something, but it adds some complexity to the solution/ 这可以通过改变内容的大小来解决,但它增加了解决方案的一些复杂性/

I also tried this style for ionic hybrid app background. 我也试过这种风格的离子混合应用程序背景。 this is also having style for background blur effect. 这也有背景模糊效果的风格。

.bg-image {
   position: absolute;
   background: url(../img/bglogin.jpg) no-repeat;
   height: 100%;
   width: 100%;
   background-size: cover;
   bottom: 0px;
   margin: 0 auto;
   background-position: 50%;


  -webkit-filter: blur(5px);
  -moz-filter: blur(5px);
  -o-filter: blur(5px);
  -ms-filter: blur(5px);
  filter: blur(5px);

} }

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

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