简体   繁体   English

覆盖CSS中的边框样式

[英]Overriding border styles in CSS

I have a three types of boxes being displayed in the image (2,3, and 4) 我在图像中显示了三种类型的框(2,3和4)

在此输入图像描述

The first element is the selected style of a box. 第一个元素是框的选定样式。

I would like to apply this selected style on the other three. 我想在其他三个上应用这个选定的风格。

  • For 2 it is straight forward. 对于2,它是直截了当的。 The gray goes to black. 灰色变黑了。
  • For 3 I would need to replace the dashed with a solid line. 对于3我需要用实线替换虚线。
  • For 4 I would replace the gray with the black, but I would want to keep the image. 对于4我会用灰色替换灰色,但我想保留图像。

However I am trying to understand how it would work with inheritance in CSS. 但是,我试图了解它如何与CSS中的继承一起使用。 I am just adding a class 'active' to mark it as selected. 我只是添加了一个“有效”类来将其标记为已选中。 It works fine for 2, but for 3 and 4 it does not know how to process the order of class names. 它适用于2,但对于3和4,它不知道如何处理类名的顺序。 I cannot remove the classes of the elements, because obviously I want to cascade the changes through (like the image in option 4). 我无法删除元素的类,因为显然我想通过级联更改(如选项4中的图像)。

My CSS looks like this : 我的CSS看起来像这样:

li {
  padding: 0.125rem;
  margin: 0.4375rem 1.0625rem 0.3125rem 0;
  border: 0.0625rem solid #d9d9d9;
}

li.active {
  border: 0.0625rem solid black;
}

li.sold {
  display: inline-block;
  border: 1px solid #ccc;
  background: url(http://i.piccy.info/i7/c7a432fe0beb98a3a66f5b423b430423/1-5-1789/1066503/lol.png);
  background-size: 100% 100%;
}

li.wait-list {
  display: inline-block;
  border: 1px dashed #ccc;
}

I have demonstrated this in a JSFiddle : 我在JSFiddle中演示了这个:

https://jsfiddle.net/561aLm4z/7/ https://jsfiddle.net/561aLm4z/7/

It's probably an obvious question, but how do I get the 'active' style to overwrite the styles on box 3 and 4? 这可能是一个显而易见的问题,但如何让“主动”风格覆盖第3和第4栏的样式?

Overwrite the background and border and move the css to the bottom, or make the css more specific (read more about css specifity) (by adding ul ) 覆盖背景和边框并将css移动到底部,或使css更具体(更多关于css指定) (通过添加ul

 add1 = false add2 = false add3 = false add4 = false $("#2").click( function() { if (!add2) { $("#2").addClass("active") add2 = true; } else { $("#2").removeClass("active") add2 = false; } } ) $("#3").click( function() { if (!add3) { $("#3").addClass("active") add3 = true; } else { $("#3").removeClass("active") add3 = false; } } ) $("#4").click( function() { if (!add4) { $("#4").addClass("active") add4 = true; } else { $("#4").removeClass("active") add4 = false; } } ) 
 li { padding: 0.125rem; margin: 0.4375rem 1.0625rem 0.3125rem 0; border: 0.0625rem solid #d9d9d9; } li.sold { display: inline-block; border: 1px solid #ccc; background: url(http://i.piccy.info/i7/c7a432fe0beb98a3a66f5b423b430423/1-5-1789/1066503/lol.png); background-size: 100% 100%; } li.wait-list { display: inline-block; border: 1px dashed #ccc; } ul li.active { border: 0.0625rem solid black; background: none; border: 1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="hse-product-variant "> <li id="1" style="display: inline-block;" class="active"> 1 </li> <li id="2" style="display: inline-block;" class=""> 2 </li> <li id="3" style="display: inline-block;" class="wait-list"> 3 </li> <li id="4" style="display: inline-block;" class="sold"> 4 </li> </ul> 

The issue you are having is called 'specificity' and is best explained by the blog post commonly known as ' specificity wars '. 您遇到的问题称为“特异性”,最好通过俗称“ 特异性战争 ”的博客文章来解释。

In a nutshell, each element has a specificity value. 简而言之,每个元素都具有特异性值。

  • Elements, like li have a value of 1. li一样的元素值为1。
  • Classes, like .sold have a specificity of 10. .sold这样的类具有10的特异性。
  • IDs, like #box have a specificity of 100. #box这样的ID具有100的特异性。

By combining these values, you can overwrite less specific items. 通过组合这些值,您可以覆盖较少的特定项目。

The order of the rules also change the specificity. 规则的顺序也改变了特异性。 If you have two items with a matching specificity score, the later of the two will shine through. 如果您有两个具有匹配特异性分数的项目,则两个项目中的较晚者将会闪现。

As such, you can overwrite your li.sold rule (score of 11) by adding a li.sold.selected rule (score of 21). 因此,您可以通过添加li.sold.selected规则(得分为21)来覆盖您的li.sold规则(得分为11)。

You can also involve parent element ids to increase specificity. 您还可以使用父元素ID来提高特异性。 For example: 例如:

div.man (score of 11) div.man(得分11)

#house div.man (score of 111) #house div.man(得分111)

#London #house div.man (score of 211) #London #house div.man(得分211)

This is the basics of specificity. 这是特异性的基础。 I recommend you read the article also - which puts the concept in terms of StarWars, which is always appreciated. 我建议你也阅读这篇文章 - 它将概念放在StarWars中,这一点值得赞赏。

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

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