2011-10-07 88 views
0

让我们假设下面的用例: 在此link如何自动居中div元素是另一个div元素里面

的HTML代码的工作示例普莱舍看看:

<div class="container"> 
    <div class="centerElem"> 
     the width of this div could be variable 
    </div> 
</div> 

的CSS风格:

.container { 
    width: 500px; /*this can also change */ 
} 
.container .centerElem{ 
    margin-right: auto /*unfortunately, it works only if I set properly the width*/ 
    margin-left: auto /*unfortunately, it works only if I set properly the width*/ 
} 

现在,它的工作原理是这样的:

 ||the width of this div could be variable|       | 

而且我想作这样的工作,不知道内部div元素的宽度:

 |    |the width of this div could be variable|   | 

回答

0

添加text-align:center到外的div似乎只是正常工作

的jsfiddle:http://jsfiddle.net/HtfZ4/

+0

减去忽略内部部件具有内部(并排)2层的元件。使所有的差异。 – Joonas