I have a bunch of child <div>
's of variable width, which I want to right-align within a parent . I also want the parent <div>
to be no wider than it needs to be to contain the children. (I don't know in advance how wide the children will be -- they'll contain dynamically generated content.)
Here's an example that works correctly in IE 8.0 but not in Firefox 3.5 (child <div>
's aren't right-aligned):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
#parentDiv{float:left; text-align:right; background-color: gray;}
.childDiv{clear:both; border: 1px solid black; background-color: white;}
</style>
</head>
<body>
<div id="parentDiv">
<div class="childDiv" style="width: 25px"> </div>
<div class="childDiv" style="width: 50px"> </div>
<div class="childDiv" style="width: 100px"> </div>
</div>
</body>
</html>
If I add float:right
to the childDiv's CSS, then it works in Firefox 3.5 but not in IE 8.0 (parentDiv's width is no longer determined from the width of its children).
Is there a way I can get the desired behavior in all major browsers?
UPDATE: Apparently the adding float:right to the child divs only produces the error in IE when I'm hosting the page in my IIS localhost. (Which is what I was originally doing.) Perhaps this is an issue with some IIS setting? I'm running IIS 6.0
UPDATE #2: So it turns out IIS 6 was causing the page to load in IE7 Standards mode. So the above code (with float:right
added to the child divs) works for IE8 and Firefox, but apparently not for IE7.
I guess that makes the question: Is there a simple way to make this work in IE7? (Besides just using a conditional comment or CSS hack to load a different stylesheet in IE7, I mean.)