Search code examples
htmlcssbackgroundbackground-image

Background-size: cover; doesn't cover the whole page


I've seen many posts on this subject but none of the solutions solved it for me. Currently working on a freecodecamp project, and I have a problem where the background image doesnt fill the whole screen even though I used background-size:cover I'm a beginner so I won't be surprised if im missing something very basic here..

Code:

body {
  font-family: 'poppins', sans-serif;
  color: #f3f3f3;
  padding: 0;
  margin: 0;
  background-position: center;
  height: 100vh;
  width: 100vw;
  background-size: cover;
  background-image: url("https://media.istockphoto.com/photos/closeup-focus-on-persons-hands-typing-on-the-desktop-computer-show-picture-id1356364268?b=1&k=20&m=1356364268&s=170667a&w=0&h=YibLOYYDkERhgK4BvRw3TzIlPYQAo4nbMnFA-5CvZ0k=");
  background-repeat: no-repeat;
}

.color-overlay {
  width: 100%;
  height: 100vh;
  background-color: rgba(45, 0, 128, 30%)
}

h1, p{
  text-align: center;
}

h1 {
  margin: 0;
  padding-top: 1em;
}

p {
  margin: 0;
  margin-bottom: 1.5em
}

form {
  max-width: 600px;
  min-width: 300px;
  border-radius: 5px;
  margin: 0 auto;
  padding-bottom: 40px;
  padding-top: 25px;
  background-color: rgba(0, 0, 0, 50%)
}

fieldset {
  border: none;
}

label {
  display: block;
  margin-left: 5px;
  margin-bottom: 0.5em;
}

input,
textarea,
select {
  width: 100%;
  margin: 10px 0 0 0;
  height: 2em;
}

select {
  width: 98%;
  margin-left: 5.5px;
}

.inline {
  width: unset;
  margin: 0;
  margin-top: -0.3em;
  margin-right: 0.5em;
  vertical-align: middle;
}
<!DOCTYPE html>
<html>
  <link rel="stylesheet" href="styles.css">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Survey Form</title>
  </head>
  <body>
      <div class="color-overlay">
    <header>
      <h1 id="title">freeCodeCamp Survey Form</h1>
    <p id="description">Thank you for taking the time to help us improve the platform</p>
    </header>
   
        <form id="survey-form">
          <fieldset>
          <label id="name-label">Name <input id="name" type="text" placeholder="Enter your name" required></label>
          <label id="email-label">Email <input id="email" type="email" placeholder="Enter your Email" required></label>
          <label id="number-label">Age<span> (Optional)</span> <input id="number" type="number" placeholder="Age" min="13" max="120"></label>
          <label>Which option best describes your current role?</label>
          <select id="dropdown">
            <option disabled selected>Select current role</option>
            <option>1</option>
          </select>
          </fieldset>

          <fieldset>
            <label>Would you recommend freeCodeCamp to a friend?</label>
            <label><input class="inline" type="radio" value="1" name="recommended"> Definetly</label>
            <label><input class="inline" type="radio" value="2" name="recommended"> Maybe</label>
            <label><input class="inline" type="radio" value="3" name="recommended"> Not sure</label>
          </fieldset>

          <fieldset>
            <label>What is your favorite feature of freeCodecamp?</label>
            <select id="dropdown2">
              <option disabled selected>Select an option</option>
              <option>test</option>
            </select>
            <label>What would you like to see improved?<span> (Check all that apply)</span></label>
            <label><input class="inline" type="checkbox" value="1">1</label>
            <label><input class="inline" type="checkbox" value="2">2</label>
            <label>Any comments or suggestions?</label>
            <textarea placeholder="Enter your comment here..."></textarea>
          </fieldset>
          <input type="submit" id="submit" value="Submit">
        </form>
      </div>
  </body>
</html>


Solution

  • Use min-height on body and .color-overlay instead of just height to prevent the size from becoming smaller than the value specified.

    body {
      font-family: 'poppins', sans-serif;
      color: #f3f3f3;
      padding: 0;
      margin: 0;
      background-position: center;
      min-height: 100vh;
      width: 100vw;
      background-size: cover;
      background-image: url("https://media.istockphoto.com/photos/closeup-focus-on-persons-hands-typing-on-the-desktop-computer-show-picture-id1356364268?b=1&k=20&m=1356364268&s=170667a&w=0&h=YibLOYYDkERhgK4BvRw3TzIlPYQAo4nbMnFA-5CvZ0k=");
      background-repeat: no-repeat;
    }
    
    .color-overlay {
      width: 100%;
      min-height: 100vh;
      background-color: rgba(45, 0, 128, 30%)
    }
    
    h1,
    p {
      text-align: center;
    }
    
    h1 {
      margin: 0;
      padding-top: 1em;
    }
    
    p {
      margin: 0;
      margin-bottom: 1.5em
    }
    
    form {
      max-width: 600px;
      min-width: 300px;
      border-radius: 5px;
      margin: 0 auto;
      padding-bottom: 40px;
      padding-top: 25px;
      background-color: rgba(0, 0, 0, 50%)
    }
    
    fieldset {
      border: none;
    }
    
    label {
      display: block;
      margin-left: 5px;
      margin-bottom: 0.5em;
    }
    
    input,
    textarea,
    select {
      width: 100%;
      margin: 10px 0 0 0;
      height: 2em;
    }
    
    select {
      width: 98%;
      margin-left: 5.5px;
    }
    
    .inline {
      width: unset;
      margin: 0;
      margin-top: -0.3em;
      margin-right: 0.5em;
      vertical-align: middle;
    }
    <!DOCTYPE html>
    <html>
    <link rel="stylesheet" href="styles.css">
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Survey Form</title>
    </head>
    
    <body>
      <div class="color-overlay">
        <header>
          <h1 id="title">freeCodeCamp Survey Form</h1>
          <p id="description">Thank you for taking the time to help us improve the platform</p>
        </header>
    
        <form id="survey-form">
          <fieldset>
            <label id="name-label">Name <input id="name" type="text" placeholder="Enter your name" required></label>
            <label id="email-label">Email <input id="email" type="email" placeholder="Enter your Email" required></label>
            <label id="number-label">Age<span> (Optional)</span> <input id="number" type="number" placeholder="Age" min="13" max="120"></label>
            <label>Which option best describes your current role?</label>
            <select id="dropdown">
              <option disabled selected>Select current role</option>
              <option>1</option>
            </select>
          </fieldset>
    
          <fieldset>
            <label>Would you recommend freeCodeCamp to a friend?</label>
            <label><input class="inline" type="radio" value="1" name="recommended"> Definetly</label>
            <label><input class="inline" type="radio" value="2" name="recommended"> Maybe</label>
            <label><input class="inline" type="radio" value="3" name="recommended"> Not sure</label>
          </fieldset>
    
          <fieldset>
            <label>What is your favorite feature of freeCodecamp?</label>
            <select id="dropdown2">
              <option disabled selected>Select an option</option>
              <option>test</option>
            </select>
            <label>What would you like to see improved?<span> (Check all that apply)</span></label>
            <label><input class="inline" type="checkbox" value="1">1</label>
            <label><input class="inline" type="checkbox" value="2">2</label>
            <label>Any comments or suggestions?</label>
            <textarea placeholder="Enter your comment here..."></textarea>
          </fieldset>
          <input type="submit" id="submit" value="Submit">
        </form>
      </div>
    </body>
    
    </html>