在相同类名下应用 CSS 样式,但排除第一个元素。

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

Apply css to all elements except first one with same class name

问题

Sure, here's the translated code part you requested:

我有以下的HTML结构,我想对所有带有类名**flw__subform-instance**的元素应用边框CSS,除了第一个:

```html
<div class="flw__subform__content-wrapper">
    <div class="flw__subform__header"></div>
    <div class="flw__subform-instance">1</div> <!--排除这个,因为它是第一个元素-->
    <div class="flw__subform-instance">2</div>
    <div class="flw__subform-instance">3</div>
</div>

我尝试了以下的代码,但它不起作用:

.flw__subform-instance {
    padding: 20px;
    border-top: 1em solid #ddd;
}
.flw__subform-instance:first-child {
    padding: 20px;
}
请注意,我只提供了代码的翻译部分,没有其他内容。

<details>
<summary>英文:</summary>

I have below html structure, I want to apply border css to all elements with class name **flw__subform-instance** except first one:


<div class="flw__subform__content-wrapper">
<div class="flw__subform__header"></div>
<div class="flw__subform-instance">1</div> <!--exclude this bc 1st element -->
<div class="flw__subform-instance">2</div>
<div class="flw__subform-instance">3</div>
</div>


I have tried below code but its not working:

.flw__subform-instance{
padding: 20px;
border-top: 1em solid #ddd;
}
.flw__subform-instance:first-child {
padding: 20px;
}


</details>


# 答案1
**得分**: 1

```css
.flw__subform-instance ~ .flw__subform-instance {}
英文:

Short and simple:

.flw__subform-instance ~ .flw__subform-instance {}

Reference: General sibling combinator

Try it:

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

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

div &gt; div {
  margin-bottom: 2px;
  height: 20px;
  background: #ff0;
}

.foo ~ .foo {
  background: #f00;
}

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

&lt;div&gt;
  &lt;div class=&quot;baz&quot;&gt;.baz&lt;/div&gt;
  &lt;div class=&quot;foo&quot;&gt;.foo&lt;/div&gt;
  &lt;div class=&quot;foo&quot;&gt;.foo&lt;/div&gt;
  &lt;div class=&quot;baz&quot;&gt;.baz&lt;/div&gt;
  &lt;div class=&quot;foo&quot;&gt;.foo&lt;/div&gt;
  &lt;div class=&quot;foo&quot;&gt;.foo&lt;/div&gt;
  &lt;div class=&quot;baz&quot;&gt;.baz&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->
.flw__subform-instance{
    padding: 20px;
    border-top: 1em solid #ddd;
}
.flw__subform-instance:first-child {
    padding: 20px;
}

.flw__subform__content-wrapper div:nth-child(2){
    padding: 0px;
    border-top: 0em solid red;
}

<!-- 语言: lang-html -->
<div class="flw__subform__content-wrapper">
    <div class="flw__subform__header"></div>
    <div class="flw__subform-instance">1</div> <!-- 不包括这个因为它是包装器的第二个元素 -->
    <div class="flw__subform-instance">2</div>
    <div class="flw__subform-instance">3</div>
</div>

<!-- 结束代码片段 -->

您可以使用 nth-child 选择器来重置包装器的第二个元素的 CSS,以解决您的问题。

英文:

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

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

.flw__subform-instance{
    padding: 20px;
    border-top: 1em solid #ddd;
}
.flw__subform-instance:first-child {
    padding: 20px;
}

.flw__subform__content-wrapper div:nth-child(2){
    padding: 0px;
    border-top: 0em solid red;
}

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

&lt;div class=&quot;flw__subform__content-wrapper&quot;&gt;
    &lt;div class=&quot;flw__subform__header&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;flw__subform-instance&quot;&gt;1&lt;/div&gt; &lt;!--exclude this bc 1st element --&gt;
    &lt;div class=&quot;flw__subform-instance&quot;&gt;2&lt;/div&gt;
    &lt;div class=&quot;flw__subform-instance&quot;&gt;3&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

You can use nth-child selector to reset the css of 1st element which is the second of the wrapper to solve your problem.

答案3

得分: 0

.flw__subform__content-wrapper .flw__subform-instance:not(.flw__subform-instance:nth-of-type(2)) {border:1px solid #000000;}

英文:
.flw__subform__content-wrapper .flw__subform-instance:not(.flw__subform-instance:nth-of-type(2)) {border:1px solid #000000;}

huangapple
  • 本文由 发表于 2023年3月23日 12:12:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819196.html
匿名

发表评论

匿名网友

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

确定