Html 从特定 DIV 中删除所有 CSS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13219362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:43:54  来源:igfitidea点击:

Remove all CSS from specific DIV

htmlcss

提问by user838437

Possible Duplicate:
Disinherit (reset) the CSS style of a specific element?

可能的重复:
取消继承(重置)特定元素的 CSS 样式?

I have a page that loads an external CSS file with different CSS attributes.

我有一个加载具有不同 CSS 属性的外部 CSS 文件的页面。

Is it possible to create an element within that same page and specifically for that element not load any of the css?

是否可以在同一页面内创建一个元素,并且专门为该元素不加载任何 css?

For example:

例如:

<style type="text/css">
p {
    background-color:#000000;
    width:550px;
}
</style>
<p>This should get the P styling from the style tag</p>
<p>This should NOT get the P styling</p>

回答by Tim Medora

As everyone else is saying, there are usually better ways to isolate an element. However, there is a CSS selector for this purpose too.

正如其他人所说,通常有更好的方法来隔离元素。但是,也有一个用于此目的的 CSS 选择器。

See The Negation Pseudo-Class

否定伪类

HTML

HTML

<p>A paragraph</p>
<p class="nostyle">Don't style me</p>
<p>A paragraph</p>
<p>A paragraph</p>

CSS

CSS

P:not(.nostyle) { color: red; }

Example: http://jsfiddle.net/LMDLE/

示例:http: //jsfiddle.net/LMDLE/

This is rarelythe right solution, but it can be useful for handling edge cases which are hard to match with another selector.

很少是正确的解决方案,但它对于处理难以与另一个选择器匹配的边缘情况很有用。

回答by Martin Lyne

You could positively isolate the P's you want styled:

您可以肯定地隔离您想要样式的 P:

<p class="hasStyle"></p
<p></p>

Or you could override the ones you want to remain unstyled:

或者你可以覆盖那些你想保持无样式的:

<style>
p {
    background-color:#000000;
    width:550px;
}

.noStyle {
 background-color: none;
 width: none /* or whatever you want here */;
}
</style>

<p>has a style</p>
<p class="noStyle"></p>

The latter is harder to maintain.

后者更难维护。

回答by Rick Calder

This would be exactly what classes were designed for.

这正是类的设计目的。

<style type="text/css">
.class1{
background-color:#000000;
width:550px;
}
</style>
<p class="class1">This should get the P styling from the style tag</p>
<p>This should NOT get the P styling</p>

For the record don't use names like class1 that was for demonstration only. Use descriptive names for classes that make sense.

为了记录,请勿使用仅用于演示的 class1 之类的名称。为有意义的类使用描述性名称。

回答by Jezen Thomas

As I commented, What's wrong with using classes, IDs, and pseudo-selectors?

正如我所评论的,使用类、ID 和伪选择器有什么问题?

For example, this works just fine:

例如,这工作得很好:

p:first-child {
    background-color:#000000;
    width:550px;
}

As does

就像

.first {background-color: #000; width: 550px;}

<p class="first">Some styled text</p>
<p>Some default text</p>