CSS text-decoration 属性不能被子元素覆盖

标签 css

<分区>

Possible Duplicate:
How do I get this CSS text-decoration override to work?

看看这个简单的例子:

<a href="#"> A <span>red</span> anchor </a>
a {
    color:blue;
    font-family:Times New Roman;
    text-decoration:underline; 
}

span {
    color:red;
    font-family:Arial;
    text-decoration:none;   
}

现场演示: http://jsfiddle.net/5t9sV/

正如您在 JSfiddle 上的演示中看到的那样,SPAN 元素覆盖了其祖先 ANCHOR 元素的 colorfont-family 属性值。但是,由于某些原因,text-decoration 属性不会被覆盖。

我假设一些 CSS 属性可以被祖先元素覆盖,而另一些 CSS 属性则不能。

是这样吗? 如果是,我怎么知道哪些可以被覆盖,哪些不能被覆盖?

最佳答案

来自 text-decoration spec :

The 'text-decoration' property on descendant elements cannot have any effect on the decoration of the ancestor.

The answer in the linked question进一步引用(但是我在规范中找不到这段文字了):

Text decorations on inline boxes are drawn across the entire element, going across any descendant elements without paying any attention to their presence.

再引用一句,CSS3似乎引入了 text-decoration-skip ,旨在通过将属性应用于后代(在您的情况下为 <span> )来解决此问题:

This property specifies what parts of the element's content any text decoration affecting the element must skip over. It controls all text decoration lines drawn by the element and also any text decoration lines drawn by its ancestors.

关于CSS text-decoration 属性不能被子元素覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4481318/

相关文章:

javascript - 在 rails 4.0 上使用 javascript 更改图像

html - 无法让文本与底部对齐

css - Div 以另一个 div 为中心

css - 为什么 <ul> 显示 :inline does not bring list items with the document flow as <ul> with display:inline-block does

php - 如果旧的 img 上传具有不同的扩展名,我如何用新的 img 上传覆盖它们

javascript - 是否可以在 CSS 宽度转换上强制硬件加速

html - 在内联 svg 多边形中使用绝对和相对单位

javascript - 尝试查找关键字并使用 JavaScript 将它们设为斜体

javascript - 在angularjs的ng-repeat中插入竖线

php - 无法将 slider 文本更改为 slider 标题