60

I'm using bootstrap jumbotron, and including a background image. 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!

Jack Bonneman
  • 1,821
  • 18
  • 24
Nick B
  • 9,267
  • 17
  • 64
  • 105

8 Answers8

92

The simplest way is to set the background-size CSS property to cover:

.jumbotron {
  background-image: url("../img/jumbotron_bg.jpg");
  background-size: cover;
}
Edric
  • 24,639
  • 13
  • 81
  • 91
Steve
  • 1,182
  • 1
  • 11
  • 25
  • 1
    I'm using cover for the background-size, but in my case, the client wants the full height of the image always visible. When the screen width extends beyond the width of the jumbotron image, they want the image to expand to greater than 100% width and height. – user3120861 Jul 15 '20 at 21:12
14

This is what I did.
First, just override the jumbotron class, and do the following:

.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 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.

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

.push-spaces
{
    height: 100px;
}
Fadils
  • 1,508
  • 16
  • 21
8

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.

Justin
  • 954
  • 4
  • 22
  • 44
8

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>
JustRocca
  • 81
  • 1
  • 1
  • 1
    Please add more details to your answer :-) – node_modules Jun 06 '16 at 13:55
  • I didn't change the .jumbotron class on the css (or anything else). I just add this style directly on the html file, as I had wrote. – JustRocca Jun 07 '16 at 17:53
  • This approach is a much better than modifying the CSS because the jumbotron usually only appears once on a website Also, by having the settings inserted inline, it enables programmatic generation of the code as is the case with my framework. I have a jumbotron() function that insert random images into the jumbotron every time the page is generated. This allows the creation of a nice randomness on the site without having to use the carousel – JG Estiot Dec 14 '17 at 06:32
6

You could try this:

Simply place the code in a style tag in the head of the html file

<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

        .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. use cover to make the image fill out the jumbotron space and finally no-repeat so that the image is not repeated.

Joan
  • 413
  • 1
  • 5
  • 16
3

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

background-size: cover; may cut off some parts of the image producing poor results.

Using background-size: 100% 100%; you force the image to take up 100% of the parent element for both height and width.

See W3Schools for more information on this.

Here is a working, responsive jumbotron background image:

<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>
maxshuty
  • 9,708
  • 13
  • 64
  • 77
2

Unfortunately, there is no way to make the div height respond to the background-size. Easiest solution that I have used is adding an img tag within your jumbotron that contains that background image.

Community
  • 1
  • 1
Irvin Zhan
  • 824
  • 4
  • 12
2

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.

Dhanesh Agrawal
  • 323
  • 2
  • 6
  • 16