Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在页面中放置了一个 div,或者在页面中放置了任何内容,它会自动将其宽度扩展到末尾,就像我将宽度设置为 100% 一样。显然我需要为这个东西设置一个宽度值(比如 100px),但我只是希望它与内容一样宽,而不是固定的。文本越长,内容应该越宽。我该如何处理?怎么了?
默认情况下,宽度auto告诉块元素扩展到父元素的整个宽度。您希望或者display: table让它display: inline-block缩小到其内容的宽度。
auto
display: table
display: inline-block
您可以display: table在此处查看实际操作:
http://codepen.io/cimmanon/pen/FvGxl
你需要设置:
width: auto; display: inline-block;