Home  >  Article  >  Web Front-end  >  How to Use Font Awesome Icons as CSS Content?

How to Use Font Awesome Icons as CSS Content?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-17 21:50:01498browse

How to Use Font Awesome Icons as CSS Content?

Use Font Awesome Icons as CSS Content

In using Font Awesome icons as CSS content, avoid embedding HTML code directly within the content property. Instead, follow these steps:

FontAwesome 5:

  1. Define the font family as "Font Awesome 5 Free" or "Font Awesome 5 Brands" depending on the icon type.
  2. Use the Unicode escape code instead of HTML entities for the icon, e.g., "f095" for "fa-phone".
  3. Ensure the font weight is set to 900.
a:before {
   font-family: "Font Awesome 5 Free";
   content: "\f095";
   display: inline-block;
   padding-right: 3px;
   vertical-align: middle;
   font-weight: 900;
}

FontAwesome 4 and Below:

  1. Copy the content property with the corresponding entity from the Font Awesome style sheet.
  2. Use the copied content within the content property with the correct font family.
a:before {
    font-family: FontAwesome;
    content: "\f095";
}

For spacing between the icon and text:

  1. Set display: inline-block; for the icon to prevent overlap.
  2. Add padding-right: to provide space between the two.
a:before {
    font-family: FontAwesome;
    content: "\f095";
    display: inline-block;
    padding-right: 3px;
    vertical-align: middle;
}

To change the icon on hover:

  1. Create a separate selector for the :hover state.
  2. Modify the content property to specify the new Unicode escape code for the hover icon.
a:hover:before {
    content: "\f099";
}

Consider setting a fixed width on the base icon declaration to prevent nudging due to different icon sizes.

The above is the detailed content of How to Use Font Awesome Icons as CSS Content?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn