Why the div does not take the remaining space - html

I have a div structure like this,
<div style="height:100%">
<div style="height:50px"></div>
<div id="auto" style="height:100%"></div>
</div>
But it seems like id="auto" is taking the height as its parent height and the parent overflows. Can just set css in a way that id=auto div take the remaining height of the parent ?
What I'm trying to do is to make the id=auto div to take the rest of the space on parent div resize.
here is the jsFiddle

That's because percentage value for height property is relative to the height of box's containing block. Therefore 100% means the entire height.
10.5 Content height: the 'height' property
Specifies a percentage height. The percentage is calculated with
respect to the height of the generated box's containing block. If the
height of the containing block is not specified explicitly (i.e., it
depends on content height), and this element is not absolutely
positioned, the value computes to 'auto'. A percentage height on the
root element is relative to the initial containing block.
One solution would be using a negative margin for the second <div> element to remove the srcollbar and then adding position: relative; to the first one to bring it back on the top of the second one.
In this case we should use padding on top of the second div to push its content down and also adding box-sizing: border-box in order to calculate the height of the box including padding borders:
Example Here
<div class="parent">
<div class="top"></div>
<div class="content"></div>
</div>
.parent { height:100%; }
.top {
height: 100px;
position: relative;
}
.content {
background-color: gold;
min-height: 100%;
margin-top: -100px; /* equals to the height of .top element */
padding-top: 100px; /* equals to the height of .top element */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
It's worth noting that this approach would work on IE8+.
Nowadays all major web browsers support box-sizing: border-box, however you use a spacer element instead of padding+box-sizing to push the content of .content down:
Example Here.
<div class="content">
<div class="spacer"></div>
<!-- content goes here -->.
</div>
.spacer, .top {
height: 100px;
}
This approach would work on IE 6/7+(*)
Alternatively, you could nest the .top element within the .content and drop the .parent in order to achieve the same result which is working on IE 6/7+(*).
Example Here.
<div class="content">
<div class="top"></div>
<div class="inner">
<!-- content goes here -->
</div>
</div>
(*) IE6+ by using height property, IE7+ by using min-height.

If you don't need to support IE8 or IE9, use CSS calc (http://caniuse.com/calc)
<div style="height:100%">
<div style="height:50px"></div>
<div id="auto" style="height:calc(100%-50px);"></div>
</div>
If you do need to support the older IE's, then I would suggest using display:table, display:table-cell, and display:table-row. There are a lot of little quirks to keep in mind when using the table displays, so stick with calc if possible.

You can achieve the desired result, if you can absolutely position the first child div (the one that is 100 pixels tall):
Demo: http://jsfiddle.net/rn2Xe/2/
<div style="height:100%; padding-top:100px; box-sizing: border-box; position: relative;">
<div style="height:100px; position: absolute; top: 0; width: 100%; box-sizing: border-box;"></div>
<div style="height:100%;"></div>
</div>
Note: Use classes for CSS. Your code could be much cleaner.

You might be able to solve this with css calc, but if you want good legacy support, use tables.

Related

Bootstrap 3 column with 100% height

I have a page with following html markup:
<html>
<head>...</head>
<body>
<div class="container">
<header>...</header>
<div class="wrapper">
<div class="row">
<div class="col-md-8">...</div>
<div class="col-md-3 col-md-offset-1 col-aside">
<aside>...</aside>
</div>
</div>
</div>
</div>
</body>
</html>
I set 100% height for:
html, body, body > .container {
height: 100%;
}
.wrapper {
min-height: 100%;
}
.wrapper > .row {
min-height: 100%;
}
.col-aside {
min-height: 100%;
}
So I want that both my columns have minimum height of 100%. While inspecting my page with chrome developer tools I realized that .row and .col-aside don't get 100% height. I am a little bit lost because I saw answers dealing with display: table but I'm pretty sure it's not necessary since I managed to do this layout without bootstrap using just divs and their heights.
So have to stretch columns so that they have min-height: 100% preferably without using display: table and position: absolute?
updated: jsfiddle http://jsfiddle.net/U6H6W/ . Something like that, here you can see that .row doesn't get 100% height in spite of the fact that .wrapper gets 100%
A better solution would be to use viewport height, e.g.:
.col-aside {
height: 100vh;
}
You can easily specify the height of one div, without having to specify the height of the higher level HTML blocks.
Viewport is supported in all modern browsers, and as far back as IE9. IE8 does not support viewport, but if you need legacy support going back that far you can set a fallback to height: 100% (making sure you cover all of the containing blocks.)
Here is a jsbin to demonstrate:
http://jsbin.com/zeyuraka/1/edit
When dealing with heigth:100%, all the parents should be in height:100%. If one isn't, then no child is.
The 100% approach is complicated, especially when it comes to many childs with padding or margins. It could not display what you expect (i.e. exceed screen size).
You could try position: fixed, with bottom:0, but you will have to handle the position of the non-fixed div.

Div doesn't have height

Why does wrapper div not have a height? If I set the height (height:200px) the green background appears but how to set with auto height?
Here is my code (JSFiddle):
HTML:
<div class="wrapper">
<div class="effect"></div>
<div class="content">
...content
</div>
</div>
CSS:
.content {
position: absolute;
background-color:red;
}
.wrapper, .effect {
background: green;
}
.wrapper {
position: relative;
width: 630px;
}
.effect {
width:100%;
position: absolute;
}
It is not working (i.e. parent element not having any height) because all the immediate descendant of the .wrapper element is absolutely positioned — this will have the effect of taking them out of the flow of the document, therefore causing the parent's dimension to collapse to nothing.
You will also notice that the effect is the same when you float all
descendants of the parent wrapper, because float also has the
effect of taking normal elements out of the document flow.
There are only two ways to prevent this from happening, both of which involving declaring a certain height for the parent .wrapper element:
Either you explicitly state a height for the parent (see example fiddle)
Or use a relative height (say, in percentages or viewport units) that is not dependent on its own content.
You should reconsider your design strategy, and what you're trying to achieve. There is probably other ways to achieve what you intend to do, will you mind showing us?

How to set the height of divs in basic layout

I'm trying to set the size of 2 divs to fill the page with a 70 - 30 % ratio.
Without setting the size of the "html ,body" how can i get the divs to display to the correct height.
Currently it displays two single lines the height of the text. Thanks
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<div style="overflow: hidden; clear: both;">
<div style="background-color: blue; height: 70%;">Top</div>
<div style="background-color: red; height: 30%;">bottom</div>
</div>
</body>
</html>
You need to make the body and html elements have height:100%, and you need to give the outer div height: 100%.
CSS:
body, html { height: 100%}
<div style="overflow: hidden; clear: both; height: 100%">
<div style="background-color: blue; height: 70%;">Top</div>
...
You cannot do this with CSS, for a good reason. If you don't set a height to the body, it's height will become as high as it needs to be to accommodate all of its children. Now, if you use percentage-based units for your children's height, the children's height will be calculated based on the height of its parent.
So, the parent's height would depend on the height of its children, and its children's height would depend on the height of the parent - infinte loop!
P.S. Fred's method works, in case your concern about setting the height revolved around setting a static height. Setting the height to 100% might solve your dilemma.
You can add a position: absolute to the parent div and subsequently stretch it to achieve full width and height. Note that the width: 100% declarations are important to enforce block-level formatting context.
<div style="position:absolute; overflow: hidden; top:0; left:0; right: 0; bottom: 0;">
<div style="background-color: blue; height: 70%; width: 100%;">Top</div>
<div style="background-color: red; height: 30%; width: 100%;">bottom</div>
</div>
Here's the fiddle
Just note that this will remove this div from 'normal flow', and that sibling elements will be obscured/obscuring. The CSS 2.1 spec provides this advice:
...the contents of an absolutely positioned element do not flow around any other boxes. They may obscure the contents of another box (or be obscured themselves), depending on the stack levels of the overlapping boxes.
Unfortunately, you need to assign a fixed height to the DIVs parent in order for the 70% - 30% ratio to work.
One thing you can do is use JavaScript to get the height of the window, and then assign this value to the parent DIV. In this way, the percents will work, since it have a reference of how it should re-size.

CSS: make middle div take whole free space of the window height (min-height)

Can anyone help me with position my content block?
It looks good if there are a lot of content, but not when window higher than content block.
Actualy I need that "content" block on my picture teked all free space (height) and thats why footer stick to the bottom.
I have next HTML markup:
<div>
<header></header>
<nav class="breadcrumbing"></nav>
<section class="left_nav"></section>
<section class="content"></section>
<footer></footer>
</div>
With this CSS:
html,body{width:100%;margin:0;padding:0;}
body{background-color:#629302}
body>div{width:400px;height:100%;margin:0 auto;background-color:#FFF;}
body>div>header{height:50px;background-color:#9dc155}
body>div>nav.breadcrumbing{display:block;height:10px;margin:0;padding:0;}
body>div>section.left_nav{width:172px;margin:8px 20px;float:left;background-color:#cdef88}
body>div>section.content{width:168px;float:left;}
body>div>footer{padding:19px 19px 22px;background-color: #e58b04;clear:left;}
I allready tried answers from Is it possible to get a div's height to be 100% of an available area? and some same questions but with no luck.
ALso my live HTML has backgroun-images, so I can't just put footer to the bottom with position:absolute.
There I post my HTML to preview: jsfiddle.
UPD: scaled live preview:
You will have to set the html and body height property to 100%; then you can set the footer height to 100%; this will tell the main container elements the real meaning of 100% and it will work.
Your updated fiddle
Basically, these are the rules you have to add:
html, body {
height: 100%;
}
footer {
height: 100%;
}
Update
Ok, I might have misunderstood your requirements, here is a cleaner example:
Working example
Basically, what you additionally do in this example is having your wrapper element display:table with an height: 100%, then you make your footer display as table-row.
Important note: This solution uses display-table which is compatible only for IE8+. If supporting IE7 is an issue for you, then you have two solutions that I can think of out of my head:
Either you use a fixed-width footer, push it below the content and then pull it back with a combination of negative margin and padding.
Or you fallback to support of older browser by putting your footer in position using some javascript.
This the breakdown of the code:
HTML
<div class="wrapper">
<header></header>
<section class="main-content">
{child elements of your main-content area}
</section>
<footer></footer>
</div>
CSS
html, body {
height: 100%;
margin: 0;
}
.wrapper {
display: table;
margin: 0 auto;
height: 100%;
}
.main-content {
display: table-row;
height: 100%;
}
footer {
display: table-row;
}
Here's an updated fiddle
The crux of this is setting the body to be absolutely positioned to the viewport. From there, if you wanted to allow it to scroll as you normally would, then you would change the footer's position to fixed and the content div's CSS to this:
body>div>div{width:400px;height:100%;margin:0 auto;background-color:#FFF;
position:absolute; top: 0; bottom: 0; overflow-y:auto;}
I've wrapped your content div in another to allow for the automatic margins to center your page, and then defined the footer's box sizing as border-box to account for the padding you're adding to it as well.

Display-inline block and image

I am new in designing and so have some problems...
I need 3 block to be inline and centered, so I tried:
#main .block{
display: inline-block;
border: 1px solid #ECEDE8;
margin: 10px 10px;
overflow: hidden;
height: 265px;
width: 265px;
}
But, when i add an image in to the block, all others goes down.
P.S.
As I see, this problem is in safari, in Firefox all ok.
P.S.S
<div id="main">
<div class="block">main
<img src="style/images/try.png">
</div>
<div class="block">main</div>
<div class="block">main</div>
<div class="block">main</div>
<div class="block">main</div>
<div class="block">main</div>
</div>
P.S.S.S
As I could figure it out thought Google, all problem is in display: inline-block, in safari works display: inline-table. What solution could be?
You need to set the vertical align property. In this case best option would probably be:
vertical-align: top
So your css should be:
#main .block{
display: inline-block;
border: 1px solid #ECEDE8;
margin: 10px 10px;
overflow: hidden;
height: 265px;
width: 265px;
vertical-align: top;
}
If your blocks are fixed width, why not float them instead and put them in a parent container with a total width and centered using margin: 0 auto
The div element is a block element. The img element is an inline-block element; it has the main features of a inline element (except that it has the block element features of height and width). Therefore, I see two main problems with your code. Firstly, your image is somehow meant to replace the same unit of space as where you as simultaneously displaying the word "main" which can cause a conflict with spacing/display. [I recommend deleting the word "main" from your HTML.] Secondly, you don't specify the height and/or width of the image, which is not really a problem since you have overflow value set to hidden, but still generally you should have a height and/or width value assigned (make them less than the parent container's height and width) as it will make the browsers rendering/displaying more universal/compatible cross-browsers. Implementing a combination of those two things [or of changing your display value to inline-table (which it kind of seems more like what you want since your display seems "tablular")] SHOULD fix your problem. If it doesn't, I would recommend changing your margin value to 0 auto (which will have the effect of centering your image in the middle of your div/parent container), because I have a lingering suspicion that your margin values may also be at play (but only if the other two suggestions don't work). Good luck!
PS: Remember that just because you have overflow: hidden doesn't mean that the browser doesn't "see" the element(s) that are overflowing. Remember the Box Model.
Try this -
<div class="main" id="main">
<div class="block">main
<img src="style/images/try.png">
</div>
</div>
<div id="main">
<div class="block">main</div>
</div>
<div id="main">
<div class="block">main</div>
</div>
<div id="main">
<div class="block">main</div>
</div>
<div id="main">
<div class="block">main</div>
</div>
And instead of #main .block{ just .main {