This question already has an answer here:
I have two divs with a height of 90%, but the display is different.
I have tried to put an outer div around them, but that has not helped. Also, it's the same on Firefox, Chrome, Opera, & Safari.
Can someone explain why I am having this problem?
Below is my code:
<div style="height: 90%">
<div ng-controller="TabsDataCtrl" style="width: 20%; float: left;">
<tabset>
<tab id="tab1" heading="{{tabs[0].title}}" ng-click="getContent(0)" active="tabs[0].active"
disabled="tabs[0].disabled">
</tab>
<tab id="tab2" heading="{{tabs[2].title}}" ng-click="getContent(2)" active="tabs[2].active"
disabled="tabs[2].disabled">
</tab>
</tabset>
</div>
<div id="leaflet_map" ng-controller="iPortMapJobController">
<leaflet center="center" markers="markers" layers="layers" width="78%"></leaflet>
</div>
</div>
Use
vh
(viewport height) instead of percentage. It will get the height of the browser and size it accordingly, e.g.try this code
with css
Working with the CSS
height
property and percentage valuesThe CSS
height
property, when used with a percentage value, is calculated with respect to the element's containing block.Let's say your
body
element hasheight: 1000px
. Then a child withheight: 90%
would get 900px.If you have not set an explicit height to the containing block (and the child is not absolutely positioned), then your child element with percentage height will have nothing to go on and height will be determined by content and other properties.
From the spec:
Hence, if you want to use percentage heights in your divs, specify the height of all parent elements, up to and including the root element (e.g.,
html, body {height:100%;}
)Note that
min-height
andmax-height
are not acceptable. It must be theheight
property.Here's a little summary:
Examples
Let's say you want a div to have 50% height of its parent.
This won't work:
Neither will this:
And neither will this:
This will fail, too:
NOW, it will finally work:
And this would work, as well:
But not this:
sample code
Use
100vh
As you can see, percentage heights are a bit tricky. You can avoid the complexity (i.e., never have to consider parent elements) by simply using viewport percentage heights. Whenever you want a box to be the height of the viewport, use
height: 100vh
instead ofheight: 100%
. Nothing else is needed.Absolute Positioning Exception
As noted in the spec, an absolutely positioned element is an exception to the rule for percentage heights. More details here: Applying 100% height to nested, non-flex elements.