What is preventing BODY from respecting min-height: 100% ?

Struggling to create a basic webpage with a container that extends to the bottom? Here's what I'm aiming for:

  1. #Main should be as tall as the viewport, but able to stretch further with additional content.
  2. body should match the viewport height, yet accommodate more if needed.
  3. html needs to fill the viewport and expand with added content.

I've attempted various combinations of height and min-height properties without success. There seems to be an issue specifically with <body> not responding to min-height.

Take a look at this fiddle to see the problem: https://jsfiddle.net/r51tvu2b/

As you can see, the body doesn't acknowledge the min-height: 100% rule, leaving me incredibly frustrated.

Note: While I've come across some solutions that "work," they don't adjust properly when new content is added. In the fiddle, the red line should reach the bottom of the viewport initially, and then extend further down if the content increases.

Answer №1

I've made a couple of tweaks to the CSS and it seems like it's now working as you intended. Take a look at the Code snippet below:

html {
  position: relative;
  margin: 0;
  padding: 0;
}
body {
  position: relative;
  background: gray;
  margin: 0;
  padding: 0;
  /* changes made here */
  min-height: 100vh;
  border-bottom: 3px solid red;
}
#Main {
  /* changes made here */
  min-height: 100vh;
  width: 50%;
  margin: 0px auto;
  background: white;
}
<div id="Main">
    I SHOULD BE AT LEAST AS TALL AS THE VIEWPORT.
    <hr>
    Let's review:
    <br>HTML is min-height: 100% -- it's as least as tall as the viewport.
    <br>BODY is min-height: 100% -- it's as least as tall as HTML.
    <br>*I* am height: 100% -- WHY AM I NOT TALL
    <hr>
    Another WTF: the entire background is gray, yet the BODY's bottom doesn't stretch to the bottom.
    <hr>
    One more thing, when my contents grow, so should the body.
    <button onclick="document.getElementById('content').style.display='block';">
      Click me to add content.
    </button>
    <div id="content" style="height: 600px; background: green; display: none;">
      big content.
    </div>
  </div>

Hopefully this does the trick.

Answer №2

html {
  margin: 0;
  padding: 0;
}
body {
  position: relative;
  background: gray;
  border-bottom: 3px solid red;
}
#Main {
  min-height: 100vh;
  width: 50%;
  margin: 0 auto;
  background: white;
}
<body>
  <div id="Main">
    MY HEIGHT SHOULD BE EQUAL TO THE VIEWPORT.
    <hr>
    Let's recap:
    <br>HTML is set to min-height: 100% -- it is always as tall as the viewport.
    <br>BODY is set to min-height: 100% -- it should be as tall as HTML.
    <br>*I* am height: 100% -- WHY AM I NOT TALL
    <hr>
    Another issue: the entire background is gray, yet the bottom of the BODY does not stretch all the way down.
    <hr>
    And also, when my content expands, so should the body.
    <button onclick="document.getElementById('content').style.display='block';">
      Click me to add content.
    </button>
    <div id="content" style="height: 600px; background: green; display: none;">
      big content.
    </div>
  </div>
</body>

I recommend using 100vh which stands for 100% of the viewport height.

Answer №3

Utilizing flexbox, I have identified a solution that has widespread support among browsers. By defining HTML as display: flex; min-height: 100%, the flexbox container will expand to at least the size of the viewport. The body, acting as a child of the flexbox, will automatically adjust its height to fill the flexbox. If the contents within the body exceed the height of the viewport, the flexbox container will dynamically increase in height accordingly.

I am still puzzled by the fact that setting min-height: 100% on the body element does not produce the desired outcome, especially when the HTML document clearly contains content upon inspection. Even after experimenting with explicit height values, the results remain perplexing – for instance, the body may match the height of the viewport while the #Main element fails to reach the same height despite being set to height: 100% – a baffling scenario indeed.

The issue becomes further confounding when the background of the body spills beyond the actual content within the body itself.

Given the frustrating nature of these inconsistencies, I must refrain from delving deeper into CSS grievances before embarking on an extensive rant about its shortcomings.

html {
  min-height: 100%;
  display: flex;
  margin: 0;
  padding: 0;
}
body {
  flex-grow: 1;
  background: gray;
  margin: 0;
  padding: 0;
  border-bottom: 3px solid red;
}
#Main {
  height: 100%;
  width: 50%;
  margin: 0 auto;
  background: white;
}
<div id="Main">
    MY HEIGHT SHOULD AT LEAST MATCH THE VIEWPORT.
    <hr>
    Let's recap:
    <br>HTML has min-height: 100% - ensuring it is as tall as the viewport.
    <br>BODY also has min-height: 100% - ensuring its height matches HTML.
    <br>Yet, *I* am set to height: 100% - WHY AM I NOT TALL
    <hr>
    Another puzzling aspect: the entire background is gray, yet the bottom of the BODY does not extend fully downwards.
    <hr>
    One more anomaly occurs when my content expands, the body fails to keep pace.
    <button onclick="document.getElementById('content').style.display='block';">
      Click me to add content.
    </button>
    <div id="content" style="height: 600px; background: green; display: none;">
      large content.
    </div>
  </div>

Answer №4

Feel free to utilize the following code:

html {
            height:100%
        } 
        body {
            min-height:100%;
            position: relative;
            background: gray;
            margin: 0;
            padding: 0;
            border-bottom: 3px solid red;
        }
        #Main {
            height: 100%;
            width: 50%;
            margin: 0px auto;
            background: white;
        }
