Use FontAwesome or Glyphicons with css :before

前端 未结 8 921
小蘑菇
小蘑菇 2020-12-07 10:59

Is there any way to embed HTML in the css content: element when using a :before pseudo-element?

I want to use a Font Awesome (or Glyphicon)

相关标签:
8条回答
  • 2020-12-07 11:32
    <ul class="icons-ul">
    <li><i class="icon-play-sign"></i> <a>option</a></li>
    <li><i class="icon-play-sign"></i> <a>option</a></li>
    <li><i class="icon-play-sign"></i> <a>option</a></li>
    <li><i class="icon-play-sign"></i> <a>option</a></li>
    <li><i class="icon-play-sign"></i> <a>option</a></li>
    </ul>
    

    All the font awesome icons comes default with Bootstrap.

    0 讨论(0)
  • 2020-12-07 11:39

    In the case of your list items there is a little CSS you can use to achieve the desired effect.

    ul.icons li {
      position: relative;
      padding-left: -20px; // for example
    }
    ul.icons li i {
      position: absolute;
      left: 0;
    }
    

    I have tested this in Safari on OS X.

    0 讨论(0)
  • 2020-12-07 11:41

    Re: using icon in :before – recent Font Awesome builds include the .fa-icon() mixin for SASS and LESS. This will automatically include the font-family as well as some rendering tweaks (e.g. -webkit-font-smoothing). Thus you can do, e.g.:

    // Add "?" icon to header.
    h1:before {
        .fa-icon();
        content: "\f059";
    }
    
    0 讨论(0)
  • 2020-12-07 11:44

    @keithwyland answer is great. Here's a SCSS mixin:

    @mixin font-awesome($content){
        font-family: FontAwesome;
        font-weight: normal;
        font-style: normal;
        display: inline-block;
        text-decoration: inherit;
        content: $content;
    }
    

    Usage:

    @include font-awesome("\f054");
    
    0 讨论(0)
  • 2020-12-07 11:44

    This is the easiest way to do what you are trying to do:

    http://jsfiddle.net/ZEDHT/

    <style>
     ul {
       list-style-type: none;
     }
    </style>
    
    <ul class="icons">
     <li><i class="fa fa-bomb"></i> Lists</li>
     <li><i class="fa fa-bomb"></i> Buttons</li>
     <li><i class="fa fa-bomb"></i> Button groups</li>
     <li><i class="fa fa-bomb"></i> Navigation</li>
     <li><i class="fa fa-bomb"></i> Prepended form inputs</li>
    </ul>
    
    0 讨论(0)
  • 2020-12-07 11:45

    The accepted answer (as of 2019 JULY 29) is only still valid if you have not started using the more recent SVG-with-JS approach of FontAwesome. In which case you need to follow the instructions on their CSS Pseudo-Elements HowTo. Basically there are three things to watch out for:

    • place the data-attribute on the SCRIPT-Tag "data-search-pseudo-elements" loading the fontawesome.min.js
    • make the pseudo-element itself have display:none
    • proper font-family & font-weight combination for the icon you need: "Font Awesome 5 Free" and 300 (fal/light), 400 (far/regular) or 900 (fas/solid)
    0 讨论(0)
提交回复
热议问题