Understanding text-decoration Inheritance
In CSS, the text-decoration property governs the decorative line styles applied to text. However, unlike other text-related styles such as font-weight, text-decoration behaves distinctly.
The Issue with Overriding Inherited Decoration
Consider the following CSS code:
ul > li { text-decoration: none; } ul > li.u { text-decoration: underline; } ul > li > ul > li { text-decoration: none; } ul > li > ul > li.u { text-decoration: underline; }
This code is intended to prevent inherited decoration from being applied to nested list items. However, the HTML structure below reveals an unexpected result:
<ul> <li>Should not be underlined</li> <li class="u">Should be underlined <ul> <li>Should not be underlined</li> <li class="u">Should be underlined</li> </ul> </li> </ul>
Explanation
The issue lies in the fact that text-decoration applies to all nested elements within the element it is applied to. This means that even if you attempt to override the inherited decoration on nested elements, the inherited decoration will still be applied.
According to the CSS 2.1 specification, "text-decoration on inline boxes are drawn across the entire element, going across any descendant elements without paying any attention to their presence."
Solution
To address this issue, a different approach is required. Instead of relying on nested selectors to override inherited decoration, use an alternate technique such as:
The above is the detailed content of How Does CSS Text-Decoration Inheritance Work, and Why Does Overriding it Sometimes Fail?. For more information, please follow other related articles on the PHP Chinese website!