Setting the css height to 100% won't be effective if the elements are floated either to the right or left

I'm currently working on a web project and I need to create an HTML page. I'd like to adjust the height of an element to a percentage to ensure it fits the page well.

However, when I try to use the float property in CSS and set it as follows:

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

The height doesn't seem to respond to this setting. As a temporary solution, I changed the position property instead of using float. I'm curious to understand why it's not working as intended. Can anyone offer some insights?

Here is the code snippet causing the issue:

html, body{
    width: 100%;
    height: 100%;
    margin: 0;
}

#test1, #test2{
    display:inline-block;
    height: 100%;
    width:30%;
}
#test1{
    float:left;
    background: #111111;
}
#test2{
    float:right;
    background: #009A61;
}

#test3{
    display:inline-block;
    height: 100%;
    width:40%;
    background: cornsilk;
}
<!doctype html>
  <html>
  <head>
    <link rel="stylesheet" href="test.css" />
  </head>
  <body>
    <div id="test1"></div>
    <div id="test3"></div>
    <div id="test2"></div>
  </body>
</html>

If you exclude the above code snippets, you'll see the following result: view detailed image

The issue is that there shouldn't be a white section at the bottom of the page.

Answer №1

Your three sections are each 1 viewport high, however, the #test3 section is inline-block, creating a line box for it to sit in. Within this line box, there is a strut with a baseline aligned vertically with the bottom of the #test3 section, and the descender part of the strut extends below this. As a result, a vertical scroll bar is displayed to accommodate the document height up to the bottom of the strut, causing a white gap to appear at the bottom.

To resolve this issue, you can disconnect the vertical alignment of the strut from the #test3 section by setting the #test3 section's vertical alignment to top.

html, body{
    width: 100%;
    height: 100%;
    margin: 0;
}

#test1, #test2{
    display:inline-block;
    height: 100%;
    width:30%;
}
#test1{
    float:left;
    background: #111111;
}
#test2{
    float:right;
    background: #009A61;
}

#test3{
    display:inline-block;
    height: 100%;
    width:40%;
    background: cornsilk;
    vertical-align:top;
}
    <div id="test1"></div>
    <div id="test3"></div>
    <div id="test2"></div>

Answer №2

To fix the issue of floated elements not affecting the height of their parent container, one solution is to add an empty block level element with clear: both; right before the closing tag of the parent element. Although this method is a quick fix, it is not recommended for long-term use.

Source: here

html, body{
    width: 100%;
    height: 100%;
    margin: 0;
}

#test1, #test2{
    display:inline-block;
    height: 100%;
    width:30%;
}
#test1{
    float:left;
    background: #111111;
}
#test2{
    float:right;
    background: #009A61;
}

#test3{
    display:inline-block;
    height: 100%;
    width:40%;
    background: cornsilk;
}

.clear {
    clear: both;
}
<!doctype html>
  <html>
  <head>
    <link rel="stylesheet" href="test.css" />
  </head>
  <body>
    <div id="test1"></div>
    <div id="test3"></div>
    <div id="test2"></div>
    <div class="clear"></div>
  </body>
</html>

Did this solution resolve the issue? Please let me know if there are any misunderstandings.

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

Modify the background color of a div by selecting a hex code from a dropdown menu

Is there a way to utilize jQuery in order to alter the background of a <div> by choosing the HEX code from a separate dropdown menu? HTML <select id="target"> <option value="#c2c2c2">Grey</option> <option value="blue">Bl ...

Strategies for making a child div fade out when the parent div is hovered over

I have a div with the class name ordershape and inside it, there is another div called fad-res. My goal is to display the corresponding fad-res when I hover over a specific ordershape, while hiding the other divs. <div class="ordershape"> & ...

A tool that enhances the visibility and readability of web languages such as HTML, PHP, and CSS

Looking to organize my own code examples, I need a way to display my code with syntax highlighting. Similar to how Symfony framework showcases it on their website: http://prntscr.com/bqrmzk. I'm wondering if there is a JavaScript framework that can a ...

AJAX File Upload: Sequentially Queuing Multiple Files

I am a beginner in the world of Javascript and jQuery. When I try to upload multiple files through a form, only the last file gets uploaded repeatedly. My goal is to upload each file one by one using AJAX requests asynchronously. Here's how I have ...

