The div contents are spilling out even though the div is set to 100% height

Currently, I am working on a survey form project from freecodecamp and encountering some challenges with my CSS (it's been a bit tricky for me, so please bear with me >.<).

Here is a shortened version of the essential HTML code:

<body>
        <div class="container">
            <h1 id="title">Survey Form</h1>
            <p id="description">Tell us how your experience was like!</p>
            <form method="post" action="" id="survey-form">
                <fieldset>
                    <label for="name-label">Name <input id="name-label" name="name" type="text" placeholder="Please enter your name"  required/></label>
                    <label for="email-label">Email <input id="email-label" name="email" type="email" placeholder="Please enter your email"  required/></label>
                    <label for="number-label">Age <input id="number-label" name="number" type="number" placeholder="Please enter your age" min="16" max="120" required/></label>       
                </fieldset>
                <fieldset>
                <legend>Would you recommend the event to a friend? (required)</legend>
                <select id="dropdown">
                    <option value="">(select an option)</option>
                    <option value="Yes">Yes</option>
                    <option value="No">No</option>
                </select>
                </fieldset>
                <fieldset>
                    <legend>Ticket Type (required)</legend>
                    <label for="normal-ticket"><input id="normal-ticket" value="normal-ticket" name="ticket-type" type="radio" checked /> Normal</label>
                    <label for="vip-ticket"><input id="vip-ticket" name="ticket-type" value="vip-ticket" type="radio" /> VIP Pass</label>
                    </fieldset>
                <fieldset>
                    <legend>What did you purchase?</legend>
                    <label for="snacks">
                        <input id="snacks" type="checkbox" value="snacks">
                    Snacks
                    </label>
                    <label for="drinks-alcoholic">
                        <input id="drinks-alcoholic" type="checkbox" value="drinks-alcoholic">
                    Drinks (alcoholic)
                    </label>
                    <label for="drinks-non-alcoholic">
                        <input id="drinks-non-alcoholic" type="checkbox" value="drinks-non-alcholic">
                    Drinks (non-alcoholic)</label>
                    <label for="food">
                        <input id="food" type="checkbox" value="food">
                    Food
                    </label>
                    <label for="merch">
                    <input id="merch" type="checkbox" value="merch">
                    Merchandise
                    </label>
                </fieldset>
                <fieldset>
                    <label>Any tips for next time? 
                        <textarea name="tips"></textarea>
                    </label>
                </fieldset>
                <input id="submit" type="submit" value="Submit"/>
            </form>
        </div>
    </body>

And here is the CSS I have used:

html,
body {
  width: 100%;
  min-height: 100%;
}
body {
  margin: 0;
  font-family: "Gill Sans", sans-serif;
  font-size: 18px;
  background: url({base64 image url here});
  background-size: cover;
  background-repeat: no-repeat;
  overflow: scroll;
  overflow-x: hidden;
}

div {
  width: 60%;
  height: 100%;
  margin: 0 auto;
  background-color: rgba(255, 255, 255, 0.4);
  display: block;
}

h1,
p {
  text-align: center;
}

form {
  margin: 0 auto;
  width: 60vw;
  max-width: 500px;
  min-width: 300px;
  height: 60vh;
}

fieldset {
  border: none;
}

label {
  display: block;
  padding: 10px;
}

input[type="submit"] {
  margin: 1em auto;
}

In essence, I aim for the div to act as a semi-transparent background for the content, while the body background features a full-screen image, and the div only occupies the specified width. However, I am facing issues where the div's height does not fill the entire screen despite being set to 100% in the CSS. Furthermore, the form content is overflowing.

I managed to address the overflow issue by using {overflow: scroll}. But I wonder if there are alternative solutions to this problem. Moreover, why is this occurring when the content is contained within the div?

I attempted to use min-height instead of height for both the body and later for html,body, but it did not resolve the issue. I also experimented with setting the div position to absolute relative to the body to guarantee that its positioning is centered properly. However, the height remained unchanged at less than 100%. Additionally, I am uncertain whether structuring the markup differently, as shown below, would be advantageous:

<body>
  <div></div>
  {rest of the markup}
</body>

Edit: In response to feedback, I added the body of the HTML code for more context. If it is too lengthy, please let me know, and I will eliminate the less relevant parts :D

Answer №1

I successfully identified the root cause of the overflow issue and the presence of extra blank spaces surrounding the div element.

Upon inspecting the page, I discovered that the body had an additional margin which was causing the problem. After setting body{margin: 0;}, the div now occupies 100vh perfectly!

In addition, I consolidated all items into a single main-container and transferred the CSS properties from the body to this new container. While there are still some design/layout adjustments needed for other elements, I have resolved this particular issue (for now, at least).

Below is the updated HTML and CSS files with the necessary fixes applied:

html,
body {
  width: 100%;
  min-height: 100%;
}

body {
  margin: 0;
}

.main-container {
  width: 100vw;
  height: 100vh;
  background-color: rgba(255, 255, 255, 0.4);
  display: block;
  font-family: "Gill Sans", sans-serif;
  font-size: 18px;
  background: url("https://images.unsplash.com/photo-1519751138087-5bf79df62d5b?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D");
  background-size: cover;
  background-repeat: no-repeat;
  overflow-y: scroll;
}

.container {
  height: 100vh;
  width: 60vw;
  background-color: rgba(255, 255, 255, 0.4);
  margin: 0 auto;
  overflow-x: hidden;
}

form {
  margin: 0 auto;
  width: 60vw;
  min-height: 100vh;
  background-color: rgba(255, 255, 255, 0.4);
  overflow-x: hidden;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Survey Form</title>
  <link rel="stylesheet" href="styles-form.css" />
</head>

<body>
  <div class="main-container">
    <h1 id="title">Survey Form</h1>
    <p id="description">Tell us how your experience was like!</p>
    <form method="post" action="" id="survey-form">
      <fieldset>
        <label for="name-label">Name <input id="name-label" name="name" type="text" placeholder="Please enter your name"  required/></label>
        <label for="email-label">Email <input id="email-label" name="email" type="email" placeholder="Please enter your email"  required/></label>
        <label for="number-label">Age <input id="number-label" name="number" type="number" placeholder="Please enter your age" min="16" max="120" required/></label>
      </fieldset>
      <fieldset>
        <legend>Would you recommend the event to a friend? (required)</legend>
        <select id="dropdown">
          <option value="">(select an option)</option>
          <option value="Yes">Yes</option>
          <option value="No">No</option>
        </select>
      </fieldset>
      <fieldset>
        <legend>Ticket Type (required)</legend>
        <label for="normal-ticket"><input id="normal-ticket" value="normal-ticket" name="ticket-type" type="radio" checked /> Normal</label>
        <label for="vip-ticket"><input id="vip-ticket" name="ticket-type" value="vip-ticket" type="radio" /> VIP Pass</label>
      </fieldset>
      <fieldset>
        <legend>What did you purchase?</legend>
        <label for="snacks">
          <input id="snacks" type="checkbox" value="snacks">
          Snacks
        </label>
        <label for="drinks-alcoholic">
          <input id="drinks-alcoholic" type="checkbox" value="drinks-alcoholic">
          Drinks (alcoholic)
        </label>
        <label for="drinks-non-alcoholic">
          <input id="drinks-non-alcoholic" type="checkbox" value="drinks-non-alcholic">
          Drinks (non-alcoholic)
        </label>
        <label for="food">
          <input id="food" type="checkbox" value="food">
          Food
        </label>
        <label for="merch">
          <input id="merch" type="checkbox" value="merch">
          Merchandise
        </label>
      </fieldset>
      <fieldset>
        <label>Any tips for next time? 
          <textarea name="tips"></textarea>
        </label>
      </fieldset>
      <input id="submit" type="submit" value="Submit" />
    </form>
  </div>
</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for targeting a specific class selector instead of an n-th child selector in CSS

My table element is generated dynamically by Angular, with rows that change based on data. The layout of the page is as follows: <table class="myDataGrid"> <thead> <tr> <th>In Service</th> ...

Ways to determine the position of elements when they are centered using `margin:auto`

Is there a more efficient way to determine the position of an element that is centered using CSS margin:auto? Take a look at this fiddle: https://jsfiddle.net/vaxobasilidze/jhyfgusn/1/ If you click on the element with the red border, it should alert you ...

Modifying inline styles in React is as easy as placing the number within a div tag

I'm currently working on a React table component that consists of rows and columns. My main goal is to have the rows arranged vertically and the numbers within each row aligned horizontally. Here's how it looks at the moment: https://i.sstatic. ...

Alert: mssql_execute() encountered an issue and was unable to locate the stored procedure

Despite spending nearly a day trying to solve this issue, I am still unable to find a solution. I am encountering a problem with an MSSQL procedure in PHP. I have a form with 5 fields that calculate an "Amount." The procedure works perfectly in SQL Managem ...

The alignment of elements side by side in Angular Flex is not supported, specifically in versions Angular 8.2.1 and Flex-Layout 8.0.0.beta 26

I'm attempting to arrange my boxes in a single line, with the ability to wrap and switch to column mode on smaller phone screens. Desired layout: Wins Losses Ties Points On shrinking screen: Wins Losses Ties Points Code ...

PHP For Loop Generating HTML Table

I need assistance troubleshooting my code in an attempt to display it within an HTML table using a while loop. Despite my efforts, the table is not appearing. Perhaps there's an issue with how I am attempting to echo out the table? <?php #---- ...

Creating multiple tables in an HTML template using loops, either with or without JavaScript, each table identical to the one previously created

<html> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3. ...

Submit information into mysql using an html form (and beyond)

Hey there! I'm new to the world of MYSQL/PHP and currently working on a simple script to enhance my skills in php/mysql. However, I've hit a few roadblocks along the way. I apologize if some of these questions have already been asked before, but ...

How can I align text to the bottom right and left corners at the same time?

Is there a way to align text so that it appears in both the bottom right and bottom left corners of the page, functioning as a footer? For example: JANUARY (bottom left corner) / 2019 (bottom right corner) If anyone knows how to achieve this using HTML ...

Calculate the pixel distance between various divs by measuring the horizontal distance from the left border to the right

I am trying to find the precise distance between : #main div left edge to the first div with class="b" between the first div with class="b" to the second div with class="b" It's worth noting that the divs may be arranged randomly and could have fix ...

Using React Native to showcase a background grid of dimensions {height: 10, width: 10}

Hey there! I'm looking to create a grid that can adapt to different device sizes and screen positions, similar to a chess grid. I want to use this as a background to help with sizing and positioning elements like text and images on the screen. Here&a ...

What are some creative ways to enhance closePath() in canvas styling?

Currently, I am faced with the challenge of styling the closePath() function on my canvas. Specifically, I would like to apply different stroke styles to each line segment. For now, let's focus on changing colors for each line. In the example below, y ...

What is the reason that CSS selectors cannot use numbers as IDs?

I have come to realize that numbers cannot be used in CSS selectors. <!DOCTYPE html> <html> <style type="text/css"> div{width:350px;word-wrap:break-all; } #1{float:left;} </style> <div class="up"> <p><img id= ...

Blur effect on backdrop-filter causing shadowy inset borders (specific to Chrome and Windows)

Note: The issue mentioned below has been resolved in the latest version of Chrome (119.0.6045.200). While transitioning on backdrop-filter, I have observed a dark inset shadow that is only visible during the transition. This behavior seems to occur only ...

Issues encountered with AngularJs filter search functionality

Why am I having trouble adapting this search filter from here? This is what my code looks like: controllers.js angular.module('starter.controllers', ['starter.services']) .controller('AppCtrl', function($scope, $ionicModal ...

Troubleshoot: Dropdown menu in Materialize not functioning (menu fails to drop down

I am currently incorporating Materialize to create a dropdown button in my navigation bar. However, I am facing an issue where nothing happens when the button is clicked. Here is a snippet of my code: <head> <meta charset="UTF-8"> <!-- ...

Responsive full-screen background image display

Why is the background image not appearing as a full image on large screens but fitting fine on small screens? <!DOCTYPE html> <html dir="ltr"> <head> <meta charset="utf-8" dir="ltr" /> <title ...

Adjust the width of a table using Bootstrap 4

I am looking to adjust the table width in bootstrap 4. In the image, you can see that the table does not extend to fill the entire width of the page. Below is the HTML code: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4. ...

Setting border radius for the first child of li dynamically using Javascript

I am currently working on creating a navigation bar that features rounded corners for the first and last children within an unordered list. My goal is to utilize the onclick javascript function to dynamically assign these rounded corners directly in JavaSc ...

The WebView component is unable to display internally stored CSS files within the project

Looking to display HTML content with CSS styling stored in a file downloaded from a URL and saved within the "context.filesDir". The downloaded folder contains all the assets required by the CSS file. Despite passing the CSS file path to the webv ...