Html 真的不可能让一个 div 的大小适合它的内容吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5903112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:27:08  来源:igfitidea点击:

Is it really impossible to make a div fit its size to its content?

htmlsizewidth

提问by Gabriele Cirulli

I'd like to clarify whether it's possible or not to make a div fit its size based on the content's size without having to make elements float or having to make their position absolute. Is it possible?

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

回答by Robert Koritnik

CSS displaysetting

CSSdisplay设置

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

这当然是可能的 - JSFiddle 概念证明,您可以在其中看到所有三种可能的解决方案:

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

  • position: absolute

  • float: left/right

  • display: inline-block-这是你不知道的

  • position: absolute

  • float: left/right

回答by Matt Ball

You can use display: inline-block.

您可以使用display: inline-block.

回答by The_HTML_Man

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/

编辑:不。见http://red-team-design.com/horizo​​ntal-centering-using-css-fit-content-value/

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

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

回答by meghaM

you can also use

你也可以使用

word-break: break-all;

断字:断断续续;

when nothing seems working this works always ;)

当似乎没有任何效果时,这总是有效;)