HTML/CSS: stretching height <div> layout - css

<div style="width: 800px; height: 600px">
<div style="height: 100px">
top fixed-height row
</div>
<div style="???">
bottom stretching row (the height should be 500px in this case.)
</div>
</div>
What's the standard way to create two-row layout without javascript where the top one is fixed-size and the bottom one stretches and fits to its parent div?
I would like both rows' styles are independent. I found a couple of solutions here, but if I change the height of the top row, the bottom row's style (e.g. margin) need to be changed along with it.
I don't need to support old browsers. If it's standard, it's just okay.
Thanks!

You can use display property in CSS to fix this.
working EXAMPLE
HTML
<div id="a" style="width: 300px; height: 200px">
<div id="b" style="height: 55%">
top fixed-height row
</div>
<div id="c" style="">
bottom stretching row (the height should be 500px in this case.)
</div>
</div> ​
CSS
#a
{
border: 1px solid black;
display:table;
}
#b
{
background-color:red;
display:table-row;
}
#c
{
background-color:green;
display:table-row;
}​

For this case you can use preprocesor (like LESS):
#contHeight: 600px;
#topHeight: 100px;
div#containingdiv {
width: 800px;
height: #contHeight;
}
div#toprow {
width: 100%;
height: #topHeight;
}
div#bottomrow {
height: #contHeight - #topHeight;
}

HTML and CSS are static not dynamic languages where you could perform calculations. You can only apply styles to each div individually because there is actually no dependency between the styles for each "row" div for you to determine the height of one based on the other.
However depending on what you are trying to achieve, it may be possible to style the containing div instead of the bottom div.
e.g. a very simplistic example where the top row should have a red background and bottom row have a yellow background, set the style for the containing div to have the appearance you require for the bottom div, and the top div will cover this appearance on the upper part of the container:
div#containingdiv { width: 800px; height: 600px; background:#ffff00; }
div#toprow { height: 100px; background:#ff0000; width 100%; }
div#bottomrow { }

I have just written a blog about HTML CSS layouts, the demo lets you manipulate most css settings via javascript.
http://attemptingawesomeness.blogspot.com.au/2012/05/css-html-layout-guide_13.html
http://attemptingawesomeness.blogspot.com.au/2012/05/ultimate-html-css-layout-demo.html

Related

CSS 100% div height with 960 grid

I have been banging my head against the wall trying to figure out this problem and I have looked high and low for the answer and came up with similar results.
Synopsis
The problem is that I am building a website using the 960 grid and have three columns that I want to stretch at 100% at all times. Here is a fiddle for your reference: http://jsfiddle.net/Uec7h/1/
Essentially the html is like so:
<div class="contentWrapper">
<div class="container_12">
<div class="grid_2 leftSide clearfix">
Left sidebar content.
</div>
<div class="grid_7 content">
Lots of content loaded from the server.
</div>
<div class="grid_3 rightSide">
Right sidebar content.
</div>
</div>
</div>
with the CSS being like
html, body {
height: 100%;
}
.content {
height: 100%;
}
.leftSide {
height: 100%;
background-color: #000000;
}
.rightSide {
height: 100%;
background-color: #000000;
}
.contentWrapper {
height: 100%;
}
The fiddle isn't completely accurate to what I am seeing on my local version, but it's close. Seems like the left and right sidebars do not want to expand to 100% no matter what I do.
What I've Tried
Most of the answers I have found on SO have suggested to put height: 100% on the html, body elements and everything should work out fine. Adding this attribute and giving both sidebars height: 100% did work a little bit, but if the content in the middle column gets too big, it stops at a certain point and won't continue to stretch.
I have tried adding the clearfix class that comes with the 960 grid but it didn't seem to help at all.
Question
How do I get the left and right side bars height in the fiddle to be 100% no matter what content is in the middle column?
If you add the following CSS to the sidebar elements it will fill the 100% of the height.
display:block;
height:auto;
position:absolute;
top:0px;
bottom:0px;
If you place the sidebar into a wrapper div with relative positioning, the content section will be again in it's right place...
I would also set padding and margin to 0 for the body.
EDIT:
If you add height: 100% to the .container_12 it will get a real height, and children elements can have a 100% height. Notice that the sidebars will be as height as the window itself, but your content at the middle can be taller than 100%... Fiddle
Dont know the 960 grid, the EDITED solution - using visibility: visible; -
HTML
<div id="box">
<div class="vision"> sdfsdfsd </div>
</div>
CSS
#box {
float: left;
border: 2px solid red;
}
.vision {
width: 300px;
height: 600px;
visibility: visible;
}

Make two divs side by side, one is resizable and the other has same height

