Search code examples
cssfont-awesomefont-awesome-5

Font Awesome 5 shows empty square when using the JS+SVG version


Attempting to replace the bullet type on an list item tag with a Font Awesome icon but I am getting an empty square:

ul {
  list-style: none;
}

.testitems {
  line-height: 2em;
}

.testitems:before {
  font-family: "Font Awesome 5 Free";
  content: "\f058";
  margin: 0 5px 0 -15px;
  color: #004d00;
  display: inline-block;
}
<script src="https://use.fontawesome.com/releases/v5.12.0/js/all.js"></script>
<ul>
  <li class="testitems">List Item 1</li>
  <li class="testitems">List Item 2</li>
  <li class="testitems">List Item 3</li>
  <li class="testitems">List Item 4</li>
  <li class="testitems">List Item 5</li>
</ul>

I know the font library is loading because I was able to use <i class="fas fa-check-circle"></i><li class="testitems">List Item 1</li> and the font rendered properly (though not styled properly).


Solution

  • If you are using the CSS version read this: Font Awesome 5, why css content is not showing?

    Using the last release of the Font Awesome 5 you can enable the use of pseudo-element with the JS version by adding data-search-pseudo-elements like below:

    ul {
      list-style: none;
    }
    
    .testitems {
      line-height: 2em;
    }
    
    .testitems:before {
      font-family: "Font Awesome 5 Free";
      content: "\f058";
      display:none; /* We need to hide the pseudo element*/
    }
    /*target the svg for styling*/
    .testitems svg {
      color: blue;
      margin: 0 5px 0 -15px;
    }
    <script data-search-pseudo-elements src="https://use.fontawesome.com/releases/v5.13.0/js/all.js"></script>
    <ul>
      <li class="testitems">List Item 1</li>
      <li class="testitems">List Item 2</li>
      <li class="testitems">List Item 3</li>
      <li class="testitems">List Item 4</li>
      <li class="testitems">List Item 5</li>
    </ul>
    <i class="fa fa-user"></i>

    You can check the documentation for more details :

    If you’re using our SVG + JS framework to render icons, you need to do a few extra things:

    Enable Pseudo Elements

    Using CSS Pseudo elements to render icons is disabled by default when using our SVG + JS Framework. You’ll need to add the <script data-search-pseudo-elements ... > attribute to the <script /> element that calls Font Awesome.

    Set Pseudo Elements’ display to none

    Since our JS will find each icon reference (using your pseudo element styling) and insert an icon into your page’s DOM automatically, we’ll need to hide the real CSS-created pseudo element that’s rendered.