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

How to Use Font Awesome Icons as CSS Content?

Mary-Kate Olsen
Release: 2024-11-17 21:50:01
Original
590 people have browsed it

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;
}
Copy after login

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";
}
Copy after login

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;
}
Copy after login

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";
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template