I bring here a drama with css height.
I would like to make a layout, a div that contains 2 divs both in same line, one is resizable and the other must fit in the parent's height (same height as the first one).
The first div can have additional information (so i can't fix the height), so it will have more lines, the important is that it must not have a scroll bar. The second div must obey the first height, if it's bigger than it will have a scroll bar.
<div class='container'> <!-- parent -->
<div class='left'> <!-- resizable -->
</div>
<div class='right'> <!-- same height as left div -->
</div>
</div>
UNSOLVED CODE
The problem is that i can't figure out how to make it just using css. Or even it's possible just with css. I would not like to use js.
Someone please help me!
Fiddle
What you do is make the right one absolutely positioned which stops its height influencing the parent's.
RELEVANT CSS
.container {
background-color: gray;
display: table;
width: 70%;
position:relative;
}
.container .left{
background-color: tomato;
width: 35%;
}
.container .right{
position:absolute;
top:0px;
left:35%;
background-color: orange;
width: 65%;
height:100%;
overflow-y: auto;
}

CSS - aligning wrapped floating divs to the center

I am trying to create something like a gallery that shows different number of images per row based on the width of the browser. This has already been achieved using overflow: hidden in the outer div and float: left in the inner div.
However, what happens with this is that my images are always aligned to the left, leaving alot of whitespace on the right. How do I make it such that the gallery is always centered in the screen no matter how many images there are per row.
My code is on http://codepen.io/anon/pen/KzqAs
Thank you very much. :)
How about this: http://codepen.io/anon/full/mtBbF
HTML
<div class="container">
<div class="red box">red</div>
<div class="blue box">blue</div>
<div class="black box">black</div>
</div>
CSS
body{
text-align:center; /*You would need to define this in a parent of .container*/
}
.container{
display: inline-block;
margin: 0 auto;
text-align: left;
}
.box {
width: 300px;
height: 300px;
float: left;
}
Demonstration
You need to use an id(or class) on the main div. Set width: 300+px and margin: auto
Also your boxes should be with display: inline-block to allow them to begave "inline"
I have changed colors of the boxes a bit for better visibility.

CSS How to set div height to 100% minus some pixels

I'm trying to design a web page these days that is a bit hard.
I have three main divs. First one for header, another for footer, and third one for main content. Header and footer must be fixed in top and bottom of the page. Their place mustn't change with resizing of browser window. Third div must be in the blank space between those divs. It can resize to fit the page with window resize.
Height of main div must exactly change, because I want to place a Google Maps in that div, so the height of this div is important.
I tried so many things, but they were not successful. Setting height of the div to 100%(while height of body and html is 100%, too) was not the answer. Using a table (with three rows, two rows with fixed height, one row with variable height, with height="100%") had some problems, too(in IE8, when I declared a doctype, the div in second row (with height:100%) didn't fit the cell anymore!).
Now I have no idea to do this work. What can I do?
Note: I prefer not to use CSS3, because compatibility with old browsers is important for me.
You could try something like this.
HTML
<div id="header"></div>
<div id="body"></div>
<div id="footer"></div>
CSS
#header {
height:50px;
width: 100%;
background: black;
position: fixed;
z-index: 1;
top: 0;
}
#body {
height:100%;
width: 100%;
background: #CCC;
position: absolute;
z-index: 0;
}
#footer {
height: 50px;
width: 100%;
background: #0CF;
position: fixed;
bottom: 0;
}
Here is a fiddle - http://jsfiddle.net/6M59T/
Use a set height for your header, and use sticky footer to keep your footer a set height and aligned to the bottom as well. The space in between should then always be the right height.
You should try the well known Clearfix hack to handle height issues, because you need to "clear" parents elements to get that full 100% height you need.
This is one of the shortcomings of css. You cannot accomplish what you want using just those three divs. You need to use additional divs to offset the height of your header and footer. Here is how to solve this:
<body style="height:100%; margin:0; padding:0;">
<div id="header" style="height:50px; position: relative; z-index: inherit; background-color:lightblue;"></div>
<div id="content" style="height:100%; margin:-50px 0 -70px 0; background-color:wheat">
<div id="header-offset" style="height:50px;"></div>
<div id="footer-offset" style="height:70px;"></div>
</div>
<div id="footer" style="height:70px; background-color:lightblue;"></div>
</body>

Div expanding with overflow/scrollbars after other divs height

I need to do a website with divs. See the code snippet for the format. The MENU is of variable height, depending if the menu-items is rolled out or not, and the content is of variable height as well, but with a minimum of 700px. If the MENU is folded together to its min-height, it is 300px, and the BOX should take up the remaining space so MENU+BOX is the same height as the CONTENT. The content of BOX is 600px, so when BOX only gets 400px, there should be scrollbars. When the CONTENT div expands, the BOX should expand as well, so they stay the same height.
Here's what i got so far, but it is not working properly. I've tried some other stuff, but deleted it for this post so I only get the points shown instead. Hope you can help, and thank you in advance!
#container{ width: 800px; }
#leftbar{ float: left; width: 250px; background-color: lightgray; }
#content { float: left; width: 550px; background-color: white; }
#menu { width: 250px; }
#box { width: 250px; height: 300px; overflow-y: scroll; }
<div id="container">
<div id="leftbar">
<div id="menu">
<div id="box">
</div>
<div id="content"></div>
</div>
<div style="clear:both;"></div>
hi at the first look i can see you have some unclosed div tags, and probably that's why it doesn't do what you want. You can assign scroll bars by using java script, if you set overflow from the css it will have a scroll bar from the beginning

Resources