简体   繁体   English

响应式 Bootstrap Jumbotron 背景图像

[英]Responsive Bootstrap Jumbotron Background Image

I'm using bootstrap jumbotron, and including a background image.我正在使用 bootstrap jumbotron,并包含背景图像。 Resizing the screen makes the image tile and repeat, whereas I want the image to be responsively resized.调整屏幕大小会使图像平铺并重复,而我希望图像能够响应地调整大小。

<div class="jumbotron" style="background-image: url(http://www.californiafootgolfclub.com/static/img/footgolf-1.jpg); background-size: 100%;">
   <div class="container for-about">
   <h1>About</h1>
   </div>
</div>

How would you go about making the image responsive?您将如何使图像具有响应性? The site is HERE .该网站在这里 Thanks for your ideas!谢谢你的想法!

The simplest way is to set the background-size CSS property to cover :最简单的方法是将background-size CSS 属性设置为cover

.jumbotron {
  background-image: url("../img/jumbotron_bg.jpg");
  background-size: cover;
}

This is what I did.这就是我所做的。
First, just override the jumbotron class, and do the following:首先,只需覆盖 jumbotron 类,然后执行以下操作:

.jumbotron{
    background: url("bg.jpg") no-repeat center center; 
    -webkit-background-size: 100% 100%;
    -moz-background-size: 100% 100%;
    -o-background-size: 100% 100%;
    background-size: 100% 100%;
}

So, now you have a jumbotron with responsive background in place.所以,现在你有了一个具有响应式背景的超大屏幕。 However, as Irvin Zhan already answered, the height of the background still not showing correctly.但是,正如詹尔文已经回答的那样,背景的高度仍然没有正确显示。

One thing you can do is fill your div with some spaces such as this:您可以做的一件事是用一些空格填充您的 div,例如:

<div class="jumbotron">
    <div class="container">
        About
        <br><br><br> <!--keep filling br until the height is to your liking-->
    </div>
</div>

Or, more elegantly, you can set the height of the container.或者,更优雅的是,您可以设置容器的高度。 You might want to add another class so that you don't override Bootstrap container class.您可能想要添加另一个类,以免覆盖 Bootstrap 容器类。

<div class="jumbotron">
    <div class="container push-spaces">
        About
    </div>
</div>

.push-spaces
{
    height: 100px;
}

I found that this worked perfectly for me:我发现这对我来说非常有效:

.jumbotron {
background-image: url(/img/Jumbotron.jpg);
background-size: cover;
height: 100%;}

You can resize your screen and it will always take up 100% of the window.您可以调整屏幕大小,它始终占据 100% 的窗口。

This is how I do :我就是这样做的:

 <div class="jumbotron" style="background: url(img/bg.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"> <h1>Hello</h1> </div>

You could try this:你可以试试这个:

Simply place the code in a style tag in the head of the html file只需将代码放在 html 文件头部的样式标记中

 <style> .jumbotron { background: url("http://www.californiafootgolfclub.com/static/img/footgolf-1.jpg") center center / cover no-repeat; } </style>

or put it in a separate css file as shown below或者把它放在一个单独的css文件中,如下所示

 .jumbotron { background: url("http://www.californiafootgolfclub.com/static/img/footgolf-1.jpg") center center / cover no-repeat; }

use center center to center the image horizontally and vertically.使用center center将图像水平和垂直居中。 use cover to make the image fill out the jumbotron space and finally no-repeat so that the image is not repeated.使用cover使图像填满jumbotron空间,最后不重复,使图像不重复。

TLDR: Use background-size: 100% 100%; TLDR:使用background-size: 100% 100%; . .

background-size: cover; may cut off some parts of the image producing poor results.可能会切断图像的某些部分,从而产生较差的效果。

Using background-size: 100% 100%;使用background-size: 100% 100%; you force the image to take up 100% of the parent element for both height and width .您强制图像占用100%的父元素的heightwidth

See W3Schools for more information on this.有关这方面的更多信息,请参阅W3Schools

Here is a working, responsive jumbotron background image:这是一个有效的响应jumbotron背景图像:

<div class="jumbotron" style="background-image: url(http://yourImageUrl.jpg); background-size: 100% 100%;">
    <h1>Welcome</h1>
    <p class="lead">Your message here</p>
    <p><a href="http://www.YourLinkHere.com" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

Unfortunately, there is no way to make the div height respond to the background-size .不幸的是,没有办法让div 高度响应 background-size Easiest solution that I have used is adding an img tag within your jumbotron that contains that background image.我使用过的最简单的解决方案是在包含该背景图像的 jumbotron 中添加一个 img 标签。

The below code works for all the screens :以下代码适用于所有屏幕:

.jumbotron {
   background: url('backgroundimage.jpg') no-repeat center center fixed;
   -webkit-background-size: cover;
   -moz-background-size: cover;
   background-size: cover;
   -o-background-size: cover;
}

The cover property will resize the background image to cover the entire container, even if it has to stretch the image or cut a little bit off one of the edges. cover 属性将调整背景图像的大小以覆盖整个容器,即使它必须拉伸图像或从边缘剪掉一点。

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

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