How to use global css styles in shadow dom

前端 未结 4 759
孤城傲影
孤城傲影 2021-01-07 22:58

Shadow dom encapsulate css styles, selectors don\'t cross the shadow boundary.

Question: How to use global common css styles in shadow

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 23:40

    I've just struggled with the same problem as an original issue, namely: defining once some global rule for, say,

    element and benefit from that within any/many ShadowDOMs.

    No, css-variables are not suited well for this thing, since even if I've defined once, say, font and color variables for

    , I'll still need to go over each and every shadowed stylesheet and add a CSS rule consuming them.

    At the point of writing this (yes, we are 2019 now) the shortest standardized solution is indeed importing some global common CSS. Works perfectly in Chrome, Firefox and Anaheim (Edge on Chromium).

    It still requires to add an @import rule in each and every component, so still costy (from coding/maintenance POV, the stylesheet fetched only once), but that's the lowest price we can pay now.

提交回复
热议问题