【问题标题】:How can I insert <span class="notifier">3</span> after <a> with CSS?如何使用 CSS 在 <a> 之后插入 <span class="notifier">3</span>?
【发布时间】:2019-09-26 16:21:29
【问题描述】:

我想在我的 WP 网站上的一个菜单项中添加一个通知圈。 样式是在 css 中完成的,现在它必须放在不存在的 &lt;span&gt; 标记中。 &lt;span&gt; 必须放在 &lt;a&gt; 标记之后才能工作。 我的方法是使用 ::after 但我似乎无法让代码工作。 我做错了什么?

我尝试了很多变化,但没有成功。

这不起作用:

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;
}

以下也不起作用。但是它在前端显示代码&lt;span class="notifier"&gt;3&lt;/span&gt;,而不是样式。

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 的红色圆圈。

【问题讨论】:

    标签: css-selectors


    【解决方案1】:

    You can't inject HTML via pseudo elements,但你不需要。只需制定一个样式规则即可根据需要插入内容和样式:

    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;
    }
    &lt;a href="#"&gt;foo&lt;/a&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多