简体   繁体   English

如何删除第三方选择器?

[英]How to remove a third party selector?

I imported a third party CSS (which I'm not allowed to modify) into my application. 我将第三方CSS(我不允许修改)导入到我的应用程序中。 This CSS file is declaring a class name and a selector, eg: 这个CSS文件声明了一个类名和一个选择器,例如:

.third-party-class{
  color: blue;
}

.third-party-class:last-of-type{
  color: red;
}

My goal is to remove the .third-party-class:last-of-type selector completely by using CSS only. 我的目标是仅使用CSS完全删除.third-party-class:last-of-type选择器。 Of course you could just override the .third-party-class:last-of-type selector and copy every property from .third-party-class . 当然你可以覆盖.third-party-class:last-of-type选择器并从.third-party-class复制每个属性。 But this is really inconvenient if .third-party-class has a lot of properties. 但是,如果。 .third-party-class有很多属性,这真的很不方便。

 .third-party-class{ color: blue; } .third-party-class:last-of-type{ color: red; } .desired-li{ color: blue; } 
 <div> Currently: <ul> <li class="third-party-class">one</li> <li class="third-party-class">two</li> <li class="third-party-class">three</li> </ul> Desired: <ul> <li class="desired-li">one</li> <li class="desired-li">two</li> <li class="desired-li">three</li> </ul> </div> 

Question

Is it possible to override/remove a CSS selector completely, without redeclaring all properties of the "base" class by using CSS (no JS) only? 是否可以完全覆盖/删除CSS选择器,而不使用CSS(无JS)重新声明“base”类的所有属性?

Since we are dealing with last-of-type and you cannot change the CSS, you can add an extra element that will trigger this selector and hide it (I suppose you are able to adjust the HTML): 由于我们处理的是last-of-type而你无法更改CSS,你可以添加一个额外的元素来触发这个选择器并隐藏它(我想你可以调整HTML):

 .third-party-class { color: blue; } .third-party-class:last-of-type { color: red; background:pink; font-size:250px; opacity:0.9; display:flex; vertical-align:sub; /*doesn't matter what CSS you will have here*/ } ul li:last-of-type { display:none!important; } 
 <ul> <li class="third-party-class">one</li> <li class="third-party-class">two</li> <li class="third-party-class">three</li> <li></li> </ul> 

I agree with the comment that BenM made to your original question. 我同意BenM对您原始问题的评论。

But...If the CSS is as simple (only one line) as your fiddle leads me to believe you could just use "!important" on the selector you want to overwrite. 但是......如果CSS简单(只有一行),你的小提琴让我相信你可以在你要覆盖的选择器上使用“!important”。

.third-party-class:last-of-type{
    color: blue !important;
 }

That would allow you, on a selector by selector basis (assuming there are more than just color being used), specify what you want the third party code to do. 这将允许您在选择器的基础上(假设不仅仅使用颜色),指定您希望第三方代码执行的操作。

You could use the [attribute^=value] Selector, it will overide every ellement who starts with "value" so you can make something like this: 您可以使用[attribute^=value]选择器,它将覆盖每个以“value”开头的元素,这样您就可以制作如下内容:

 .third-party-class{ background: blue; height: 20px; width: 20px; margin-bottom: 10px; } .third-party-class:last-of-type{ background: red; } div[class^="third-party-class"]{ background: green; } 
 <div class="third-party-class"></div> <div class="third-party-class"></div> <div class="third-party-class"></div> <div class="third-party-class"></div> <div class="third-party-class"></div> <div class="third-party-class"></div> <div class="third-party-class"></div> <div class="third-party-class"></div> <div class="third-party-class"></div> <div class="third-party-class"></div> 

Sorry if i do not understand it right 对不起,如果我不明白的话

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

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