如何让div自动根据内容调整宽度

发布于 2024-09-25 11:50:38 字数 265 浏览 1 评论 0原文

我想创建类似 fieldsetlabel 的效果。

alt text

当标题沿边变化时,标题白色背景的宽度保持不变。

如果我可以使 headingdiv 元素的宽度足以适应其内容,它将给出适当的效果。

如果有任何其他解决方案请告诉我。

I want to create an effect like that of fieldset and label.

alt text

When the headings change along the side, the width that the white background of the heading remains the same.

If I can make the heading or div element to take the width enough to fit its content, it will give the proper effect.

If there is any other solution please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-10-02 11:50:38

您可以在要缩小的 div 上使用 float ,或 display: inline-block (尽管这在 IE6 中不起作用(因为它应用 inline-默认情况下,仅对内联元素进行块),而不指定宽度。或者,显然,您可以同时使用两者,尽管我不确定为什么您想让浮动块表现得像 一样。内联

演示位于 jsbin

You can use float on the div you want to shrink, or display: inline-block (though that won't work in IE6 (since it applies inline-block only to elements inline by default) without specifying a width. Or you can use both, obviously. Though I'm not sure why you'd want to make a floated-block act as if it was inline.

Demo at jsbin

以为你会在 2024-10-02 11:50:38

我会推荐这个

#element {
    display: table; /* IE8+ and all other modern browsers */
}

I would recommend this

#element {
    display: table; /* IE8+ and all other modern browsers */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文