首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CSS在<a>之后插入<span class="notifier">3</span>?

如何使用CSS在<a>之后插入<span class="notifier">3</span>?
EN

Stack Overflow用户
提问于 2019-05-09 05:11:45
回答 1查看 21关注 0票数 0

我想添加一个通知圆到我的可湿性粉剂网站上的一个菜单项。样式是在css中完成的,现在它必须放在一个不存在的<span>标记中。必须将<span>放在<a>标记之后,它才能工作。我的方法是使用::after,但我似乎无法让代码正常工作。我做错了什么?

我尝试了许多变体,但都没有成功。

这不起作用:

代码语言:javascript
复制
a::after { 
  content: "<span class="notifier">3</span>";
  }
.notifier {
    position: absolute;
    top: 1.25rem;
    right: -0.5rem;
    background: #ea3326;
    width: 1.25rem;
    height: 1.25rem;
    text-align: center;
    line-height: 1.225rem;
    color: #ffffff;
    font-weight: bold;
    border-radius: 100%;
    z-index: 97;
    font-size: 0.8rem;
    cursor: pointer;
}

以下内容也不起作用。但是,它在前端显示了代码<span class="notifier">3</span>,但没有显示样式。

代码语言:javascript
复制
a::after { 
  content: "<span class=\"notifier\">3</span>";
  }
.notifier {
    position: absolute;
    top: 1.25rem;
    right: -0.5rem;
    background: #ea3326;
    width: 1.25rem;
    height: 1.25rem;
    text-align: center;
    line-height: 1.225rem;
    color: #ffffff;
    font-weight: bold;
    border-radius: 100%;
    z-index: 97;
    font-size: 0.8rem;
    cursor: pointer;
}

我希望按钮旁边有一个红色的圆圈和一个白色的3。

EN

回答 1

Stack Overflow用户

发布于 2019-05-09 05:20:32

You can't inject HTML via pseudo elements,但您不需要这样做。只需创建一条样式规则,根据需要插入内容和样式:

代码语言:javascript
复制
a::after {
  content: "3";
  display: inline-block;
  position: relative;
  top: -5px;
  right: -5px;
  background: #ea3326;
  width: 1.25rem;
  height: 1.25rem;
  text-align: center;
  line-height: 1.25rem;
  color: #ffffff;
  font-weight: bold;
  border-radius: 100%;
  z-index: 97;
  font-size: 0.8rem;
  cursor: pointer;
}
代码语言:javascript
复制
<a href="#">foo</a>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56049114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档