且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在中心对齐浮动div,其中父容器没有宽度?

更新时间:2022-11-02 14:19:22

这是最近的解决方案,而不使用javascript。
它是 display:inline-block; ,这使得div侧边栏和中心在同一时间。我试着将.parent变成 display:table-cell; 但没有工作。因此需要使用实际的< table> 来实现强大的中心行为。

This is the nearest solution without the use of javascript. It is display:inline-block; that makes divs side bar side and being in center at the same time. I've try turning .parent into display:table-cell; but didn't work. So need to use the actual <table> for the powerful centering behavior.

http://jsfiddle.net/Dgdhr/

编辑: http://jsfiddle.net/Dgdhr/1 (无表格:感谢MartinodF)

Edited: http://jsfiddle.net/Dgdhr/1 (without table: thanks to MartinodF)

<table width="100%">
<tr>
<td align="center">   
          <div class="child">1</div>
          <div class="child">2</div>
          <div class="child">3</div>
          <div class="child">4</div>
          <div class="child">5</div>
          <div class="child">6</div>   
</td>
</tr>
</table>

.child {
    width: 100px;
    height:100px;
    margin:10px;
    display:inline-block;
    background:#e0e0e0;
}