如何通过css在单个元素之前或之后添加文本



我无法修改源html和类覆盖这么大的跨度的东西,我有麻烦添加到它,因为我想。

我正在尝试添加文本到已经有文本的区域。

尝试了以下操作,但没有任何效果。

.content div:firstchild:after { content: 'Test'; }

当我执行以下操作时,它会添加文本,但它会多次添加文本,因为有许多div和其他元素(我曾尝试将其直接添加到表中,在b之后等)

.content div:after { content: 'Test'; }

当我尝试上面的

时看到的控制台页面中该部分的HTML

你的CSS选择器有一个错别字,:firstchild应该是:first-child

这应该做你需要它做的事情:

.content div:first-child:after { content: 'Test'; }

使用:before:after选择器指定应该在该元素内部内容之前(或之后)插入哪些内容。输入元素没有内容

.content div:first-child:before
{
color: green;
content: 'working ';
}
.content div:not(:first-child):after
{
color: green;
content: ' is working';
}

.content div:before:first-child
{
color: red;
content: 'this will never show up';
}
.content div:after:not(:first-child)
{
color: red;
content: 'and this will not show up either';
}