如何在<a> with CSS?

时间:2019-05-08 21:11:45

标签: css-selectors

I want to add a notifier circle to one menu item on my WP site. The styling is done in css and now it must be placed in a <span> tag that does not exist. The <span> has to be placed after the <a> tag for it to work. My approach is to use ::after but I can't seem to get the code to work. What am I doing wrong?

I have tried many variations but no succes.

This does not work:

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

The following also does not work. However it shows the code <span class="notifier">3</span> on the front end but not the styling.

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

I expect a red circle with a white 3 next to a button.

1 个答案:

答案 0 :(得分: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;
}
<a href="#">foo</a>