Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 901
Edart Avatar asked May 26 '26 03:05

Edart


1 Answers

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.

like image 100
Thanveer Shah Avatar answered May 30 '26 05:05

Thanveer Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!