简体   繁体   English

子类的CSS属性选择器

[英]CSS attribute selector of subclass

just a curiosity. 只是好奇心。

If i have CSS rules like this: 如果我有这样的CSS规则:

.section{width:100%;min-height:200px;}
.section.container-1{background:blue;}
.section.container-2{background:red;}
.section.container-3{background:orange;}

is it possible to use the 是否可以使用

css attribute selector CSS属性选择器

so that i can write something like: 这样我就可以写:

.section.[class*='container-'] h1{color:white}

Just to clarify: 只是澄清一下:

  • I will have a structure where .section is the common class to all the DIVs (width and height) 我将拥有一个结构,其中.section是所有DIV(宽度和高度)的通用类
  • All the containers inside .section will have different style (background color) .section内的所有容器将具有不同的样式(背景色)
  • All the H1 inside .container-n will have the same style. .container-n所有H1都将具有相同的样式。 (color, font etc) (颜色,字体等)

here's an example HTML code to make it more clear: 这是一个使它更清晰的示例HTML代码:

<div class="section container-1"><h1></h1><!--Will display blue background and white text --></div>
<div class="section container-2"><h1></h1><!--Will display red background and white text --></div>
<div class="section container-3"><h1></h1> <!--Will display orange background and white text --></div>

I know that i can do it in different ways, and i already know how to do it, i just wonder if something like above could be possible or not. 我知道我可以用不同的方式来做,而且我已经知道如何去做,我只是想知道是否可以进行上述类似的事情。

thanks 谢谢

Yes, it will work. 是的,它将起作用。 Just remove the period prefixing the attribute selector. 只需删除属性选择器前面的句点即可。

.section.[class*='container-'] h1
/*      ^ remove that.. it's an attribute selector, not a class. */

Example Here 这里的例子

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

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