html - css 计数器和自定义组件(没有 shadow dom)

标签 html css custom-component

我在 css 中使用自定义计数器进行打印。但是,当 h1 标签位于自定义组件内时,这会起作用,计数器似乎会重置。这是一个错误吗?

例子: CSS:

h1 {
counter-increment: countH1;
}

html:

<h1>hello</h1>     -> counter is 1
<h1>hello</h1>     -> counter is 2
<my-component>
  <h1>hello again</h1> -> counter is 1 again :-(
</my-component>

请注意,my-component 是一个自定义元素。这是一个 customElements.define('my-component', myComponent);在 JS 中实现。

css 计数器是否与自定义组件不兼容?

约翰。

最佳答案

您可以保留 HTML 原样,但尝试使用 :before 或 :after 使您的 CSS 看起来像这样。

body {
  counter-reset: countH1;
}

h1 {
counter-increment: countMe;
}

h1:before {
  content: counter(countMe, upper-roman);
  padding-right: 20px;
}

关于html - css 计数器和自定义组件(没有 shadow dom),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51509034/

相关文章:

javascript - 使用 javascript 显示/隐藏脚本

ios - 创建自定义类型的 SKSpriteNode

ios - 快速线程1 : Fatal error: init(coder:) has not been implemented (Calling super solution doesn't work)

html - 按钮高度大于嵌套内容的高度

html - 如何让mask div中的center div居中对齐?

html - css 中的大纲不适用于 IE(最新版本)

html - 强制页脚停留在页面底部

css - 图片和 HTML5 视频不会有相同的高度

java - 如何在 javax.swing.text.HtmlDocument 中为元素创建 ComponentView

html - 我无法像我想要的那样定位和居中元素