如何防止CSS自动访问到头部元素的底部

发布于 2025-01-28 08:09:34 字数 1212 浏览 5 评论 0 原文

一组志愿者使用Stenciljs创建了一个单个/多选择的Web组件: https://github.com /notherag/adg-components

对于此Web组件,不启用Shadow dom,以便开发人员可以覆盖/自定义样式。

现在,我们想将其交给客户,以便他们可以在项目中使用它。他们这样做是这样的:

<script type="module" src="/build/adg-components.esm.js"></script>
<script nomodule src="/build/adg-components.js"></script>

<adg-combobox
  id="hobbies"
  name="hobbies"
  label="Please select your hobbies"
  optionslabel="Hobbies"
  multi="true"
  lang="de"
></adg-combobox>

他们所经历的是,由于未启用了影子dom,因此CSS自动连接到&lt; head&gt; 的最底部,在其他任何样式之后:

这很难覆盖特定的样式。有没有办法防止这种情况?例如,在我们的情况下,在&lt; script type =“ module” src =“/build/adg-components.esm.js”&gt;&gt;/script&gt之后会立即添加样式更有意义。 ; ,因此以下任何以下&lt; style&gt; 都将优先。

我能想到的另一个解决方法是,运行 $ npm run build 时,完全删除样式,然后让客户手动导入样式。但这感觉不是正确的方法。

任何帮助将受到高度赞赏。谢谢。

A group of volunteers has created a single/multi-select web component using StencilJS: https://github.com/NothingAG/adg-components

For this web component, Shadow DOM is not enabled so that developers can override/customise styles.

Now we want to hand it over to the client so they can use it in their project. They do it like this:

<script type="module" src="/build/adg-components.esm.js"></script>
<script nomodule src="/build/adg-components.js"></script>

<adg-combobox
  id="hobbies"
  name="hobbies"
  label="Please select your hobbies"
  optionslabel="Hobbies"
  multi="true"
  lang="de"
></adg-combobox>

What they experience though is that because the Shadow DOM is not enabled, the CSS is auto-attached to the very bottom of <head>, after any other styles:

enter image description here

This makes is very hard to override specific styles. Is there a way to prevent that? For example, it would make much more sense in our case that the styles are added right after <script type="module" src="/build/adg-components.esm.js"></script>, so any following <style> would take precedence.

One other workaround I can think of is to remove the styles altogether when running $ npm run build, and then let the client import the styles manually. But this doesn't feel like the right way to do it.

Any help is highly appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2025-02-04 08:09:34

当关闭阴影DOM时,模板将CSS附加到 head 元素,第一个链接>链接元素

这是执行此操作的相关代码行:

因此您需要做的一切至少是一个 link> link> link> link> link> link> link> link> link> link> link> >并且CSS将以正确的文档顺序插入,以便您可以覆盖样式。

When the Shadow DOM is turned off, Stencil attaches the CSS to the head element, before the first link element.

Here is the relevant line of code that does this:
https://github.com/ionic-team/stencil/blob/0cd139b6a0e17c6b0916d8b0fb3e943a523f4a2f/src/runtime/styles.ts#L79

So all you need to do is have at least one link element in the head and the CSS will be inserted in the correct document order so that you can override the styles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文