Background image not covering entire page?

16.2k Views Asked by At

I've just begun learning about HTML and CSS (and a little Bootstrap) and I'm trying to make a simple scrolling webpage. My problem is that I cannot get the div background image to cover the full height of the page -- it only covers as far as the h2 text, like it thinks the end of that element is the end of the page. I have no idea why it's doing this and I haven't been able to fix it. This has really stumped me and I'm sure it has a really simple solution that I've missed. Any advice would be hugely appreciated.

Here is the codepen:

body {
  background-color: rgb(255, 255, 255);
}

#front {
  background: url("https://image.ibb.co/mjTmwv/port34.png") no-repeat;
  overflow: hidden;
  background-size: cover;
  width: 100%;
  height: 100%;
}

#h1 {
  color: white;
  font-size: 50px;
  margin-top: 40vh;
  padding: 10px;
  background: rgba(43, 142, 255, 0.4);
}

#pt {
  color: white;
  font-size: 30px;
  background: rgba(39, 220, 130, 0.4);
}
<ul style="z-index:9;">
  <li>
    <a class="active" href="#"><img class="img-fluid" style="width:auto;height:auto;max-height:26px;max-width:20px;" src="https://image.ibb.co/mg1s3a/lyreimage27.png" alt="website icon"></img> placeholder inc.</a>
  </li>
  <li><a href="#">About
      </a>
  </li>
  <li><a style="text-decoration:none" href="#">Portfolio</a>
  </li>
  <li><a href="#">Contact</a>
  </li>
</ul>

<header>
  <div id="front">
    <div class="container-fluid">
      <h1 id="h1" class="text-center">Placeholder</h1>
      <h2 id="pt" class="text-center">Placeholder Text</h2>
</header>

4

There are 4 best solutions below

0
On BEST ANSWER

I cleaned up your markup a little bit (try not to use inline styles when possible) and think I achieved the effect you are looking for.

#front {
  background: url("https://image.ibb.co/mjTmwv/port34.png") no-repeat;
  overflow: hidden;
  background-size: cover;
  width: 100%;
  min-height: 100vh;
}

.text-center {
  text-align: center;
}

h1 {
  color: white;
  font-size: 50px;
  margin-top: 40vh;
  padding: 10px;
  background: rgba(43, 142, 255, 0.4);
}

h2 {
  color: white;
  font-size: 30px;
  background: rgba(39, 220, 130, 0.4);
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.7);
  position: fixed;
  top: 0;
  width: 100%;
  font-size: 16px;
  z-index: 9;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover:not(.active) {
  color: white;
  background-color: rgba(39, 220, 130, 0.4);
  transition: 0.5s;
  text-decoration: none;
}

.active {
  color: white;
  background-color: rgba(43, 142, 255, 0.4);
}

.active:hover {
  color: white;
  text-decoration: none;
}

.active img {
  width:auto;
  height:auto;
  max-height:26px;
  max-width:20px;
}
<ul>
  <li><a class="active" href="#"><img class="img-fluid" src="https://image.ibb.co/mg1s3a/lyreimage27.png" alt="website icon" />placeholder inc.</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Portfolio</a></li>
  <li><a href="#">Contact</a>
  </li>
</ul>

<header id="front">
  <div>
    <div class="container-fluid text-center">
      <h1>Placeholder</h1>
      <h2>Placeholder Text</h2> 
    </div>
  </div>
</header>

3
On

Your #front div does not have enough content to display on entire page. You are suppose to have enough content to cover entire page or else set background-image for body element. Else change your #front css as below.

#front {
  background: url("https://image.ibb.co/mjTmwv/port34.png") no-repeat;
  overflow: hidden;
  background-size: cover;
  width: 100%;
  height: 100vh;
}

body
    {
     background-color: rgb(255, 255, 255);
    }

#front {
    background:       url("https://image.ibb.co/mjTmwv/port34.png") no-repeat;
  overflow: hidden;
  background-size: cover;
  width: 100%;
  height: 100vh;
}

#h1 {
  color: white;
  font-size: 50px;
  margin-top: 40vh;
  padding: 10px;
  background: rgba(43, 142, 255, 0.4);
}

#pt {
  color: white;
  font-size: 30px;
  background: rgba(39, 220, 130, 0.4);
}
<ul style="z-index:9;">
  <li>
    <a class="active" href="#"><img class="img-fluid" style="width:auto;height:auto;max-height:26px;max-width:20px;" src="https://image.ibb.co/mg1s3a/lyreimage27.png" alt="website icon"></img> placeholder inc.</a>
  </li>
  <li><a href="#">About
      </a>
  </li>
  <li><a style="text-decoration:none" href="#">Portfolio</a>
  </li>
  <li><a href="#">Contact</a>
  </li>
</ul>

<header>
  <div id="front">
    <div class="container-fluid">
      <h1 id="h1" class="text-center">Placeholder</h1>
      <h2 id="pt" class="text-center">Placeholder Text</h2>
</header>

0
On

#front is 100% the size of header. Try setting your header to height: 100%. Alternatively (the better solution), set the background on the body if you want it to cover the whole page. It's the simpler option and makes more sense.

0
On

Set height of your header element to 100vh;

So in your css add:

header {
height: 100vh;
}