简体   繁体   English

如何忽略全局css?

[英]How to ignore global css?

I have a html page with div like below.我有一个带有 div 的 html 页面,如下所示。

<div id="container">/* lots of nested html with elements like div, ul, li etc */</div>

Above html page also has lots of other html elements outside of the above div.上面的 html 页面在上面的 div 之外还有很多其他的 html 元素。 Now, we have a global css file like style.css which is included in each html file.现在,我们有一个像 style.css 这样的全局 css 文件,它包含在每个 html 文件中。 It has a css rule like following.它有一个如下的 css 规则。

body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td {
    margin: 0;
    padding: 0;
}

Now, i do not want these rules to be applied to any of the elements in the following div.现在,我不希望这些规则应用于以下 div 中的任何元素。

<div id="container">/* lots of nested html with elements like div, ul, li etc */</div>

Css rules should be applied to rest of the stuff in the html. CSS 规则应该应用于 html 中的其余内容。

How should I do this?我该怎么做?

It's quite easy, you can combine the effects of :not and :is CSS selectors.这很容易,您可以结合:not:is CSS 选择器的效果。 Here is a working example.这是一个工作示例。

 :is(body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td):not(#container *) { margin: 0; padding: 0; }
 <div id="container"> <ul> <li>Inside</li> <li>The</li> <li>Container</li> </ul> </div> <ul> <li>Outside</li> <li>The</li> <li>Container</li> </ul>

Run the snippet and you can see that the styles aren't applied to the list inside the container.运行代码段,您可以看到样式未应用于容器内的列表。 What's happening here is that we're selecting all those elements with the :is selector and then deselecting all the children of the #container using the :not selector.这里发生的事情是我们使用:is选择器选择所有这些元素,然后使用:not选择器取消选择#container所有子元素。

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

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