CSS:具有惰性宽度的元素

标签 css

假设我想制作一个 <p>具有固定高度和宽度的元素,只有当高度不足以显示所有文本时,宽度才会增加。默认情况下,<p>是 block 级元素。这意味着它有贪婪的宽度和惰性的高度。我想要相反的惰性动态宽度和固定/贪婪高度。如果可能,行内 block 元素会尝试在一行中显示文本,这不是我想要的。我想要一个纯 CSS 解决方案只是因为。可能吗?

最佳答案

这是不可能的。

9.4.2 Inline formatting contexts

In an inline formatting context, boxes are laid out horizontally [...]. The rectangular area that contains the boxes that form a line is called a line box.

The width of a line box is determined by a containing block and the presence of floats. [...]

In general, the left edge of a line box touches the left edge of its containing block and the right edge touches the right edge of its containing block. However, floating boxes may come between the containing block edge and the line box edge. Thus, although line boxes in the same inline formatting context generally have the same width (that of the containing block), they may vary in width if available horizontal space is reduced due to floats. [...]

Line boxes are created as needed to hold inline-level content within an inline formatting context. [...]

因此,行框的宽度只会受包含 block 的宽度和 float 的存在的影响。然后,将有尽可能多的线框。

关于CSS:具有惰性宽度的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29418841/

相关文章:

html - 将网站的 css 样式调整为 Django 脆皮表单复选框

html - 静态/相对 div 是否接受以 % 为单位的高度

css - 如何将样式从另一个元素应用到同级元素

html - 如何固定div及其内容?

css - 如何将背景图像放入文本框

css - 如何强制 ionic 幻灯片尊重列宽

javascript - 单击 anchor 链接使滚动回到顶部变得不可能,因为顶部消失

javascript - 如何通过操纵最高值来提升所有元素?

css - Bootstrap 更改行/列的顺序

css - float 时自动向图像添加填充