html - 子类的CSS属性选择器

标签 html css

只是好奇。

如果我有这样的 CSS 规则:

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

是否可以使用

css attribute selector

这样我就可以写这样的东西了:

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

澄清一下:

  • 我将有一个结构,其中 .section 是所有 DIV(宽度和高度)的公共(public)类
  • .section 中的所有容器将具有不同的样式(背景颜色)
  • .container-n 中的所有H1 都具有相同的样式。 (颜色、字体等)

下面是一个示例 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>

我知道我可以用不同的方式做到这一点,而且我已经知道该怎么做,我只是想知道上面的事情是否可能。

谢谢

最佳答案

是的,它会起作用。只需删除属性选择器前面的句点。

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

Example Here

关于html - 子类的CSS属性选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25675326/

相关文章:

javascript - 如何使用 ajax 发布一些内容然后检索它?

html - 为我的网站 CSS 相关修复 Google Chrome 错误

css - React Native - 如何重叠边距?

html - CSS 问题 : link is wrongly displayed

css - 无法在我的 Meteor 应用程序上呈现样式表(MIME 错误)

html - HTML 和 CSS 中的水平线和垂直线相交

PHP 邮件();以纯文本和 HTML 格式发送电子邮件

html - 对齐嵌套表格宽度

html - 我的 WordPress 主题是连字符,无法使用 CSS 将其关闭

html - 如何固定 <div> 高度