【问题标题】:Why does the text-decoration: none not work inside p?为什么 text-decoration: none 在 p 中不起作用?
【发布时间】:2017-04-11 06:51:31
【问题描述】:

我有以下 HTML 和 CSS sn-p,我想要“!”不必强调,但确实如此。我做错了什么?

p {
  color:red; 
  text-decoration: 
    underline; 
  font-size: 1.2em;
}

span.none {
  text-decoration: none;
}
<p class="p">Click the thumb<span class="none">!</span></p>

【问题讨论】:

    标签: html css text-decorations


    【解决方案1】:

    为什么 text-decoration: none 在 p 中不起作用?

    tldr; 当文本装饰被传播到其后代元素时,它们无法撤消,但是在某些情况下,它们不会被传播到它们的后代。

    这个确切的例子记录在MDN:(强调我的)

    文本装饰跨越后代元素。这意味着它 无法在后代上禁用文本装饰 在其祖先之一上指定。

    例如,在标记中:

    <p>This text has <em>some emphasized words</em> in it.</p>,

    样式规则p { text-decoration: underline; } 将导致整个段落 下划线。样式规则em { text-decoration: none; } 不会 造成任何改变;整个段落仍会加下划线。

    但是,有时文本装饰不会传播到它们的后代元素 - 请参阅“text-decoration”属性上的W3C spec

    请注意,文本装饰不会传播到浮动和 绝对定位的后代,也不是 atomic 的内容 内联级后代,例如内联块和内联表。

    所以这意味着如果 span 元素有

    1) display: inline-block,或

    2) 浮动或

    3) 绝对定位然后

    文本装饰一开始不会传播给它。 (这也意味着 text-decoration: none; 不是必需的)

    p {
      color:red; 
      text-decoration: 
        underline; 
      font-size: 1.2em;
    }
    
    span.none {
      display: inline-block;
    }
    <p class="p">Click the thumb<span class="none">!</span></p>

    【讨论】:

      【解决方案2】:

      display:inline-block; 添加到span.none class

      p {
        color:red; 
        text-decoration: 
          underline; 
        font-size: 1.2em;
      }
      
      span.none {
        text-decoration: none;
        display:inline-block;
      }
      <p class="p">Click the thumb<span class="none">!</span></p>

      【讨论】:

      • 感谢您的回答。现在可以了。你能解释一下为什么这是必要的,即它到底做了什么吗?
      【解决方案3】:

      快速修复,但丑陋的一个,但做的伎俩。希望其他人提出更好的答案。

      p {
         color:red; 
         text-decoration: underline; 
         font-size: 1.2em;
      }
      p { 
         display: inline 
      }
      p.none {
         text-decoration: none;
      }
      <p class="p">Click the thumb</p><p class="none">!</p>

      【讨论】:

        猜你喜欢
        • 2014-02-25
        • 2021-12-30
        • 2015-09-20
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多