我的布局类似于:

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

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


当前回答

我尝试了div.classname{display:table cell;},结果成功了!

其他回答

尝试显示:inline block;。为了实现跨浏览器兼容,请使用以下css代码。

第二部分{显示:内联块;显示:-moz内联堆栈;缩放:1;*显示:内联;边框样式:实心;边框颜色:#0000ff;}<div><表><tr><td>第1列</td><td>第2列</td><td>第3列</td></tr></table></div>

    .outer{
          width:fit-content;   
          display: flex;
          align-items: center;
    }
    .outer .content{
         width: 100%;
    }
        
        
        
        
<div class=outer>
    <div class=content>
       Add your content here


    </div>
        
</div>
div{
  width:auto;
  height:auto;
}
<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封装在表格中真的很容易。。。和它可以有内联属性,也可以没有内联属性,但表仍然是保持内容总宽度的表。=)

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