Search code examples
htmlcsspseudo-class

load font-awesome icon in ::before and ::after pseudo element


I can't load icons from font awesome in pseudo-element. I tried to follow the docs but it didn't work.

.x::before {
  display: inline-block;
  font-style: normal;
  font-variant: normal;
  text-rendering: auto;
}

.x::before {
  font-family: "Font Awesome 5 Free";
  font-weight: 900;
  content: "\f007";
}
<p class="x">hello, my name is john</p>


Solution

  • Adding the font-awesome stylesheet to your code and it will load the icon.

    .x::before {
      display: inline-block;
      font-style: normal;
      font-variant: normal;
      text-rendering: auto;
    }
    
    .x::before {
      font-family: "Font Awesome 5 Free";
      font-weight: 900;
      content: "\f007";
    }
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"></link>
    <p class="x">hello, my name is john</p>