简体   繁体   English

如何避免子元素从正文元素的样式中获取文本装饰样式

[英]How to avoid child elements to get text-decoration style from body element's style

HTML example: HTML示例:

<span>line1</span>
<div id="div1" class="no-underline">
    <div id="div2" class="no-underline">subline1</div>
    <div>subline2</div>
</div>
body {
    font-family: Arial;
    font-size: 8px;
    color: rgb(255, 0, 85);
    font-weight: bold;
    font-style: italic;
    text-decoration: underline;
}
#div1 {
    color: black !important;
}
#div2 {
    color: green;
}
.no-underline {
    text-decoration:none !important;
}

The result is : 结果是:

  • line1: red and underlined 第1行:红色并带有下划线
  • subline1: green and underlined subline1:绿色和带下划线的
  • subline2: black and underlined subline2:黑色和带下划线的

Demo here 在这里演示

I want subline1 and subline2 not underlined. 我希望subline1和subline2不带下划线。 But body's style should stay at my case. 但是身体的风格应该由我来决定。

How is this possible? 这怎么可能?

First I would recommend to avoid using inline Css and !important, that's a bad practice and will make your code very hard to modify later. 首先,我建议避免使用内联Css和!important,这是一种不好的做法,并且会使以后的代码很难修改。 one solution would be to put your text decoration on the span: 一种解决方案是将文本修饰放在跨度上:

<span style ="text-decoration: underline;">
line1
</span>

You can get rid of text-decoration set on an ancestor using 您可以使用以下方法消除祖先上设置的text-decoration

display: inline-block;

Since in your case your elements are blocks, you may also want 由于在您的情况下,您的元素是块,因此您可能还需要

width: 100%;

to make them more block-like. 使它们更像块状。

Demo 演示版

Here you go! 干得好! http://jsfiddle.net/41ragvd2/ http://jsfiddle.net/41ragvd2/

<body style="font-family: Arial; font-size: 8px; color: rgb(255, 0, 85); font-weight: bold; font-style: italic;" >
<span style="text-decoration: underline;">
line1
</span>

<div style="color:black; !important;">
<div style="color:green; !important;">
subline1
</div>

<div>
subline2
</div
</div>
</body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在使用多个属性时设置单个文本装饰的样式? - Is there a way to style individual text-decoration when using multiple properties? CSS列表样式和文本装饰无效 - CSS list-style and text-decoration having no effect 如何更换<span style="“text-decoration:" underline;”> </span>和<u></u> angular 6 中动态字符串的标记 - How to replace <span style=“text-decoration: underline;”> </span> with <u></u> tag for a dynamic string in angular 6 无法撤消子元素的文本修饰 - Cannot undo text-decoration for child-elements 如何从父元素的样式定义一组子元素的样式? - how to define style for a group of child elements from parent element's style? Elements 的样式不显示文本装饰(下划线)? - Elements's Styles does not show text-decoration (underline)? <a>在对孩子进行一些定位时,</a>如何防止<a>“文本装饰:下划线”损坏<span>?</span></a> - How to keep <a> “text-decoration: underline” from breaking while applying some positioning to the child <span>? 如何在ASP.Net <span style=“text-decoration: underline;”>名称</span>转换为“ <u>名</u> ”(基本的HTML)的SSRS报告? - How to convert <span style=“text-decoration: underline;”>Name</span> to “<u>Name</u>”(basic HTML) in ASP.Net for SSRS reports? 我如何让这个 CSS 文本装饰覆盖工作? - How do I get this CSS text-decoration override to work? 文本修饰不适用于浮动元素 - text-decoration not working on a floating element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM