css - What's a cross-browser compatible way to right-align child divs and make the parent div get its width from the children? -


i have bunch of child <div>'s of variable width, want right-align within parent . want parent <div> no wider needs to contain children. (i don't know in advance how wide children -- they'll contain dynamically generated content.)

here's example works correctly in ie 8.0 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">&nbsp;</div>       <div class="childdiv" style="width: 50px">&nbsp;</div>       <div class="childdiv" style="width: 100px">&nbsp;</div>     </div>   </body> </html> 

if add float:right childdiv's css, works in firefox 3.5 not in ie 8.0 (parentdiv's width no longer determined width of children).

is there way can desired behavior in major browsers?

update: apparently adding float:right child divs produces error in ie when i'm hosting page in iis localhost. (which doing.) perhaps issue iis setting? i'm running iis 6.0

update #2: turns out iis 6 causing page load in ie7 standards mode. above code (with float:right added child divs) works ie8 , firefox, apparently not ie7.

i guess makes question: there simple way make work in ie7? (besides using conditional comment or css hack load different stylesheet in ie7, mean.)

edited jan 21, 2010 @ 21:00 mst : need float parent div right. floated child divs right, caused trouble in ie7. if have firebug, take @ this test page, has result you're after. tested in firefox 3.5, ie7, ie8, chrome, , safari 4.

here relevant css , html (i added margin/padding , background colors can more see effect):

   <style type="text/css">       #parent {          margin:0;          background:#ccc;          float:right;          padding:20px;       }       #parent div {          background:#eee;          margin:0 0 20px;          text-align:right;       }    </style>      ...     <div id="parent">       <div>nulla facilisi. suspendisse potenti. sed sed euismod tortor.</div>       <div>lorem ipsum, pharetra nec justo. in dapibus neque libero cursus laoreet nunc luctus.</div>       <div>lorem ipsum dolor sit amdolor.</div>    </div> 

my guess why original didn't work ie7 has number of documented bugs (see here list, includes links several float bugs). if float both parent , child elements right, desired results in ie8 , other modern browsers, in ie7 parent's width won't collapse width of widest child (enter mischievous bug).

ie7 behaves expected if float both parent , child elements left, (but isn't after).


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -