Use FontAwesome or Glyphicons with css :before

前端 未结 8 922
小蘑菇
小蘑菇 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:50

    This approach should be avoided. The default value for vertical-align is baseline. Changing the font-family of only the pseudo element will result in elements with differing fonts. Different fonts can have different font metrics and different baselines. In order for different baselines to align, the overall height of the element would have to increase. See this effect in action.

    It is always better to have one element per font icon.

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

    What you are describing is actually what FontAwesome is doing already. They apply the FontAwesome font-family to the ::before pseudo element of any element that has a class that starts with "icon-".

    [class^="icon-"]:before,
    [class*=" icon-"]:before {
      font-family: FontAwesome;
      font-weight: normal;
      font-style: normal;
      display: inline-block;
      text-decoration: inherit;
    }
    

    Then they use the pseudo element ::before to place the icon in the element with the class. I just went to http://fortawesome.github.com/Font-Awesome/ and inspected the code to find this:

    .icon-cut:before {
      content: "\f0c4";
    }
    

    So if you are looking to add the icon again, you could use the ::after element to achieve this. Or for your second part of your question, you could use the ::after pseudo element to insert the bullet character to look like a list item. Then use absolute positioning to place it to the left, or something similar.

    i:after{ content: '\2022';}
    
    0 讨论(0)
提交回复
热议问题