如何使div不大於其內容?

個人佈局相似於: css

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

我但願div僅擴展到table寬度。 css3


#1樓

display: inline-block爲您的元素增長了額外的邊距。 瀏覽器

我建議這樣作: ide

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

獎勵:如今,您只需添加margin: 0 auto ,就能夠輕鬆地將新的#element居中。 佈局


#2樓

篡改Firebug時,我發現了屬性值-moz-fit-content ,該屬性值徹底符合OP的要求,能夠按如下方式使用: spa

width: -moz-fit-content;

儘管它只能在Firefox上運行,可是找不到其餘瀏覽器(例如Chrome)的等效項。 code


#3樓

我已經解決了一個相似的問題(我不想使用display: inline-block由於該項目居中),方法是在div標籤內添加一個span標籤,而後將CSS格式從外部div標籤移至新的內部span標籤。 只是把它扔掉,做爲另外一個可能的display: inline block對您來講不是一個合適的答案。 element


#4樓

我嘗試了div.classname{display:table-cell;} ,它起做用了! get


#5樓

您能夠嘗試fit-content (CSS3): it

div {
  width: fit-content; 
  /* To adjust the height as well */ 
  height: fit-content;
}
相關文章
相關標籤/搜索