How to select multiple elements that are children of given element?

后端 未结 3 1006
北恋
北恋 2020-12-24 05:10

I have

and I need to select all pre and div elements that are children of #mydiv.

I coul

相关标签:
3条回答
  • 2020-12-24 05:40

    As far as I know, there is no shorthand for selector grouping.

    See "Selector Grouping".

    Although, with LESS, it is possible in the "Nested Rules" section.

    0 讨论(0)
  • 2020-12-24 05:42

    You'll have to reference #mydiv twice...

    #mydiv > pre, #mydiv > div
    

    I removed the extraneous div element selector as the ID is specific enough.

    0 讨论(0)
  • 2020-12-24 05:54

    The only way to reference the id only once is to use it with the * selector:

    #mydiv > * {
    

    which will apply to all elements that are children of that div.

    Depending on what styles you plan to apply this might be workable (I typically use this to zero-out margins, padding and border styles), but it's still probably best to do it the first way because it makes it easier to make exceptions/changes later down the road.

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