Why doesnt percentage height work in HTML/CSS?
Asked Answered
R

7

228

I am trying to set a <div> to a certain percentage height in CSS, but it just remains the same size as the content inside it. When I remove <!DOCTYTPE html> however, it works; the <div> taking up the whole page as desired. I want the page to validate, so what should I do?

I have this CSS on the <div>, which has an ID of page:

#page {
    padding: 10px;
    background-color: white;
    height: 90% !important;
}
Religious answered 25/10, 2009 at 20:50 Comment(3)
Here's a simple and clear explanation of the CSS height property with percentage values: https://mcmap.net/q/36844/-why-is-percentage-height-not-working-on-my-div-duplicateSosthena
addresses the DOCTYPE issue: https://mcmap.net/q/37037/-why-does-height-100-work-when-doctype-is-removedSosthena
Have a look at this post that gives an answer based on CSS specs: https://mcmap.net/q/49259/-css-why-doesn-t-percentage-height-work-duplicateElinorelinore
U
432

I am trying to set a div to a certain percentage height in CSS

Percentage of what?

To set a percentage height, its parent element(*) must have an explicit height. This is fairly self-evident, in that if you leave height as auto, the block will take the height of its content... but if the content itself has a height expressed in terms of percentage of the parent you've made yourself a little Catch 22. The browser gives up and just uses the content height.

So the parent of the div must have an explicit height property. Whilst that height can also be a percentage if you want, that just moves the problem up to the next level.

If you want to make the div height a percentage of the viewport height, every ancestor of the div, including <html> and <body>, have to have height: 100%, so there is a chain of explicit percentage heights down to the div.

(*: or, if the div is positioned, the ‘containing block’, which is the nearest ancestor to also be positioned.)

Alternatively, all modern browsers and IE>=9 support new CSS units relative to viewport height (vh) and viewport width (vw):

div {
    height:100vh; 
}

See here for more info.

Uncivilized answered 25/10, 2009 at 21:17 Comment(3)
Although not working for portrait screen orientation #23198737Fecal
Please see the answer by Brian Campbell below I believe that is the correct solution. This answer seems to be a workaround and does not address the actual issue.Contrapose
Okay then why jsfiddle.net/8dkzp49w/16 this example don't create catch 22 for width? :/Munafo
R
80

You need to set the height on the <html> and <body> elements as well; otherwise, they will only be large enough to fit the content. For example:

<!DOCTYPE html>
<title>Example of 100% width and height</title>
<style>
html, body { height: 100%; margin: 0; }
div { height: 100%; width: 100%; background: red; }
</style>
<div></div>
Raama answered 25/10, 2009 at 21:15 Comment(5)
¬_¬ It took me 20 minutes to figure out I had to set the height of the document to 100%, too. I read this a bit too late, but it's very brilliant, still. sighKaunas
This should be voted up as answer as it gives a solution to lot of people coming here wondering what can be done most watch the first answer and think its not possible and wander off thinking of whether any other method exists without reading thisCrossjack
I agree this is the correct answer, the height does not work because parent element that is body and html needs to be set. This answer should be the accepted answer.Contrapose
This is not a correct answer - if the content is larger than will fit the height of the screen then the rest of the content is no longer visible anyway, thus the height has NOT been set to 100% of browser window but again to 100% of content. That's maybe not logical, but that is what does happen in the two main browsers, Firefox and Chrome - just try it out. Thus the accepted answer is the only correct one.Gadson
This is the thing that I have been missing for years: that the HTML and BODY tags also need a height: setting.Singles
O
19

bobince's answer will let you know in which cases "height: XX%;" will or won't work.

If you want to create an element with a set ratio (height: % of it's own width), use the aspect-ratio property. Make sure height is not explicitly set on the element for it to work. https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio

.square {
  width: 100%;
  height: unset;
  aspect-ratio: 1 / 1;
}

Historically, the best way to do this was to set the height using padding-bottom. Example for square:

<div class="square-container">
  <div class="square-content">
    <!-- put your content in here -->
  </div>
</div>

.square-container {  /* any display: block; element */
  position: relative;
  height: 0;
  padding-bottom: 100%; /* of parent width */
}
.square-content {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
}

The square container will just be made of padding, and the content will expand to fill the container. Long article from 2009 on this subject: http://alistapart.com/article/creating-intrinsic-ratios-for-video

Oneidaoneil answered 17/6, 2013 at 22:46 Comment(1)
this works great. It also feels fast in the browser whilst resizing a page holding 100+ elements. thanks!Aphrodite
P
8

In order to use percentage(%), you must define the % of its parent element. If you use body{height: 100%} it will not work because its parent have no percentage in height. In that case in order to work that body height you must add this in html{height:100%}

In other cases to get rid of that defining parent percentage you can use

body{height:100vh}

vh stands for viewport height

Phane answered 29/11, 2018 at 5:57 Comment(0)
C
6

You can use 100vw / 100vh. CSS3 gives us viewport-relative units. 100vw means 100% of the viewport width. 100vh; 100% of the height.

    <div style="display:flex; justify-content: space-between;background-color: lightyellow; width:100%; height:85vh">
        <div style="width:70%; height: 100%; border: 2px dashed red"></div>
        <div style="width:30%; height: 100%; border: 2px dashed red"></div>
    </div>
Carotenoid answered 26/10, 2016 at 6:58 Comment(0)
S
3

Sometimes, you may want to conditionally set the height of a div, such as when the entire content is less than the height of the screen. Setting all parent elements to 100% will cut off content when it is longer than the screen size.

So, the way to get around this is to set the min-height:

Continue to let the parent elements automatically adjust their height Then in your main div, subtract the pixel sizes of the header and footer div from 100vh (viewport units). In css, something like:

min-height: calc(100vh - 246px);

100vh is full length of the screen, minus the surrounding divs. By setting min-height and not height, content longer than screen will continue to flow, instead of getting cut off.

Sim answered 22/11, 2018 at 11:27 Comment(0)
D
1

With new CSS sizing properties you can get away with not setting exact height on parent. The new block-size and inline-size properties can be used like this:

<!DOCTYPE html>
<style>
  #parent {
    border: 1px dotted gray;
    height: auto;   /* auto values */
    width: auto;
  }

  #wrapper {
    background-color: violet;
    writing-mode: vertical-lr;
    block-size: 30%;
    inline-size: 70%;
  }

  #child {
    background-color: wheat;
    writing-mode: horizontal-tb;
    width: 30%;  /* set to 100% if you don't want to expose wrapper */
    height: 70%; /* none of the parent has exact height set */
  }
</style>

<body>
  <div id=parent>
    <div id=wrapper>
      <div id=child>Lorem ipsum dollar...</div>

Resize the browser window in full page mode. I think the values are relative to viewport height and width.

For more info refer: https://www.w3.org/TR/css-sizing-3/
Almost all browsers support it: https://caniuse.com/?search=inline-size

Doodlebug answered 19/11, 2021 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.