2011-10-07 9 views
0

はのは、次のユースケースを想定してみましょうスタイル:は、自動的に別のdiv要素内にあるdiv要素をセンタリングする方法

.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

は、外側のdivにtext-align:centerを追加するだけで正常に動作するようです

Jsfiddle:http://jsfiddle.net/HtfZ4/

+0

マイナス内側の要素は、(並んで)内部の2要素を持っていることを無視するので、同様

。すべての違いを生み出します。 – Joonas

関連する問題