CSS selector for shadow root or all top level elements in shadow root

前端 未结 1 1186
南笙
南笙 2021-01-02 07:14

I need a selector for usage in css inside a shadow root, which selects all children (but not grand children) of the shadow root, no matter what tag they are and without givi

1条回答
  •  鱼传尺愫
    2021-01-02 07:26

    Use this selector: :host > *

    The :host selector is described in https://drafts.csswg.org/css-scoping/#host-selector

    document.querySelector( 'my-element' )
      .attachShadow( { mode: 'open' } )
      .innerHTML = `
        SPAN
        A
        

    P

    DIV SPAN IN DIV
    `
    
    

    :host may also hold a compound selector, which must be places in brackets. E.g. :host([foo=bar]) selects a host element which has attribute foo set to bar.

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