Bootstrap 5, full viewport + responsive bg

Viewed 701

I am able to use 2 full viewport div using the following code. Sadly, when I try to add an HD (1366x768) image as the background for the first full viewport div, it breaks everything. I tried to use img-fluid as mentioned in Bootstrap 5 docs

I am using the default HTML template provided by getbootstrap.com as starting template.

It would be nice to know how to make the background image responsive and still keep the 2 full viewport sections intact. Please feel free to ask any other info you would want to know. Thanks.

 <div class="container-fluid px-0 overflow-hidden">

      <div class="w-100 vh-100 bg1">

<!-- need to add a responsive background using an SVG file with HD size here -->

<img src="bg.svg" class="img-fluid" alt="bg"> <!-- breaks everything -->

     </div> 
 
<!-- 2-->


    <div class="w-100 vh-100 bg2"> 

        <div class="row bg3 text-center">
            
                
                    <div class="col pd50">


                        <div class="row row-cols-2">


                            <div class="col pd20 and"><i class="fab fa-android fa-5x green"></i><p>Android apps</p></div>
                            <div class="col pd20 ang"><i class="fab fa-angular fa-5x red"></i><p>Single Page Apps</p></div>
                          
                          
                            <div class="col pd20 chr"><i class="fab fa-chrome fa-5x blue"></i><p>Web Apps</p></div>
                            <div class="col pd20 dok"><i class="fab fa-docker fa-5x bluesky"></i><p>Cloud based apps</p></div>
                          
                        </div> 

                      
             
                    </div>                
                      
              
              <div class="col pd50">
                <i class="fab fa-chromecast fa-10x blue"></i><hr>One website, which works on Android, iPhone, iPad, Tablets, Laptops, Desktop and all. Fast, responsive, modern and secure framework.
                
              </div>
             
              <div class="align-item-center text-center pdbot50">

                <a href="mailto:jeeve@gmail.com"><button type="button" class="btn btn-outline-danger">@gmail</button></a>
              </div>

            </div>

         
                        
          </div>

        </div>

CSS in case you need to refer

/* Google Fonts */

@import url('https://fonts.googleapis.com/css2?family=Work+Sans:wght@300&display=swap');

body {
    font-family: 'Work Sans', sans-serif;
}


.fullbg { 
    background-image: url(/home.svg) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
   
      }

.landpg {
    background-image: url(/home.svg) fixed; 
 
   
}

  
#grad {
    background-image: linear-gradient(to bottom right, #0ca8cc,#9700ff);
  }


.bg1 {
    background-color: #007cff;
    color: white;
}

.bg2 {
    background-color: #00ff64;
    color: black;
}

.bg3 {
    background-color: white;
}

.pd10 {
    padding: 10px;
}

.pd20 {
    padding: 20px;
}

.pd50 {
    padding: 50px;
}

.pt50 {
    padding-top: 50px;
}

.pdbot50 {
    padding-bottom: 50px;
}

.marleft10 {
    margin-left: 10px;
}

.green {
    color: #1fec1f;
}

.blue {
    color: blue;
}

.red {
    color: red;
}

.bluesky {
    color: skyblue;
}

.white {
    color: white;
   }

/* hover */

.and:hover {
    background-color: rgba(37, 147, 161, 0.212);
}

.ang:hover {
    background-color: rgba(37, 147, 161, 0.212);
}

.chr:hover {
    background-color: rgba(37, 147, 161, 0.212);
}

.dok:hover {
    background-color: rgba(37, 147, 161, 0.212);
}

screen cast. When I add the responsive image code form Bootstrap docs everything breaks as seen in GIF below enter image description here

1 Answers

i found issue you have to remove vh-100 from your div which wraps your image check below snippet it's working fine let me know if there is any changes i have added a class named as custom to set 100% image width and to treat image as block element. You can remove if you don't want and add yours

/* Google Fonts */

@import url('https://fonts.googleapis.com/css2?family=Work+Sans:wght@300&display=swap');

body {
    font-family: 'Work Sans', sans-serif;
}

.custom{
  width: 100%;
  display: block;
  margin:auto;
}

.fullbg { 
    background-image: url(https://images.unsplash.com/photo-1593642632823-8f785ba67e45?ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=889&q=80) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
   
      }

.landpg {
    background-image: url() fixed; 
 
   
}

  
#grad {
    background-image: linear-gradient(to bottom right, #0ca8cc,#9700ff);
  }


.bg1 {
    background-color: #007cff;
    color: white;
}

.bg2 {
    background-color: #00ff64;
    color: black;
}

.bg3 {
    background-color: white;
}

.pd10 {
    padding: 10px;
}

.pd20 {
    padding: 20px;
}

.pd50 {
    padding: 50px;
}

.pt50 {
    padding-top: 50px;
}

.pdbot50 {
    padding-bottom: 50px;
}

.marleft10 {
    margin-left: 10px;
}

.green {
    color: #1fec1f;
}

.blue {
    color: blue;
}

.red {
    color: red;
}

.bluesky {
    color: skyblue;
}

.white {
    color: white;
   }

/* hover */

.and:hover {
    background-color: rgba(37, 147, 161, 0.212);
}

.ang:hover {
    background-color: rgba(37, 147, 161, 0.212);
}

.chr:hover {
    background-color: rgba(37, 147, 161, 0.212);
}

.dok:hover {
    background-color: rgba(37, 147, 161, 0.212);
}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" referrerpolicy="no-referrer" />

    <title>Hello, world!</title>
  </head>
  <body>
   <div class="container-fluid px-0 overflow-hidden">

      <div class="w-100 bg1">
      <!----problem was here removed vh-100 from above div------>

<!-- need to add a responsive background using an SVG file with HD size here -->
<img src="https://images.unsplash.com/photo-1593642632823-8f785ba67e45?ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=889&q=80" class="img-fluid custom" alt="bg"> <!-- breaks everything -->

     </div> 
 
<!-- 2-->


    <div class="w-100 bg2"> 

        <div class="row bg3 text-center">
            
                
                    <div class="col pd50">


                        <div class="row row-cols-2">


                            <div class="col pd20 and"><i class="fab fa-android fa-5x green"></i><p>Android apps</p></div>
                            <div class="col pd20 ang"><i class="fab fa-angular fa-5x red"></i><p>Single Page Apps</p></div>
                          
                          
                            <div class="col pd20 chr"><i class="fab fa-chrome fa-5x blue"></i><p>Web Apps</p></div>
                            <div class="col pd20 dok"><i class="fab fa-docker fa-5x bluesky"></i><p>Cloud based apps</p></div>
                          
                        </div> 

                      
             
                    </div>                
                      
              
              <div class="col pd50">
                <i class="fab fa-chromecast fa-10x blue"></i><hr>One website, which works on Android, iPhone, iPad, Tablets, Laptops, Desktop and all. Fast, responsive, modern and secure framework.
                
              </div>
             
              <div class="align-item-center text-center pdbot50">

                <a href="mailto:jeeve@gmail.com"><button type="button" class="btn btn-outline-danger">@gmail</button></a>
              </div>

            </div>

         
                        
          </div>

        </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>

  </body>
</html>

Related