我的布局类似于:

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

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


当前回答

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

我希望你觉得这很有用。

其他回答

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

我建议这样做:

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

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

只需将样式放入CSS文件

div { 
    width: fit-content; 
}
div{
  width:auto;
  height:auto;
}
width:1px;
white-space: nowrap;

对我来说很好:)

<table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td>
            <div id="content_lalala">
                this content inside the div being inside a table, needs no inline properties and the table is the one expanding to the content of this div =)
            </div>
        </td>
    </tr>
</table>

我知道人们有时不喜欢表格,但我必须告诉你,我尝试了css内联技巧,它们在一些div中有点奏效,但在其他div中没有,所以,将扩展的div封装在表格中真的很容易。。。和它可以有内联属性,也可以没有内联属性,但表仍然是保持内容总宽度的表。=)