仅当元素具有两个类时才应用的 CSS 规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5796654/
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-30 00:24:26  来源:igfitidea点击:

CSS rule to apply only if element has BOTH classes

csscss-selectors

提问by Majid Fouladpour

Let's say we have this markup:

假设我们有这个标记:

<div class="abc"> ... </div>
<div class="xyz"> ... </div>
<div class="abc xyz" style="width: 100px"> ... </div>

Is there a way to select only the <div>which has BOTH abcand xyzclasses (the last one) AND override its inline width to make the effective width be 200px?

有没有办法只选择<div>具有 BOTHabcxyz类(最后一个)并覆盖其行内宽度以使有效宽度为 200px 的方法?

Something like this:

像这样的东西:

[selector] {
  width: 200px !important;
}

回答by esqew

div.abc.xyz {
    /* rules go here */
}

... or simply:

...或者简单地说:

.abc.xyz {
    /* rules go here */
}

回答by John Hartsock

Below applies to all tags with the following two classes

以下适用于具有以下两个类的所有标签

.abc.xyz {  
  width: 200px !important;
}

applies to div tags with the following two classes

适用于具有以下两个类的 div 标签

div.abc.xyz {  
  width: 200px !important;
}

If you wanted to modify this using jQuery

如果你想使用 jQuery 修改它

$(document).ready(function() {
  $("div.abc.xyz").width("200px");
});

回答by scrappedcola

If you need a progmatic solution this should work in jQuery:

如果您需要一个程序化的解决方案,这应该适用于 jQuery:

$(".abc.xyz").css("width", 200);