Why can't I make my div 100% height if I use an HTML5 doctype? How do I get it 100% height
Only if the parent element has a defined height, i..e not a value of auto
. If that has 100% height, the parent's parent height must be defined, too. This could go until to the html
root element.
So set the height of the html
and the body
element to 100%
, as well as every single ancestor element of that element that you wish to have the 100%
height
in the first place.
See this example, to make it clearer:
html, body, .outer, .inner, .content { height: 100%; padding: 10px; margin: 0; background-color: rgba(255,0,0,.1); box-sizing: border-box;}
<div class="outer"> <div class="inner"> <div class="content"> Content </div> </div></div>
Why does height 100% work when DOCTYPE is removed?
CSS height
property, percentage values & DOCTYPE
The first part of your question asking how to apply a 100% height to your div
has been answered several times by others. Essentially, declare a height on the root element:
html { height: 100%; }
A complete explanation can be found here:
- Working with the CSS
height
property and percentage values.
The second part of your question has received much less attention. I'll try to answer that.
Why does removing the
doctype
make [the green background] work?
When you remove the DOCTYPE (document type declaration) the browser switches from standards mode to quirks mode.
In quirks mode, also known as compatibility mode, the browser simulates an old browser so it can parse old web pages – pages authored before the advent of web standards. A browser in quirks mode pretends to be IE4, IE5 and Navigator 4 so it can render old code as the author intended.
Here's how Wikipedia defines quirks mode:
In computing, quirks mode refers to a technique used by some web
browsers for the sake of maintaining backward compatibility with web
pages designed for older browsers, instead of strictly complying with
W3C and IETF standards in standards mode.
Here's MDN's take:
In the old days of the web, pages were typically written in two
versions: One for Netscape Navigator, and one for Microsoft Internet
Explorer. When the web standards were made at W3C, browsers could not
just start using them, as doing so would break most existing sites on
the web. Browsers therefore introduced two modes to treat new
standards compliant sites differently from old legacy sites.
Now, here's the specific reason why the height: 100%
in your code works in quirks mode but not in standards mode:
In standards mode, if the parent element has a height: auto
(no height defined), then the percentage heights of child elements will also be treated as height: auto
(as per the spec).
This is why the answer to your first question is html { height: 100%; }
.
For height: 100%
to work in your div
, you must set a height
on parent elements (more details).
In quirks mode, however, if the parent element has a height: auto
, then the percentage heights of child elements are measured relative to the viewport.
Here are three references covering this behavior:
- https://www.cs.tut.fi/~jkorpela/quirks-mode.html
- https://stackoverflow.com/a/1966377/3597276
- https://developer.mozilla.org/en-US/docs/Mozilla_Quirks_Mode_Behavior
TL;DR
Here's what developers need to know in a nutshell:
A browser in quirks mode will render web pages in a way that is
unpredictable, unreliable and often undesirable. So always include a
DOCTYPE for the document to render in standards mode.Selecting the right DOCTYPE used to be an ambiguous and somewhat
confusing process with many DOCTYPE versions to choose from. But
today the process is as simple as ever. Just use:<!DOCTYPE html>
Why doesn't height: 100% work to expand divs to the screen height?
In order for a percentage value to work for height, the parent's height must be determined. The only exception is the root element <html>
, which can be a percentage height. .
So, you've given all of your elements height, except for the <html>
, so what you should do is add this:
html {
height: 100%;
}
And your code should work fine.
* { padding: 0; margin: 0; }html, body, #fullheight { min-height: 100% !important; height: 100%;}#fullheight { width: 250px; background: blue;}
<div id=fullheight> Lorem Ipsum </div>
Why does min-height:100% not work since declaring a doctype?
When you remove the doctype the browser goes into quirks mode which does things differently to help older code that is not validated to render correctly.
Because ancient browsers had odd, inconsistent behavior and browsers treat Doctypes like an intelligence test to see if the author is writing code to the standards or to what they learned from W3Schools a decade ago.
If you have height: 100% and the height of the parent element is auto then 100% means auto.
Only if the parent element has a defined height, i.e not a value of auto. If that has 100% height, the parent's parent height must be defined, too. This could go until to the html root element.
So set the height of the html and the body element to 100%, as well as every single ancestor element of that element that you wish to have the 100% height in the first place.
html, body {
margin: 0;
padding: 0;
border: none;
height: 100%;
}
#mydiv {
height: 100%;
}
Make body have 100% of the browser height
Try setting the height of the html element to 100% as well.
html,
body {
height: 100%;
}
Body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well.
However the content of body will probably need to change dynamically.
Setting min-height to 100% will accomplish this goal.
html {
height: 100%;
}
body {
min-height: 100%;
}
Related Topics
CSS Selectors - Selecting a Specific Child Element
Ionic 2 How to Customize Ion-Range
Less: How to Pass a Mixin as an Argument to Another Mixin
How to Make a Scrollable Element Fill 100% of the Remaining Height of a Parent Element
How to Write a Caption Under an Image
How to Line Up 3 Divs on The Same Row
CSS Text Ellipsis When Using Variable Width Divs
Why Can't I Make My Div 100% Height If I Use an HTML5 Doctype? How to Get It 100% Height
How to Add a Search Box with Icon to The Navbar in Bootstrap 3
Stacking Divs on Top of Each Other
Div Side by Side Without Float
Difference Between <Span> and <Div> with Text-Align:Center;
Overflow:Hidden Not Working as Expected in Google Chrome
How to Have an Image Placeholder in Angularjs
Arabic Ttf (Truetype Font) in Uiwebview iOS 4.2.1
CSS Display Property When a Float Is Applied
:Before and :After Pseudo Elements on HTML Tag Is Wonky in Chrome