如何链接具有多个父类的CSS类选择器?

huangapple go评论67阅读模式
英文:

how to chain CSS class selectors , which having multiple parent class?

问题

如果HTML结构如下:

-section1
--h1
--h2
--h3
--section2
--h1
--h2
--h3
-section3
--h1
--h2
--h3
--section4
--h1
--h2
--h3

如果我们想要让section1, section2, section3具有相同的样式,而section4具有不同的样式,可以使用以下简短的代码:

.section1 .h1,
.section2 .h1,
.section3 .h1 {
    color: blue;
}

.section1 .h2,
.section2 .h2,
.section3 .h2 {
    color: blue;
}

.section1 .h3,
.section2 .h3,
.section3 .h3 {
    color: blue;
}

.section4 .h1,
.section4 .h2,
.section4 .h3 {
    /* 不同的样式 */
}

这种方式可以更简洁地为不同的部分定义样式。

英文:

if HTML is like:

-section1
--h1
--h2
--h3
--section2
--h1
--h2
--h3
-section3
--h1
--h2
--h3
--section4
--h1
--h2
--h3

what should we do if we want to give section1, section2, section3 same style and section4 different style ? in short code.

I wrote the code like

.section1 .h1,
.section1 .h2,
.section1 .h3,
.section2 .h1,
.section2 .h2,
.section2 .h3,
.section3 .h1,
.section3 .h2,
.section3 .h3,{
     color: blue;
}

but its too long and looking bad, is there any short way to write this.

答案1

得分: 1

你也可以像下面这样做:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
[class^="section"]:not(.section4) :is(h1,h2,h3) {
  color: blue;
}

<!-- language: lang-html -->
<section class="section1">
  <h1>Headline 1</h1>
  <h2>Headline 2</h2>
  <h3>Headline 3</h3>
</section>
<section class="section2">
  <h1>Headline 1</h1>
  <h2>Headline 2</h2>
  <h3>Headline 3</h3>
</section>
<section class="section3">
  <h1>Headline 1</h1>
  <h2>Headline 2</h2>
  <h3>Headline 3</h3>
</section>
<section class="section4">
  <h1>Headline 1</h1>
  <h2>Headline 2</h2>
  <h3>Headline 3</h3>
</section>

<!-- end snippet -->
英文:

You can do like below as well:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

[class^=&quot;section&quot;]:not(.section4) :is(h1,h2,h3) {
  color: blue;
}

<!-- language: lang-html -->

&lt;section class=&quot;section1&quot;&gt;
  &lt;h1&gt;Headline 1&lt;/h1&gt;
  &lt;h2&gt;Headline 2&lt;/h2&gt;
  &lt;h3&gt;Headline 3&lt;/h3&gt;
&lt;/section&gt;
&lt;section class=&quot;section2&quot;&gt;
  &lt;h1&gt;Headline 1&lt;/h1&gt;
  &lt;h2&gt;Headline 2&lt;/h2&gt;
  &lt;h3&gt;Headline 3&lt;/h3&gt;
&lt;/section&gt;
&lt;section class=&quot;section3&quot;&gt;
  &lt;h1&gt;Headline 1&lt;/h1&gt;
  &lt;h2&gt;Headline 2&lt;/h2&gt;
  &lt;h3&gt;Headline 3&lt;/h3&gt;
&lt;/section&gt;
&lt;section class=&quot;section4&quot;&gt;
  &lt;h1&gt;Headline 1&lt;/h1&gt;
  &lt;h2&gt;Headline 2&lt;/h2&gt;
  &lt;h3&gt;Headline 3&lt;/h3&gt;
&lt;/section&gt;

<!-- end snippet -->

答案2

得分: 0

最短的纯CSS代码是通过使用[class^="..."]属性选择器选择以section开头的所有元素,然后专门覆盖第4个部分的样式:

[class^="section"] h1,
[class^="section"] h2,
[class^="section"] h3 {
  color: blue;
}

.section4 h1,
.section4 h2,
.section4 h3 {
  color: red;
}

或者,你也可以使用预处理器如SASS:

[class^="section"] {
  h1, h2, h3 {
    color: blue;
  }
}

.section4 {
  h1, h2, h3 {
    color: red;
  }
}
英文:

The shortest vanilla CSS code is by selecting all elements that start with section by using the [class^=&quot;...&quot;] attribute selector. Then overwrite the styles for section 4 specifically:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

[class^=&quot;section&quot;] h1,
[class^=&quot;section&quot;] h2,
[class^=&quot;section&quot;] h3 {
  color: blue;
}

.section4 h1,
.section4 h2,
.section4 h3 {
  color: red;
}

<!-- language: lang-html -->

&lt;section class=&quot;section1&quot;&gt;
  This is Section 1
  &lt;h1&gt;Headline 1&lt;/h1&gt;
  &lt;h2&gt;Headline 2&lt;/h2&gt;
  &lt;h3&gt;Headline 3&lt;/h3&gt;
&lt;/section&gt;
&lt;section class=&quot;section2&quot;&gt;
  This is Section 2
  &lt;h1&gt;Headline 1&lt;/h1&gt;
  &lt;h2&gt;Headline 2&lt;/h2&gt;
  &lt;h3&gt;Headline 3&lt;/h3&gt;
&lt;/section&gt;
&lt;section class=&quot;section3&quot;&gt;
  This is Section 3
  &lt;h1&gt;Headline 1&lt;/h1&gt;
  &lt;h2&gt;Headline 2&lt;/h2&gt;
  &lt;h3&gt;Headline 3&lt;/h3&gt;
&lt;/section&gt;
&lt;section class=&quot;section4&quot;&gt;
  This is Section 4
  &lt;h1&gt;Headline 1&lt;/h1&gt;
  &lt;h2&gt;Headline 2&lt;/h2&gt;
  &lt;h3&gt;Headline 3&lt;/h3&gt;
&lt;/section&gt;

<!-- end snippet -->

Alternatively, you can also use a Preprocessor like SASS:

[class^=&quot;section&quot;] {
  h1, h2, h3 {
    color: blue;
  }
}

.section4 {
  h1, h2, h3 {
    color: red;
  }
}

huangapple
  • 本文由 发表于 2023年2月24日 14:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定