我的布局类似于:

<div>
    <table>
    </table>
</div>

我希望div只扩展到表的宽度。


当前回答

仅仅

<div style="display: inline;">
    <table>
    </table>
</div>

其他回答

我在一个div中有一个span,只需在容器div中设置margin:auto就可以了。

对我有用的是:

display: table;

(在Firefox和Google Chrome上测试)。

display:inline块为元素添加额外的边距。

我建议这样做:

#element {
    display: table; /* IE8+ and all other modern browsers */
}

奖金:你现在只需添加margin:0auto,就可以轻松地将这个新奇的#元素居中。

我只需要设置填充:-whateverYouWantpx;

只需使用display:inline;(或空白:nowrap;)。

我希望你觉得这很有用。