让 div 的大小适合其内容真的是不可能的吗?

我想澄清一下,是否有可能根据内容的大小使 div 适合它的大小,而不必使元素浮动或使它们的位置绝对。有可能吗?

157920 次浏览

CSS display setting

It is of course possible - JSFiddle proof of concept where you can see all three possible solutions:

  • display: inline-block - this is the one you're not aware of

  • position: absolute

  • float: left/right

You can use:

width: -webkit-fit-content;
height: -webkit-fit-content;
width: -moz-fit-content;
height: -moz-fit-content;

EDIT: No. see http://red-team-design.com/horizontal-centering-using-css-fit-content-value/

ALSO: http://dev.w3.org/csswg/css-box-3/

you can also use

word-break: break-all;

when nothing seems working this works always ;)

it works well on Edge and Chrome:

  width: fit-content;
height: fit-content;