如何使用CSS在运行时在HTML元素之前插入一些内容

作者:互联网

2026-03-29

HTML教程
假设 HTML定义为

Some Important Text.Insert "Important" Before This
General Text

所以一般来说输出就是

Some Important Text.Insert “Important” Before This

General Text

但渲染后它应该告诉我们

Important Generated Content

Some Important Text.Insert “Important” Before This

General Text

我正在寻找只有css的解决方案.

你正在寻找这个

.runtime:before{    content: "Important Generated Content";    font-weight: bold;    display: block;}

是的,您可以根据建议的其他答案添加一些保证金.如果我直接更改我的代码,我会觉得犯了剽窃行为.请在这里注释.

你可能会猜到,之前有过吗?答案是肯定的!

你可以做的事情

.importantStuff:after{    content: "!!!!!";}

这会在importantStuff类的末尾添加!s

最后一件事,每个元素只能在前一个和后一个之后只有一个,所以明智地使用它们.并享受使用CSS.