一个字段集上的第一个孩子

Cha*_*erj 3 html css

我在名为myform的表单标记中有2个字段集

我正试图进入第一个fieldset.现在在fieldsets之前还有其他div和p标签.我正在尝试第一个fieldset和last

<myform>
  <div></div
  <p></p>
  <fieldset></fieldset>
  <div></div>
  <fieldset></fieldset>
</myform>

#myform fieldset{
// all round styles for the fieldset
}

    #myform:first-child fieldset{
// specific to the first field set
}

    #myform:last-child fieldset{
specific to the last filedset
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*ida 8

你的HTML不正确.<myform></myform>应该<form id="myform"></form>

适当的选择器是 #myform fieldset:first-of-type

这个页面是关于css伪类如何工作的一个很好的参考


jer*_*oen 5

这取决于您的浏览器要求,但要定位所有现代浏览器,您可以使用:

#myform fieldset:first-of-type {
}
Run Code Online (Sandbox Code Playgroud)

第一个fieldset.

编辑:您不能使用,:first-child因为第一个fieldset不是它的父亲的第一个孩子,请参阅http://www.w3.org/TR/CSS2/selector.html#first-child