Creating a scrollable area inside a div that fills the parent container? - html

I'm trying to create something like the following:
Inside of a container, I want to have a fixed sized header, and then a scrollable div below it that fills all of the remaining space. I seem to be going about this wrong, as when I try setting the scrollable div's height to 100%, it pops out of the containers body.
Here's what i've come up with so far:
HTML
<div class="container">
<div class="top-part">
Hello!
</div>
<div class="scrollable-bottom">
<h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1
</div>
</div>
CSS
.container {
width: 400px;
height: 400px;
border: 1px solid #cacaca;
}
.top-part {
width: 100%;
height: 70px;
border: 1px solid #cacaca;
}
.scrollable-bottom {
height: 100%;
overflow-y: scroll;
background-color: rgba(255, 255, 247, 0.4);
}
If you checkout the fiddle here, you'll see that the scrollable-bottom shoots outside of the container. I can't figure out why this is..
Doesn't the height property refer to the parent elements height? If so, then why isn't it being constrained?

height: 100%; means 100% of the elements parent height, so if parent height is 400px, child height is going to be 400px as well.
You can get away from calculating sizes of your .scrollable-bottom on your own with use of calc() function. It's supported in modern browsers
You can also get rid of magic numbers by changing box model behavior of .top-part and .scrollable-bottom, setting box-sizing: border-box; for .top-part and .scrollable-bottom
It will change its behavior to contain paddings and borders inside the defined width/height of the box area instead of enlarging it by the padding and border width/height
This way, you no longer need to worry about changing .scrollable-bottom height/width whenever you add or remove padding or border to it or the .top-part
.container {
width: 400px;
height: 400px;
border: 1px solid #cacaca;
}
.top-part,
.scrollable-bottom {
box-sizing: border-box;
}
.top-part {
width: 100%;
height: 70px;
border: 1px solid #cacaca;
}
.scrollable-bottom {
height: calc(100% - 70px); /* parent height - .top-part height */
overflow-y: scroll;
background-color: rgba(255, 255, 247, 0.4);
}
<div class="container">
<div class="top-part">
Hello!
</div>
<div class="scrollable-bottom">
<h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1
</div>
</div>

