3
<div id="wr">
    <div id="con">
        <div id="unknownWidth">some box with unknown width</div>
    </div>
    <div id="knownWidth"></div>
</div>

#wr {
    width:300px;
    height:100px;
    border:1px solid red;
    margin:50px;
}
#knownWidth {
    width:100px;
    height:30px;
    margin:0px auto;    
    border:1px solid gray;
}
#unknownWidth{
    height:30px;
    margin:0px auto;    
    border:1px solid blue;
}

Here is link to fiddle: http://jsfiddle.net/7EsMR/

Is it possible to avoid using display:table on #unknownWidth box and keep the same functionality as it offers?

Need it to work in IE7, but not IE6. :D

Required result: http://jsfiddle.net/7EsMR/1/

Thanks ;)

David Thomas
  • 249,100
  • 51
  • 377
  • 410
Somebody
  • 9,316
  • 26
  • 94
  • 142
  • Could be a duplicate of: http://stackoverflow.com/questions/283961/centering-a-div-block-without-the-width – sascha Dec 22 '11 at 14:34

3 Answers3

3

You can use a combination of display: inline on the child and text-align: center on the wrap:

http://jsfiddle.net/3snzb/2/

justisb
  • 7,081
  • 2
  • 26
  • 44
2

A combination of display:inline + text-align:center might do the trick:

http://jsfiddle.net/kG846/2/

ptriek
  • 9,040
  • 5
  • 31
  • 29
0

I guess you could do:

#wr {
    width:300px;
    height:100px;
    border:1px solid red;
    margin:50px;
    text-align: center; 
}
#knownWidth {
    width:100px;
    height:30px;
    margin:0px auto;    
    border:1px solid gray;
}
#unknownWidth{
    display:inline-block;
    height:30px;
    border:1px solid blue;
}

Should work in IE7 since inline-block is supported.

Paul Lernmark
  • 581
  • 1
  • 6
  • 17