I don\'t know if it\'s a common problem, but I can\'t find the solution in web so far. I would like to have two divs wrapped inside another div, however these two divs insid
Float everything.
If you have a floated div
inside a non-floated div
, everything gets all screwy. That's why most CSS frameworks like Blueprint and 960.gs all use floated containers and divs
.
To answer your particular question,
<div class="container">
<!--
.container {
float: left;
width: 100%;
}
-->
<div class="sidebar">
<!--
.sidebar {
float: left;
width: 20%;
height: auto;
}
-->
</div>
<div class="content">
<!--
.sidebar {
float: left;
width: 20%;
height: auto;
}
-->
</div>
</div>
should work just fine, as long as you float:left;
all of your <div>
s.
Instead of using overflow:hidden
, which is a kind of hack, why not simply setting a fixed height, e.g. height:500px
, to the parent division?
This should do it:
<div id="wrap">
<div id="nav"></div>
<div id="content"></div>
<div style="clear:both"></div>
</div>
<html>
<head>
<style>
#main { border: 1px #000 solid; width: 600px; height: 400px; margin: auto;}
#one { width: 20%; height: 100%; background-color: blue; display: inline-block; }
#two { width: 80%; height: 100%; background-color: red; display: inline-block; }
</style>
</head>
<body>
<div id="main">
<span id="one">one</span><span id="two">two</span>
</div>
</body>
</html>
The secret is the inline-block
. If you use borders or margins, you may need to reduce the width of the div that use them.
NOTE: This doesn't work properly in IE6/7 if you use "DIV" instead of "SPAN". (see http://www.quirksmode.org/css/display.html)
Use this CSS hack, it saved me lot of trouble and time.
http://swiftthemes.com/2009/12/css-tips-and-hacks/problem-with-height-of-div-containing-floats-and-inline-lists/
I use it in every project.