Html 如何根据文本内容创建具有动态宽度的 div 框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34766562/
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
How to create a div box with dynamic width based on text content?
提问by membersound
I want to create a simple div
that shrinks and expands according to the content contained.
我想创建一个div
根据包含的内容缩小和扩展的简单。
https://jsfiddle.net/qa5dr0x8/
https://jsfiddle.net/qa5dr0x8/
<body>
<div style="border: 1px solid red; padding: 15px; width:auto;">
test text
</div>
</body>
Result: the red box expands to the full width of the page. Why?
结果:红色框扩展到页面的整个宽度。为什么?
回答by Darren Sweeney
Try this:
尝试这个:
<div style="border: 1px solid red; padding: 15px; min-width:100px; display: inline-block">
test text
</div>
Here's an explanation on display: inline
& display: inline-block
- a good read to learn