<div id="Main">
    I SHOULD BE AT LEAST AS TALL AS THE VIEWPORT.
    <hr>
    Let's review:
    <br>HTML is min-height: 100% -- it's as least as tall as the viewport.
    <br>BODY is min-height: 100% -- it's as least as tall as HTML.
    <br>*I* am height: 100% -- WHY AM I NOT TALL
    <hr>
    Another WTF: the entire background is gray, yet the BODY's bottom doesn't stretch to the bottom.
    <hr>
    One more thing, when my contents grow, so should the body.
    <button onclick="document.getElementById('content').style.display='block';">
    Click me to add content.
    </button>
    <div id="content" style="height: 600px; background: green; display: none;">
    big content.
    </div>
    </div>

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

Centered HTML Columns on the Screen

Figuring out this simple html + css issue is proving to be quite challenging for me. I am attempting to create a '3 column' layout. I envision a central column (approximately 10em in width) with two additional columns flanking it on each side (e ...

Utilizing AngularJS to include information into JSON-LD

As a newcomer to AngularJS, I find myself stuck in one of my projects. My goal is to convert the user-entered data in a form into the format: << "schema:data" >> and then push and display it within the @graph of the json-ld. Here are my HTML and Angular co ...

Identifying and detecting Label IDs when clicked using the for tag

I am facing an issue with labels and input fields in my code. I have multiple labels that trigger the same input field, but I want to know which specific label triggered the input field. <label id="label1" for="input1">Document1</label> <la ...

Can you explain the relationship between HTML 5 Canvas coordinates and browser pixels?

When trying to use HTML 5 canvas as a drawing board in my application, I encountered an issue. The drawings appear at an offset from the mouse pointer. My method involves using the Jquery .offset function to determine the event's position on the canv ...

Struggling with the transition of a CSS navigation menu from hover to focus

I'm attempting to transition a "mega menu" from 'hover' to 'focus'. My goal is to have the dropdown menus appear "on click" and remain visible until another top-level selection is clicked. Ideally, I am looking for a CSS-only solut ...

The navigation bar seems to be missing from the page

I am currently working on developing a responsive navigation bar with CSS animation. However, I am encountering some issues when trying to run the code on my laptop. Upon running it, only the checkbox appears while the list of navigation items is displayed ...

The hover effect flickers in Firefox, but remains stable in Google Chrome

Here is an example link to check out: I am facing a dilemma on how to solve this issue and unsure if it is related to jQuery or the DOM structure. If you have any suggestions, they would be greatly appreciated. Thank you! ...

Ensure that the HTML link is valid and authenticated using SESSIONS

When creating a website, one of the initial tasks I like to tackle is adding links at the bottom of the page for checking valid HTML and CSS: HTML5  •   CSS <div> <a href="http://validator.w3.org/check?uri=referer" ...

Tips for incorporating css @keyframes within a cshtml file:

My cshtml page includes a Popup that I created, but I encountered an issue with keyframes. When I tried to use it without keyframes, the fade effect was lost. I am looking for a way to fix my @keyframes. (I tested the code on Chrome and Opera) I found the ...

Correcting the reference to "/" (root) for assets after relocating the site to a subdirectory

I currently have a website located in the public_html directory, where all assets (images, css, js, etc) are referenced using /asset_folder/asset. The "/" at the beginning ensures that the browser starts from the root and navigates through the directories. ...

Is it possible for a mobile device to play .ogg videos?

Currently, I am setting up a server to stream videos using HTML. So far, I have tested playing both .mp4 and .ogg video formats on my computer successfully. However, when I attempted to play the same videos on a mobile device, only the .mp4 file showed u ...

What is the process for using AJAX and PHP to upload an image file?

I'm facing an issue where I want to insert an uploaded image into a database with user details for a profile picture. The output I receive currently shows {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}. How can th ...

Performing JavaScript functions after fetching an XSLT page via AJAX

Is there a way to trigger JavaScript at a particular time without relying on setInterval() or onClick()? The issue I'm facing is that when I click to load an XSLT page using Ajax, the JavaScript within it does not execute even though it's writt ...

What is the method to alter the fill of a circle using CSS when it is hovered over in JavaFX?

I am seeking assistance for a task involving a circle within an hBox. I would like the color of the circle to change when the mouse cursor hovers over it. Is there a way to accomplish this using JavaFX and CSS? If not, what is the recommended approach fo ...

Show a grid layout with adjustable sizing and elements centered in the middle

When dealing with a layout like the one below, how can we ensure that elements are centered within the wrap container? The margins around the elements should be flexible but at least 14px. .wrap{ display: grid; grid-template-columns: repeat(auto-fi ...

Refreshing Django HTML table dynamically upon database updates

Currently, I have an HTML table set up using a Django template to showcase data from my database. I am looking to ensure that this table remains updated in real-time whenever the database undergoes changes. Despite my research efforts, there is limited inf ...

Tips for effectively placing a page scope block using BEM

Here is the current structure of my header. The page component is assumed to be the root. Currently, the geometry of the social-links block is being controlled by the header__social-links mix (positioned absolutely relative to the header). How can I prop ...

How can we call a function in HTML from an external JavaScript file?

I am attempting to utilize a function that I have defined in my .js file within my HTML document. js/newsalerts.js function decodeHTML(html) { //https://stackoverflow.com/a/2989105/4650297 var tempDiv = document.createElement("DIV"); tempDiv. ...

Inserting lines into the text, creating a visually appealing layout

Can a notepad design be created using CSS only? Is it feasible to include lines between text lines without using underscores? I want it to look like this example but with minimal spacing between lines and no manual insertion of span tags since the text wi ...

scraping mixed content from an HTML span using Selenium and XPath

Currently, I am attempting to extract information from a span element that contains various content. <span id="span-id"> <!--starts with some whitespace--> <b>bold title</b> <br/> text here that I want to grab.... < ...