【问题标题】:Inherited text-indent value behavior when changing display value from block to inline-block将显示值从块更改为内联块时继承的文本缩进值行为
【发布时间】:2013-09-17 06:35:49
【问题描述】:

这是我的小提琴:http://jsfiddle.net/xxgkB/

我有一个容器<div>,它有一个孩子<p>

为什么将段落的显示值从块更改为行内块时,text-indent 的值会翻倍?

HTML:

<div class=container>
    <p>Example Paragraph</p>
</div>

CSS:

div {
    background: slategray;
    height: 2in;
    text-indent: 1in;
    width: 2in;
}

p {
    display: inline-block; /* Notice the change when removing this declaration */
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    text-indent 默认继承。当您使p 元素成为内联块时,它成为div 块的内联格式化上下文的第一行的一部分,因此缩进1 英寸。然后p 元素本身从div 元素继承text-indent 值,导致它自己的文本再缩进1 英寸。

    来自spec:

    注意:由于 'text-indent' 属性是继承的,当在块元素上指定时,它将影响后代的 inline-block 元素。因此,在指定为“display:inline-block”的元素上指定“text-indent: 0”通常是明智的。

    p 元素文本的第二行似乎也缩进了,因为缩进的是整个p 元素,而不仅仅是第一行。 when you give the p element its own background color 和 when you close the p element and add more text after it into the div 更清楚地说明了这一点。

    【讨论】:

    • @Adrift:没问题 :) 我碰巧喜欢回答这样的问题——我更倾向于解释性的答案,而不是“我如何在 CSS 中做到这一点?” (...除非是关于选择器的问题!)。
    • 我同意 - 我也对 why 比 how 更感兴趣,但不幸的是,这种方法在 SO 上似乎并不普遍。 . 哦,好吧:]
    • @Adrift:我也注意到了——我想这取决于问题的性质,以及你问的对象。查看this question on meta,并将其与this discussion 进行比较。
    猜你喜欢
    • 2015-11-24
    • 2019-03-25
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多