简体   繁体   English

CSS first-child 绝对不工作

[英]CSS first-child absolutely not working

I have these html elements:我有这些 html 元素:

 .form-group:first-child { display: none; } #form-group:first-child { display: none; } #my-form #form-group:first-child { display: none; } #my-form .form-group:first-child { display: none; }
 <form id="my-form"> <div class="form-group" id="form-group">first</div> <div class="form-group" id="form-group">second</div> <div class="form-group" id="form-group"></div> <div class="form-group" id="form-group"></div> <div class="form-group" id="form-group"></div> <div class="form-group" id="form-group"></div> <div class="form-group" id="form-group"></div> <div class="form-group" id="form-group"></div> <div class="form-group" id="form-group"></div> <div class="form-group" id="form-group"></div> </form>

And none of these work, what am I doing wrong?这些都不起作用,我做错了什么?

Firstly, ID's should be unique but首先,ID 应该是唯一的,但是

#my-form .form-group:first-child {

display: none;
}

..will work regardless ..无论如何都会工作

 #my-form .form-group:first-child { display: none; }
 <form id="my-form"> <div class="form-group" id="form-group">1</div> <div class="form-group" id="form-group">2</div> <div class="form-group" id="form-group">3</div> <div class="form-group" id="form-group">4</div> <div class="form-group" id="form-group">5</div> <div class="form-group" id="form-group">6</div> <div class="form-group" id="form-group">7</div> <div class="form-group" id="form-group">8</div> <div class="form-group" id="form-group">9</div> <div class="form-group" id="form-group">10</div> </form>

All those variations should work.所有这些变化都应该有效。 You probably have an error in your <style> or <link> tag, or a problem with the document loading and referencing an external stylesheet.您的<style><link>标记中可能有错误,或者文档加载和引用外部样式表时出现问题。

Examples of correct syntax:正确语法示例:

Referencing an external stylesheet:引用外部样式表:

<link rel="stylesheet" href="./styles.css">

Or an inline stylesheet.或内联样式表。

<style>
.form-group:first-child {
    display: none;
}

#form-group:first-child {
    display: none;
}

#my-form #form-group:first-child {
    display: none;
}

#my-form .form-group:first-child {
    display: none;
}
</style>

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

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