如何基于文本内容创建具有动态宽度的div框? [英] How to create a div box with dynamic width based on text content?

查看:45
本文介绍了如何基于文本内容创建具有动态宽度的div框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的 div ,该代码根据所包含的内容进行收缩和扩展.

I want to create a simple div that shrinks and expands according to the content contained.

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?

推荐答案

尝试一下:

<div style="border: 1px solid red; padding: 15px; min-width:100px; display: inline-block">
  test text
</div>

此处是 display:inline & display:inline-block -很好的学习指南

Here's an explanation on display: inline & display: inline-block - a good read to learn

这篇关于如何基于文本内容创建具有动态宽度的div框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