Using Vue to toggle an active HTML class on click: A tutorial

Can someone assist me with this code snippet that is intended to toggle a class using a Vue on-click event? I'm encountering issues as it's not working and I'm seeing several errors in the console. Any help would be appreciated. <div id=& ...

Incorporating various elements within a single column of a table

I have been attempting to include multiple table cells elements beneath each of my heading cells, but I am facing issues in getting it to work properly. For better understanding, this is a screenshot: https://i.sstatic.net/kg2XR.png My goal is to have o ...

Retrieve data from backend table only once within the bootstrap modal

How can I retrieve values from a table once a modal is displayed with a form? I am currently unable to access the values from the table behind the modal. Are there any specific rules to follow? What mistake am I making? I would like to extract the values ...

Is there a way to prevent a tag from being clickable on the entire row, except for the actual link itself?

My question is why, when using the a tag, the entire row on the right becomes clickable as well? Despite there being no link, the area at the back of the row on the right side is clickable. Is there any way to remove this clickable area? <a id="ba ...

Update the div element without needing to reload the entire page

Is there a way to reload a div tag without refreshing the entire page? I understand this question has been asked before, but I want to make sure I have a clear understanding. <p>click HERE</p> <div class="sample"> <?php functi ...

Dividing one SVG into Multiple SVGs

I'm facing a challenge with loading an SVG overlay on a Google Map. The SVG file is quite large, about 50mb, resulting in a delay of around 10 seconds for it to load in the browser. One solution I'm considering is splitting the SVG into 171 smal ...

What is the best way to maintain parameters in PHP form code?

I am facing an issue with a script that needs to run with a specific parameter (for example, details.php?studentid=10325). The script contains a form with the following code snippet to send form data back to the current script. However, for some reason, t ...

What is the best way to load an image file using JavaScript and store it in a separate directory?

Is there a way to transfer an image file from one directory to another by clicking a button using Javascript that will work on all browsers (IE, FF, Chrome)? I already have code for this: <input type="button" name="button" value="Move image to another ...

What is the reason that the larrow image is not set against a yellow background?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head> <style type="text/css"> *{padding:0px;margin:0px;} .strip {width:1000px;border:1px solid red;clear:none;height:10 ...

Can a div be aligned in the center with an "absolute" parent element?

<style type="text/css"> .square { width:251px; height:207px; border: 1px solid #d6d6d6; -webkit-box-shadow: 1px 1px 3px rgba(0,0,0,.1); -moz-box-shadow: 1px 1px 3px rgba(0,0,0,.1); box-shadow: 1px 1px 3px rgba(0,0,0,.1); ...

When an external image is dragged over, include the class in the drop area of the file input

Hey, does anyone know how to use Jquery to add a class when an element is being dragged over a drop area? Here's the HTML code I'm working with: <div class="upload"> <form action=""> <input class="uploadfile" type="file" ...

Change the code from a for-loop to use the Array#map method

I have been working on a simple JavaScript function and attempting to convert the code from using a for-loop to Array#map. I'm sharing my code in the following fiddle as I am currently learning about array map: http://jsfiddle.net/newtdms2/. function ...

Refresh the webpage source code for an AJAX request

When using AJAX calls on a page, I have noticed that the page source remains unchanged. This can be problematic if a user performs forward/backward operations in their browser, as the browser will display the original HTML code instead of the updated conte ...

Is it possible for CSS to prevent the insertion of spaces?

When filling out a form, I am able to insert spaces in inputs but not in the textarea (which is necessary). Interestingly, inserting spaces in the textarea works flawlessly. <form action="/#wpcf7-f519-o1" method="post" class="wpcf7-form" enctype="mu ...

How can I remove the color of a button in jquery after it's been clicked?

<button style="background-color:lightblue" id="extractv"> <b> Extract<br>v </b> </button> ... $("#extractv").click(function () { $("#extractv").removeAttr("style"); }); Upon clicking the button, my ...

Identify all unticked checkboxes using jQuery

Looking for help with checkboxes: <input type="checkbox" name="answer" id="id_1' value="1" /> <input type="checkbox" name="answer" id="id_2' value="2" /> ... <in ...