我想澄清是否有可能根据内容的大小调整div的大小,而不必使元素浮动或必须使其位置绝对。有可能吗?
答案 0 :(得分:112)
display
设置你当然是possible - JSFiddle proof of concept 可以看到所有三种可能的解决方案:
display: inline-block
- 这是您不知道的
position: absolute
float: left/right
答案 1 :(得分:20)
您可以使用display: inline-block
。
答案 2 :(得分:11)
您可以使用:
width: -webkit-fit-content;
height: -webkit-fit-content;
width: -moz-fit-content;
height: -moz-fit-content;
编辑:否。请参阅http://red-team-design.com/horizontal-centering-using-css-fit-content-value/
答案 3 :(得分:1)
您也可以使用
分词:打破所有;
当似乎没有任何效果时,它始终有效;)
答案 4 :(得分:1)
在Edge和Chrome上运行良好:
width: fit-content;
height: fit-content;