You're absolutely correct. The 100% height refers to the full height of the container. But also note there is the top bar height that should be calculated here.
Giving it a negative top margin the same amount of the top bar should fix it, but it will hide a partial amount of the bottom content itself, that's why we add a top padding with the same amount of the height.
*{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.container {
width: 400px;
height: 400px;
border: 1px solid #cacaca;
}
.top-part {
width: 100%;
height: 70px;
border: 1px solid #cacaca;
position: relative;
z-index: 2;
background: white;
}
.scrollable-bottom {
height: 100%;
overflow-y: scroll;
padding-top: 70px;
margin-top: -70px;
background-color: rgba(255, 255, 247, 0.4);
}
<div class="container">
<div class="top-part">
Hello!
</div>
<div class="scrollable-bottom">
<h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1>
</div>
</div>

As your containers height is 400px and 70px height is covered by the header.
Just give max-height:330px to scrollable div. It will solve your problem. Here is the result http://jsfiddle.net/o2307qup/2/
There maybe some paddings, margins and borders that you might be playing with. So change the max-height according to them, if you are not seeing the perfect result. Also read the comment below of #Marc

Related

Text is deforming my div

The text is deforming my box.
here is what I have:
<div class="BigOne" ></div>
<div class="leftOne" ></div>
<div class="rightOne">This text is deforming the "leftOne"</div>
</div>
And the CSS:
.leftOne {
float: left;
width: 100%;
height: 100%;
border: 3px dashed #444;
border-radius: 7px;
box-sizing: border-box;
}
.rightOne {
float: left;
width: 100%;
height: 100%;
border: 3px dashed #444;
border-radius: 7px;
box-sizing: border-box;
}
How can I precent the text of right div for example, from deforming left div?
Thank you
What I understand from your question and explanation, you want the width of leftOne and rightOne to always be the same size regardless of BigOne's width. Then you'd want to change the width to 50% instead of 100% like so:
.leftOne, .rightOne {
float: left;
width: 50%; /* Change this to 50% instead of 100% */
height: 100%;
border: 3px dashed #444;
border-radius: 7px;
box-sizing: border-box;
}
Take note that you can refactor the CSS because both classes have the same styling. If you want to apply different styling to each of them, you can separate them back.
I also noticed that you have set the height of both of them to 100%. If you want them to maintain the same height, you need to specify the height of BigOne class. Eg:
.bigOne {
width: 100%; /*or whatever width you want to set it to */
height: 100px; /*or whatever height you want to set it to */
}
Another thing, you have an extra </div> on the first line. Remove it and your code should work.
<div class="bigOne">
<div class="leftOne"></div>
<div class="rightOne">This text is deforming the "leftOne"</div>
</div>
Hope that helps.

why does body height render as zero when it is specified as non-zero

Here is my code:
...
<STYLE>
body{
position: static;
height: 95%;
width: 95%;
border: 2px;
border-style: solid;
border-radius: 5px;
border-color: black;
background-color: pink;
}
<STYLE>
...
<body>
<DIV id="div_1" >
<DIV id="div_2a" >
</DIV>
<DIV id="div_2b" >
</DIV>
</DIV>
</BODY>
...
Although the height and width are specified as 95%, the width works but the height collapses, apparently because there is no content to the div's. This is totally counter-intuitive. To add
insult to the situation, the background color extends outside the border and fills the visible
page area. Is this a function of some sort of default body css declaration? If so what is it?
And how can I stop this behavior? If the css is changed to reference HTML the behavior is intuitive
in that the border doesn't collapse but the background color extends outside the border. At this
point it seems like a bug.
You specify the height of the body at 95%, meaning that it will take 95% of it's parents height. So, giving it's parent the htmltag a height, will also make your body take height.
html {
height: 100%;
margin: 0;
padding: 0;
}
body{
height: 95%;
width: 95%;
border: 2px;
border-style: solid;
border-radius: 5px;
border-color: black;
background-color: pink;
}

How to deal with child's overflowing border out of it's parent container?

I'm trying to figure out what are ways to stop borders from overflowing from it's parent container.
The only solution that I can come up is to set childs width and height by using calc() to calculate and subtract width and height of child's border.
Is there any better ways with dealing with this?
What solution would be suitable for pre-IE8?
Here's jsFiddle example.
CSS
.container {
width: 100px;
height: 100px;
margin: 0;
padding: 0;
clear: both;
background-color: purple;
}
.child {
width: 100%;
height: 100%;
border: 5px solid red;
}
HTML
<div class="container">
<div class="child">
text
</div>
</div>
you have set the width and height of child element to 100 percent so obviously it will be equal to the container one. Now setting a border,it will take extra width and height that will overflow.
so the first solution is the changing dimensions.
.child {
width: 90%;
height: 90%;
border: 5px solid red;
}
Note-90% is only in this case.
and the other solution is
.child {
width: 100%;
height: 100%;
border: 5px solid red;
box-sizing:border-box;}
As #dlev said:
One option is to set box-sizing: border-box; on the child, which forces the border to be considered part of the child element's "box", rather than just the content.
But even if you one to use calc css method then just deduct the border size:
.child {
width: calc(100% - 5px);
height: calc(100% - 5px);
border: 5px solid red;
}
demo

HTML/CSS: Remove vertical scroll with height: 100%;

I'm creating two columns that I want to fill the page. Very simple. However, I'm getting a very slight vertical scrollbar. Setting margin: 0 and padding: 0 on the html and body didn't fix it.
I've looked into overflow: hidden but I don't like it. I also looked into placing a clear:both div at the bottom, but that didn't do anything. I've looked into using min-height, but I can't seem to get it to work properly.
I have two questions:
Why is that vertical scrollbar appearing?
How can I remove the vertical scrollbar?
Live Example: http://jsfiddle.net/XrYYA/
HTML:
<body>
<div id="palette">Palette</div>
<div id="canvas">Content</div>
</body>
CSS:
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#palette {
float: left;
width: 300px;
height: 100%;
border: 1px solid black;
}
#canvas {
margin-left: 300px;
height: 100%;
border: 1px solid blue;
}
It's because of the 1px borders on each side of the element.
100% + 2px border(s) != 100%.
You could use box-sizing to include the borders in the height of the element.
jsFiddle example
div {
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
Alternatively, you could use calc() to subtract the 2px.
height: calc(100% - 2px);
jsFiddle example

CSS - div height

I am getting a little gap between child-div and its parent-div. Is it possible for child-div to on its parent-div height? or (the way around)possible if the parent-div can scope the height of its child-div for not to overlap or get some extra spaces.
I have a DOM like this:
<div class="parent-div">
<div class="parent-image">
</div>
<div class="child-div">
</div>
</div>
here is my CSS:
.parent-image:{
height:60px;
}
.parent-div{
border: 1px solid #E3E3E3;
border-radius: 4px 4px 4px 4px;
margin-bottom: 20px;
width: 100%;
}
.child-div{
????
}
If you specify height: 100%; it will take the height of the parent.
If your child has padding, you need to change its box-sizing.
.child {
padding: 10px;
box-sizing: border-box;
}
If your child has more content than the parent, you either need to tell it to scroll, or hide. Note that on some browsers the scroll-bar will be inside the div, and on other browsers, it'll be on the outside.
.parent.c .child {
overflow: auto;
}
or
.parent.d .child {
overflow: hidden;
}
Demo of All
In your CSS, you can set your child-div to:
.child-div{
height:100%;
}
Here is a demonstration: http://jsfiddle.net/Xq7zQ/