Why doesn't this CSS :first-child selector work?

后端 未结 1 1925
猫巷女王i
猫巷女王i 2020-12-16 12:28

I\'m working on an Asp.Net MVC 3 project and have run into a brick wall on why this doesn\'t work like I think it should.

My markup is:



        
相关标签:
1条回答
  • 2020-12-16 12:47

    fieldset > div:first-child means "select the first child element of a fieldset if it's a div".

    It does not mean "select the first div in the fieldset".

    The first child in this case is <input type="hidden" value="2">.

    To select that div without changing the HTML, you need to use fieldset > div:first-of-type.

    Unfortunately, while :first-child is widely supported, :first-of-type only works in IE9+ and other modern browsers.

    So, in this case, the best fix is to continue using fieldset > div:first-child, and simply move <input type="hidden" value="2"> so that's it's not the first child.

    0 讨论(0)
提交回复
热议问题