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 inside have to be align the same level (for example: left one takes 20%width of the wrappedDiv, right one take another 80%). To achieve this purpose, I used the following example CSS. However, now the wrap DIV didn't wrap those divs all. The wrap Div has a small height than those two divs contained inside. How could I make sure that the wrap Div has the largest height as the contained divs? Thanks!!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>liquid test</title>
<style type="text/css" media="screen">
body
{
margin: 0;
padding: 0;
height:100%;
}
#nav
{
float: left;
width: 25%;
height: 150px;
background-color: #999;
margin-bottom: 10px;
}
#content
{
float: left;
margin-left: 1%;
width: 65%;
height: 150px;
background-color: #999;
margin-bottom: 10px;
}
#wrap
{
background-color:#DDD;
height:100%;
}
</style>
</head>
<body>
<div id="wrap">
<h1>wrap1 </h1>
<div id="nav"></div>
<div id="content"><a href="index.htm">< Back to article</a></div>
</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)
Float everything.
If you have a floated
div
inside a non-floateddiv
, everything gets all screwy. That's why most CSS frameworks like Blueprint and 960.gs all use floated containers anddivs
.To answer your particular question,
should work just fine, as long as you
float:left;
all of your<div>
s.Here i show you a snippet where your problem is solved (i know, it's been too long since you posted it, but i think this is cleaner than de "clear" fix)
Here's another, I found helpful. It works even for Responsive CSS design too.
ADDITION TO Meep3D's ANSWER
With CSS3, in a dynamic portion, you can add clear float to the last element by:
Where your divs are:
Reference:
:last-of-type
- CSS-TRICKSThis should do it:
overflow:hidden
(as mentioned by @Mikael S) doesn't work in every situation, but it should work in most situations.Another option is to use the
:after
trick:And for IE6: