I have several floats and a DIV around them. That DIV is inside another DIV, and is supposed to be horizontally centered inside it. The thing is that the inner DIV is not fixed-width, and can't be. Here is the code:
div.outer {
text-align: center;
}
div.inner {
display: inline-block;
width: auto;
}
div.floatdiv {
display: block;
float: left;
width: 270px;
height: 400px;
margin: 5px;
background-color: Gray;
text-align: center;
}
div.clearboth {
clear: both;
}
And the html:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="main.css" rel="stylesheet" type="text/css" static="screen" />
</head>
<body>
<div class="outer">
<div class="inner">
<div class="floatdiv">
Some text.
</div>
<div class="floatdiv">
Some text.
</div>
<div class="floatdiv">
Some text.
</div>
<div class="floatdiv">
Some text.
</div>
<div class="clearboth">
</div>
</div>
</div>
</body>
This works provided the browser window is wide enough for all floats to stay in one line. If the window is narrowed, then the last of the floats jumps to the next line of floats - the "inner" DIV becomes as wide as the containing block "outer" and suddenly inner is not centered in outer anymore.