英文:
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 > div {
margin-bottom: 2px;
height: 20px;
background: #ff0;
}
.foo ~ .foo {
background: #f00;
}
<!-- language: lang-html -->
<div>
<div class="baz">.baz</div>
<div class="foo">.foo</div>
<div class="foo">.foo</div>
<div class="baz">.baz</div>
<div class="foo">.foo</div>
<div class="foo">.foo</div>
<div class="baz">.baz</div>
</div>
<!-- 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 -->
<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>
<!-- 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;}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论