简体   繁体   English

内部元素有类时如何忽略正文元素样式

[英]How to ignore body element style when there is a class at element inside

Look, i have the html below:看,我有下面的html:

<body style="color:red;">
text inside 1
<p>
text inside p
...
</p>
<div class="divable1">
text inside div 1
</div>

</body>

and i want only "text inside 1" and "text inside p" to have color red BUT "text inside div 1" i want to ignore the style of the body element.并且我只希望“文本内 1”和“p 内的文本”具有红色但“div 1 内的文本”我想忽略 body 元素的样式。

How is this possible with CSS? CSS怎么可能做到这一点?

Thank you in advance!先感谢您!

There is no way to ignore rules in CSS, only to override them.没有办法忽略 CSS 中的规则,只能覆盖它们。 Write a ruleset with:编写一个规则集:

  • A selector that matches the div (such as .divable1 )匹配 div 的选择器(例如.divable1
  • A rule that changes the color property so it has a value other than the default, which is inherit , such as color: black .更改color属性使其具有除默认值(即inherit以外的值的规则,例如color: black

This is basic CSS hierarchy, you can't ignore the rules, but you can easily overwrite them:这是基本的 CSS 层次结构,您不能忽略规则,但可以轻松覆盖它们:

body {
    color: red;
}
.divable1 {
    color: green;
}

http://jsfiddle.net/vLtqjjpk/ http://jsfiddle.net/vLtqjjpk/

I'm aware that this question was asked a very long time ago, but it could possibly be done by unsetting one or all properties:我知道很久以前就有人问过这个问题,但可以通过取消设置一个或所有属性来完成:

body {
  color: red;
}

.divable1 {
  all: unset; /* resets everything */
  color: unset; /* resets specific property */
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM