Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cover background images in Bootstrap 3 grids?

I'm trying to make a landing page using Bootstrap 3. I want a top image in a full width column with three images below it in columns across and no margins or borders so the images joins seamlessly.

I can get close but when I narrow the view port a space opens up between the top image an the ones below it.

Here is the URL:

Here is my code:

HTML:

<div class="container-fluid">
  <div class="row">      

    <div class="landing-col col-xs-12"></div>

  </div>

  <div class="row">

    <div class="first-col col-sm-4"></div>                      
    <div class="second-col col-sm-4"></div>           
    <div class="third-col col-sm-4"></div>

  </div>

</div>

CSS:

.landing-col {
   background: url('../images/99.jpg') no-repeat;
   -webkit-background-size: 100% auto;
   -moz-background-size: 100% auto;
   -o-background-size: 100% auto;
    background-size: 100% auto;
    height: 500px; }

.first-col {
   background: url('../images/44.jpg') no-repeat;
   -webkit-background-size: 100% auto;
   -moz-background-size: 100% auto;
   -o-background-size: 100% auto;
    background-size: 100% auto;
    height: 300px;
}

.second-col {
background: url('../images/33.jpg') no-repeat;
-webkit-background-size: 100% auto;
-moz-background-size: 100% auto;
-o-background-size: 100% auto;
 background-size: 100% auto;
 height: 300px;
}

.third-col {
background: url('../images/22.jpg') no-repeat;
-webkit-background-size: 100% auto;
-moz-background-size: 100% auto;
-o-background-size: 100% auto;
 background-size: 100% auto;
 height: 300px;
}

I have tried using min-width and max-width with 100% but they don't work for me.

A possibility might be to use a media query to get the exact width of the current grid and set the image width inside it to that width. Is this possible?

like image 586
markhorrocks Avatar asked Aug 17 '14 09:08

markhorrocks


People also ask

How do I put a background image on a grid in Bootstrap?

You could always add a transparent image placeholder inside the div to hold the container, and then set an auto height on the containing div 'col-xs-12'. This will still enable you to use a background image with the cover attribute which will fill the area on narrower columns.

How do you cover a background image in HTML?

If you want the background image to cover the entire element, you can set the background-size property to cover.

How do you put a background on a picture?

The most common & simple way to add background image is using the background image attribute inside the <body> tag. The background attribute which we specified in the <body> tag is not supported in HTML5. Using CSS properties, we can also add background image in a webpage.


1 Answers

You can use block element with background image where height is set via padding-bottom with percentage value

.img {
    margin-right: -15px; // Remove right gap
    margin-left: -15px;  // Remove left gap
    padding-bottom: 62.5%; // ratio is 16:10
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-size: cover;
}

Example in Codepen

or with img elements (in this case your images should have the same dimensions)

Example in Codepen

like image 104
Rakhat Avatar answered Oct 19 '22 03:10

Rakhat