Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

height: calc(100%) not working correctly in CSS

Tags:

css

I have a div that I want to fill the whole height of the body less a set number in pixels. But I can't get height: calc(100% - 50px) to work.

The reason I want to do this is I have elements that have dynamic heights based on some varying criteria, e.g. height of the header changes based on different elements it can contain. A content div then needs to stretch to fill the rest of the available space available.

The div element, however, stays the height of the content - it doesn't seem as if it interprets 100% to be the height of the body element.

body {    background: blue;    height: 100%;  }    header {    background: red;    height: 20px;    width: 100%;  }    h1 {    font-size: 1.2em;    margin: 0;    padding: 0;    height: 30px;    font-weight: bold;    background: yellow;  }    #theCalcDiv {    background: green;    height: calc(100% - (20px + 30px));    display: block;  }
<header>Some nav stuff here</header>  <h1>This is the heading</h1>  <div id="theCalcDiv">This blocks needs to have a CSS calc() height of 100% - the height of the other elements.</div>

I would appreciate any help or pointers in the right direction.

like image 901
elizabethmeyer Avatar asked Apr 23 '13 08:04

elizabethmeyer


People also ask

Why is not working on height in CSS?

Height % is based on it's parent (so you have to set every element above the target element to 100%) , there are a few workarounds to this though. For instance you can set it to height: 100vh; This will create the element to be 100% of your window height. Or you can use px instead.

How do I change my height to 100% in CSS?

height:100vh The . box class has only 100vh which is 100% of the viewport height. When you set the height to 100vh, the box element will stretch its height to the full height of the viewport regardless of its parent height.

Why is height percentage not working?

If set relatively, element's height will be relative to the height of the parent — if set. If the height of parent is not set — the height of the element will stay auto (like: 50% from auto is auto). That is why relative values of height usually don't work — because certain conditions must be met beforehand.


2 Answers

First off - check with Firebug(or what ever your preference is) whether the css property is being interpreted by the browser. Sometimes the tool used will give you the problem right there, so no more hunting.

Second off - check compatibility: http://caniuse.com/#feat=calc

And third - I ran into some problems a few hours ago and just resolved it. It's the smallest thing but it kept me busy for 30 minutes.

Here's how my CSS looked

#someElement {     height:calc(100%-100px);     height:-moz-calc(100%-100px);     height:-webkit-calc(100%-100px); } 

Looks right doesn't it? WRONG Here's how it should look:

#someElement {     height:calc(100% - 100px);     height:-moz-calc(100% - 100px);     height:-webkit-calc(100% - 100px); } 

Looks the same right?

Notice the spaces!!! Checked android browser, Firefox for android, Chrome for android, Chrome and Firefox for Windows and Internet Explorer 11. All of them ignored the CSS if there were no spaces.

Hope this helps someone.

like image 36
Kickass Avatar answered Oct 26 '22 17:10

Kickass


You need to ensure the html and body are set to 100% and also be sure to add vendor prefixes for calc, so -moz-calc, -webkit-calc.

Following CSS works:

html,body {     background: blue;     height:100%;     padding:0;     margin:0; } header {     background: red;     height: 20px;     width:100% } h1 {     font-size:1.2em;     margin:0;     padding:0;     height: 30px;     font-weight: bold;     background:yellow } #theCalcDiv {     background:green;     height: -moz-calc(100% - (20px + 30px));     height: -webkit-calc(100% - (20px + 30px));     height: calc(100% - (20px + 30px));     display:block } 

I also set your margin/padding to 0 on html and body, otherwise there would be a scrollbar when this is added on.

Here's an updated fiddle

http://jsfiddle.net/UF3mb/10/

Browser support is: IE9+, Firefox 16+ and with vendor prefix Firefox 4+, Chrome 19+, Safari 6+

like image 126
daamsie Avatar answered Oct 26 '22 15:10

daamsie