Edart
Edart

Reputation: 41

Responsive background image in bootstrap container

I'm using multiple containers so I can have unique transparent image in them.

I want the background image in them to be responsive and to keep ratio, I found a lot of suggestions to put background-size: cover; but it doesn't work for me. I can't get the image to be responsive.

what I've tried:

  object-fit: contain;    
  -webkit-background-size: cover;     
  -moz-background-size: cover;    
  -o-background-size: cover;    
  background-size: cover;            
  flex: 1;     

css code:

 .container{
  display: block;
  position: relative;
}

 .container1::after {
  content: "";
  background: url(bg2.jpg);
  background-repeat:no-repeat;
  background-position: center center;
  opacity: 0.3;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1; 
  object-fit: contain;  
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  flex: 1; 
}

html:

<div class="container container1">
<p> some text </p>
</div>

Upvotes: 3

Views: 29188

Answers (1)

Thanveer Shah
Thanveer Shah

Reputation: 3323

If you want the same image to scale based on the size of the browser window then here is the code.

background-image:url('../images/bg.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;

Do not set width, height, or margins.

Upvotes: 10

Related Questions