'content'属性用于继承节点值

jol*_*olt 13 css css3 css-content

有没有办法将节点值继承到content属性?

比如,如果<h2/>标记值设置为"随机标题",我可以content在CSS中的属性内获取此值吗?

我试过了content: inherit;,但它似乎没有用.

我的理论的一个例子:

HTML:

<h2>Random title</h2>
Run Code Online (Sandbox Code Playgroud)

CSS:

h2:after {
    content: inherit; /* should be "Random title" */
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

thi*_*dot 14

不,没有办法做到这一点.这不是inherit为了什么.

你能得到的最接近的是:

<h2 data-title="Random title">Random title</h2>

h2:after {
    content: attr(data-title);
}
Run Code Online (Sandbox Code Playgroud)

..因为重复而显然很可怕.


Alo*_*hci 9

CSS3有办法,content: contents;但(a)它似乎是以一种无用的方式设计的; (b)可能尚未在任何地方实施.见http://dev.w3.org/csswg/css3-content/#contents0

至少@thirtydot的回答是正确的.

  • 8 年后,仍然没有“内容”支持:/ (2认同)