我的布局类似于:

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

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


当前回答

您可以使用内联块作为@user473598,但要注意较旧的浏览器。。

/* Your're working with */
display: inline-block;

/* For IE 7 */
zoom: 1;
*display: inline;

/* For Mozilla Firefox < 3.0 */
display:-moz-inline-stack;

Mozilla根本不支持内联块,但他们有-moz内联堆栈,这几乎是一样的

内联块显示属性周围的一些跨浏览器:https://css-tricks.com/snippets/css/cross-browser-inline-block/

您可以在以下位置看到一些具有此属性的测试:https://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/

其他回答

width:1px;
white-space: nowrap;

对我来说很好:)

    .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>

在Firebug中我发现了属性值-moz fit content,它完全符合OP的要求,可以如下使用:

width: -moz-fit-content;

虽然它只在Firefox上运行,但我找不到其他浏览器(如Chrome)的等效版本。

我的CSS3 flexbox解决方案有两种风格:顶部的表现像span,底部的表现像div,在包装器的帮助下获取所有宽度。它们的类分别是“top”、“bottom”和“bottomwrapper”。

正文{字体系列:无衬线;}.顶部{显示:-webkit内联flex;显示:内联flex;}.顶部,.底部{背景色:#3F3;边框:2px实心#FA6;}/*底部包装器将占据其余宽度*/.底部包装{显示:-webkit flex;显示:柔性;}表{边界塌陷:塌陷;}表格,th,td{宽度:280px;边框:1px实心#666;}第{背景色:#282;颜色:#FFF;}标准差{颜色:#444;}第,天{填充:0 4px 0 4px;}这是吗<div class=“top”><表><tr>操作系统</th><th>版本</th></tr><tr><td>OpenBSD</td><td>5.7</td></tr><tr><td>Windows</td><td>请升级到10</标准差></tr></table></div>你在找什么?<br>或者可能是。。。<div class=“bottomwrapper”><div class=“bottom”><表><tr>操作系统</th><th>版本</th></tr><tr><td>OpenBSD</td><td>5.7</td></tr><tr><td>Windows</td><td>请升级到10</标准差></tr></table></div></div>这就是你要找的。

仅仅

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