Why is this div's calculated height more than its content?
Turns out it was a line-height
issue:
#access {
background: #773736;
clear: both;
display: block;
margin: 0 auto;
width: 100%;
text-align: center;
line-height: 12px;/*this fixes it*/
}
Should also point out it was inheriting a line height from body of 1.625 (should be units here)
Div height 100% and expands to fit content
Here is what you should do in the CSS style, on the main div
display: block;
overflow: auto;
And do not touch height
Why does container div insist on being slightly larger than IMG or SVG content?
Trying adding:
img { display: block; }
to your CSS. Since an <img>
is an inline element by default, its height is calculated differently as related to the default line-height value.
On inline elements, the line-height CSS property specifies the height that is used in the calculation of the line box height.
On block level elements, line-height specifies the minimal height of line boxes within the element.
Source: https://developer.mozilla.org/en/CSS/line-height
Content overflows from div with 100% height
padding
will mess up height: 100%
. It seems to calculate the height and then add the padding so the resulting height is actually closer to 120%. I tried this code in a local html file and it seems to do the trick.
Try this instead:
<html>
<head>
<style>
body, html {
margin: 0;
padding: 0;
}
html {
background: url('http://losingmedotorg.files.wordpress.com/2013/04/two-roads-in-a-wood.jpg') no-repeat center center fixed;
background-size: cover;
}
#main {
margin-left: 20%;
margin-right: 20%;
background-color: white;
height: 100%;
}
#content {
padding: 10%;
}
</style>
</head>
<body>
<div id="main">
<div id="content">
<h1>The Road Not Taken</h1>
Two roads diverged in a yellow wood, And sorry I could not travel both And be one traveler, long I stood And looked down one as far as I could To where it bent in the undergrowth; Then took the other, as just as fair And having perhaps the better claim, Because it was grassy and wanted wear; Though as for that the passing there Had worn them really about the same, And both that morning equally lay In leaves no step had trodden black. Oh, I kept the first for another day! Yet knowing how way leads on to way, I doubted if I should ever come back. I shall be telling this with a sigh Somewhere ages and ages hence: Two roads diverged in a wood, and I - I took the one less traveled by, And that has made all the difference.
<p>- Robert Frost</p>
</div>
</div>
</body>
</html>
Why is percentage height not working on my div?
Use vh
(viewport height) instead of percentage. It will get the height of the browser and size it accordingly, e.g.
height:90vh;
try this code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id ="wrapper">
<div id="tabs" ng-controller="TabsDataCtrl">
<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"></leaflet>
</div>
</div>
</body>
</html>
with css
<style>
#wrapper{height:60vh;}
#tabs {width:20% float:left; height:60vh; overflow-y:scroll; overflow-x:hidden;}
#leaflet-map{width:78%; height:60vh; overflow-y:scroll; overflow-x:hidden;}
</style>
Div Different Height Than Span
The issue is that the span
element is an inline element. For these instances, change the span
to and inline-block
display:
span {
display:inline-block;
}
Here's some more reading:
css - inline elements ignoring line-height
Here's the updated fiddle:
https://jsfiddle.net/f2bmfkxg/1/
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>
Div not getting correct height with img content
It's because of the parent's line-height
. Either set the line-height
(or font-size
) of the parent to zero, or set the image to display: block
.
Related Topics
Use CSS to Alternate Ul Bullet Point Styles
Why Does Overflow: Hidden Add Additional Height to an Inline-Block Element
How to Add Multiple Classes to Markdown Using Jekyll
React Native Flatlist Separator Between Columns
Bootstrap Center 3 Col-Md-3 Divs
Responsive Media Query Not Working in Google Chrome
How to Stack Two Arrow Images (Upvote/Downvote) on Top of Eachother Using CSS
Sticky Scrollbar at Bottom of Table
CSS Style for Links Pointing to The Current Page
Auto-Contrast Font Color to Background
Internet Explorer Not Recognizing CSS for Svg Elements
How to Set Div Height to 100% of Chosen Print Paper
Bootstrap 4.0 Invalid-Feedback Doesn't Show
Absolutely Positioned Flexbox Doesn't Expand to Fit Contents
How to Make Entire Div Change Color on Hover Using CSS
How to Align Panelgrid to Center? Jsf-Primefaces
Linear-Gradient Using CSS3 Pie in Ie9 Not Working, Ie8 Does
Strange Border-Width Behavior in Chrome - Floating Point Border-Width