Setting image height to 100vh causes overlap with other elements

Viewed 970

I am trying to create a page using Bootstrap 4 that has a header, footer and main content. The main content should fill the space below the header and the footer should be below this.

I have tried to do this by setting 100vh height on the main content, this works but when I insert an image it is overlapping the footer. Where am I going wrong? Why is the image not resizing?

#content-header {
  background: red;
  color: white;
}

#content-main {
  background: blue;
  color: white;
  height: 100vh;
}

#content-header {
  background: red;
  color: white;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

<div id="app" class="h-100">
  <div id="content" class="d-flex flex-column">
    <nav id="content-header" class="p-4">
      <div class="navContent d-flex justify-content-between">
        Navbar
      </div>
    </nav>
    <main id="content-main" class="flex-grow-1 p-5">
      Main Content
      <img class="img-fluid" src="https://placeimg.com/1000/1000/any">
    </main>
    <div id="footer" class="p-4">
      Footer Content
    </div>
  </div>
</div>

5 Answers

add min-height insted of height

#content-main {
  background: blue;
  color: white;
  min-height: 100vh;
}

#content-header {
  background: red;
  color: white;
}

#content-main {
  background: blue;
  color: white;
  min-height: 100vh;
}

#content-header {
  background: red;
  color: white;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

<div id="app" class="h-100">
  <div id="content" class="d-flex flex-column">
    <nav id="content-header" class="p-4">
      <div class="navContent d-flex justify-content-between">
        Navbar
      </div>
    </nav>
    <main id="content-main" class="flex-grow-1 p-5">
      Main Content
      <img class="img-fluid" src="https://placeimg.com/1000/1000/any">
    </main>
    <div id="footer" class="p-4">
      Footer Content
    </div>
  </div>
</div>

Try this code. I added max-width: 100% and height: auto to the image

 #content-header {
    background:red;
    color:white;
   }
   #content-main {
    background:blue;
    color:white;
    height:100vh;
   }
   #content-header {
    background:red;
    color:white;
   }
            #background-img{
                max-width: 100%;
                height: auto;
            }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div id="app" class="h-100">
  
   <div id="content" class="d-flex flex-column">
    
    <nav id="content-header" class="p-4">
    
     <div class="navContent d-flex justify-content-between">
    
      Navbar
     
     </div>
    
    </nav>

    <main id="content-main" class="flex-grow-1 p-5">
                    
     Main Content
     <img id="background-img" class="img-fluid" src="https://placeimg.com/1000/1000/any">

    </main>

    <div id="footer" class="p-4">
    
     Footer Content
 
    </div>

   </div>

  </div>

Please change height: 100vh to height: 100%

#content-main {
  background: blue;
  color: white;
  height: 100%;
}

You are question is right. Because you are set main-content height 100vh, but if you observe that you are image size is more then screen height. You are screen size will 700px to 800px, but image size is 1000px. So your image is overlap to footer.

You want the entire layout to be 100% height, not just #content-main.

Therefore, you want to set min-height:100vh (min-vh-100) on the #content. Then, the flex-grow-1 will allow main to fill the height between nav and footer:

<div id="app">
    <div id="content" class="d-flex flex-column min-vh-100">
        <nav id="content-header" class="p-4">
            <div class="navContent d-flex justify-content-between"> Navbar </div>
        </nav>
        <main id="content-main" class="flex-grow-1 p-5 overflow-auto">
            Main Content
            <img class="img-fluid" src="https://placeimg.com/1000/1000/any">
        </main>
        <div id="footer" class="p-4"> Footer Content </div>
    </div>
</div>

https://codeply.com/p/gnLCSZG9gy

Related