简体   繁体   English

如何防止CSS影响外部插件?

[英]How do i prevent my CSS affecting external plugins?

As an example I got a css class which applies to all labels within my website. 举个例子,我得到了一个CSS类,该类适用于网站中的所有标签。

label
{
 font-size: 18px;
}

Now after i install a external JS plugin i find that the plugin itself is affected by my base css. 现在,在安装外部JS插件后,我发现该插件本身受基本CSS影响。

<div>
    <div class="plugin" />
    <label>Xyz</label> 
    //Dynamic html inserted by plugin
</div>

The plugin has its own stylesheet so how can i prevent my base css style touching any elements within the plugin div? 该插件具有自己的样式表,因此如何防止我的基本CSS样式触摸插件div中的任何元素?

EDIT 编辑

I must add that label was a very simple example. 我必须添加标签是一个非常简单的示例。 The actual layout is more complex with global styles touching various elements. 实际布局更加复杂,全局样式涉及各种元素。

Don't make your css too general, try to be as specific as possible when you want to style only some of your elements. 不要让CSS过于笼统,当您只想对某些元素进行样式设置时,请尝试使其尽可能具体。 If you can't select your elements without affecting the plugin's elements add a class to them. 如果您不能在不影响插件元素的情况下选择元素,请向它们添加一个类。

label{ /* too general, don't use this */
 /* ... */
}
body > div > form > label{ /* more specific, but maybe still affecting your plugin */
 /* ... */
}

label.noplugin{ /* use a class on non-plugin elements */
 /* ... */
}

div:not(.plugin) > label{ /* affecting only children of div which are NOT
    tagged with the plugin class */
 /* ... */
}

So in your case a better way to style your label would be 因此,在您的情况下,更好的样式标签样式是

<div>
    <div class="plugin">
    <label>Xyz</label>
    //Dynamic html inserted by plugin
    </div>
</div>

CSS: CSS:

*:not(.plugin) > label
{
 font-size: 18px;
}

Please note that :not is unfortunately not supported by IE ≤8 . 请注意:not IE≤8不支持 :not

You need to do two things. 您需要做两件事。

i) Give your parent container ID i)提供您的父容器ID

ii) And style child label of container. ii)和容器的样式子标签。

Here is fiddle workout . 这是小提琴锻炼

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

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