Sass (SCSS)
您是否在项目中使用 SCSS 作为 CSS 预处理器?没问题,Font Awesome 也有 SCSS 版本,如果您希望将我们的样式导入到您的工作流程中。
我们将介绍选择项目所需的 SCSS 文件的基础知识,将 Font Awesome 添加到您的编译,使用混合注入代码到类以及更多内容。
开始设置
您将在 Font Awesome 下载 的 fontawesome6
目录中找到所需的一切。以下是专门为 SCSS 制定的文件列表。您可以将它们全部添加到您的项目中,也可以只选择您需要的文件。
文件 | 功能 |
---|---|
fontawesome.scss | 主要的 Font Awesome 编译 |
brands.scss | 品牌图标样式 |
solid.scss | 实心图标样式 |
regular.scss | 常规图标样式 |
light.scss | 浅色图标样式 |
thin.scss | 细线图标样式 |
sharp-solid.scss | 锐利实心图标样式 |
sharp-regular.scss | 锐利常规图标样式 |
sharp-light.scss | 锐利浅色图标样式 |
sharp-thin.scss | 锐利细线图标样式 |
duotone.scss | 双色调 图标样式 |
_duotone-icons.scss | 双色调图标类 |
_variables.scss | 定义贯穿样式使用的变量的地方 |
_mixins.scss | 贯穿样式/图标定义使用的实用程序 |
_core.scss | 基本图标引用类语法和定义 |
_sizing.scss | 图标大小 支持样式 |
_fixed-width.scss | 固定宽度 图标支持样式 |
_list.scss | 列表中的图标 支持样式 |
_bordered-pulled.scss | 带边框和拉伸的 图标支持样式 |
_animated.scss | 动画 图标支持样式 |
_rotated-flipped.scss | 旋转 图标支持样式 |
_stacked.scss | 堆叠 图标支持样式 |
_icons.scss | 所有图标定义 |
_screen-reader.scss | 屏幕阅读器专用和 无障碍性 支持样式 |
_custom-icons.scss | 包含 工具包下载 中的自定义图标(如果您正在使用工具包) |
将 Font Awesome 添加到您的编译
将 scss
文件夹复制到您的项目中。然后将整个 webfonts
文件夹复制到您的项目中,您的静态文件将在该文件夹中提供服务。
打开项目的 scss/variables.scss
并编辑 $fa-font-path
变量,使其指向您放置 webfonts
文件夹的位置。
$fa-font-path: '../webfonts';
在您的主 SCSS 编译文件中,通过添加核心样式文件 @import "scss/fontawesome.scss"
来导入 Font Awesome。然后导入一个或多个样式。
这是一个简单的示例
// importing core styling file@import './fontawesome/scss/fontawesome.scss';
// our project needs Solid + Brands@import './fontawesome/scss/solid.scss';@import './fontawesome/scss/brands.scss';
如果您想使用更多样式,请添加更多样式导入
// importing core styling file@import './fontawesome/scss/fontawesome.scss';
// our project needs Classic Solid, Brands, and Sharp Solid@import './fontawesome/scss/solid.scss';@import './fontawesome/scss/brands.scss';@import './fontawesome/scss/sharp-solid.scss';
使用双色调图标
如果项目使用双色调图标,则需要 @import
两个单独的文件。
// importing core styling file@import './fontawesome/scss/fontawesome.scss';
// our project needs Duotone - which requires both of the following files@import './fontawesome/scss/duotone.scss';@import './fontawesome/scss/\_duotone-icons.scss';
使用下载的工具包
从 6.4 版开始,您现在可以像使用 Font Awesome 一样下载工具包进行编译和自行托管!要下载工具包,请确保设置中的工具包版本设置为“最新 6.x”,或者如果您选择了特定版本,则它至少需要为 6.4。然后,在工具包的“设置”选项卡中,您将看到下载选项。您的工具包下载将包含所有 上面提到的 SCSS 文件。
下载的工具包中的自定义图标
如果您在工具包中包含自定义图标,它们将作为工具包下载中的附加文件包含。
文件路径 | 文件的作用 |
---|---|
/webfonts/custom-icons.woff /webfonts/custom-icons.ttf | 自定义图标字体,以 WOFF2 和 TTF 格式提供 |
/scss/custom-icons.scss | Sass (SCSS) 预处理器片段,用于使用 Web 字体显示自定义图标 |
以下是一个简单的示例,该示例遵循 上述编译步骤 以及自定义图标
// importing core styling file@import './fontawesome/scss/fontawesome.scss';
// importing Font Awesome styles@import './fontawesome/scss/solid.scss';@import './fontawesome/scss/brands.scss';
// importing a kit's custom icons@import './fontawesome/scss/custom-icons.scss';
添加图标
在添加了上面的导入并设置了包含 Font Awesome 的编译后的 CSS 并 在您的项目中引用 后,您可以 将图标添加到项目的 HTML 中。
以下是如何使用编译后的托管 CSS 引用图标的示例
<head> <!--load your compiled CSS (including Font Awesome) --> <link href="/your-path-to-your-compiled-css-including-fontawesome/file.css" rel="stylesheet" /></head><body> <!-- This example uses <i> element with: 1. the `fa-solid` style class for solid style 2. the `user` icon with the `fa-` prefix --> <i class="fa-solid fa-user"></i>
<!-- Or you can use a <span> element, with classes applied in the same way -->
<span class="fa-solid fa-user"></span></body>
基于 SCSS 的自定义 CSS 规则
如果您想使用任何 SCSS 混合 或实用程序,您可以利用我们的 CSS 伪元素 方法将图标添加到您的项目中。当无法更改项目的 HTML 时,这种方法也很有用。
使用 @include fa-icon-
和 @include fa-family-
混合,您可以轻松地使用特定于项目的 CSS 选择器生成自定义 CSS 伪元素规则。
// Solid style of user.user { @include fa-icon-solid($fa-var-user);}
// Regular style of triangle-exclamation.triangle-exclamation { @include fa-icon-regular($fa-var-triangle-exclamation);}
// Light style of newspaper.newspaper { @include fa-icon-light($fa-var-newspaper);}
// Thin style of paintbrush-fine.paintbrush-fine { @include fa-icon-thin($fa-var-paintbrush-fine);}
// Duotone style of camera-retro.camera-retro { @include fa-icon-duotone($fa-var-camera-retro);}
// Twitter brand icon.twitter { @include fa-icon-brands($fa-var-twitter);}
// Sharp Solid style of trash.trash { @include fa-icon-solid($fa-var-trash); @include fa-family-sharp(); // define Sharp family}
// Sharp Regular style of trash.trash { @include fa-icon-regular($fa-var-trash); @include fa-family-sharp(); // define Sharp family}
// Classic Solid style of trash.trash { @include fa-icon-solid($fa-var-trash); @include fa-family-classic(
); // explicity setting Classic family (optional since this is Font Awesome's default)}
样式混合
要使用样式混合,您需要
- 选择适合您所需样式的混合。
- 传入要显示的图标的 SCSS 变量。
样式混合 | 可用性 | 呈现的样式 |
---|---|---|
fa-icon-brands() | 免费计划 | 品牌 |
fa-icon-solid() | 免费计划 | 实心 |
fa-icon-regular() | 专业版 | 常规 |
fa-icon-light() | 专业版 | 浅色 |
fa-icon-thin() | 专业版 | 细线 |
fa-icon-duotone() | 专业版 | 双色调 |
字体族混合
如果您使用我们的 mixin 在特定系列中渲染图标,例如我们的Sharp 风格系列,您需要引用正确的系列 mixin(例如 @include fa-family-sharp
)。Classic 是我们的默认风格系列,因此无需使用系列 mixin 设置它,除非您试图显式设置以覆盖之前的系列。
系列 Mixin | 可用性 | 呈现的样式 |
---|---|---|
fa-family-classic() | 免费计划 | Classic 与其他风格 mixin 一起使用时 |
fa-family-sharp() | 专业版 | Sharp 与其他风格 mixin 一起使用时 |
更手动的方式
如果您希望在编写样式规则时采用更可定制的方法,我们已将风格 mixin 中使用的实用程序提供给您,以获得相同的渲染结果。
要编写手动规则,您需要
- 通过添加
@extend %fa-icon;
来获取图标的核心设置样式。 - 使用
@extend .fa-solid;
包含在 Solid 风格中渲染图标的样式(其他风格的语法类似)。 - 使用
@extend .fa-classic;
或@extend .fa-sharp;
包含在特定系列中渲染图标的样式(Classic 默认设置)。 - 使用
fa-content
和变量名使包含图标的 Unicode 值更容易。这与通过伪元素添加图标 的语法类似。
// Solid style of user.user { @extend %fa-icon; @extend .fa-solid;
&:before { content: fa-content($fa-var-user); }}
// Regular style of triangle-exclamation.triangle-exclamation { @extend %fa-icon; @extend .fa-regular;
&:before { content: fa-content($fa-var-triangle-exclamation); }}
// Light style of newspaper.newspaper { @extend %fa-icon; @extend .fa-light;
&:before { content: fa-content($fa-var-newspaper); }}
// Thin style of paintbrush-fine.paintbrush-fine { @extend %fa-icon; @extend .fa-thin;
&:before { content: fa-content($fa-var-paintbrush-fine); }}
// Duotone style of camera-retro.camera-retro { @extend %fa-icon; @extend .fa-duotone;
&:before { content: fa-content($fa-var-camera-retro); }}
// Twitter brand icon.twitter { @extend %fa-icon; @extend .fa-brands;
&:before { content: fa-content($fa-var-twitter); }}
// Sharp Solid style of trash.trash { @extend %fa-icon; @extend .fa-solid; @extend .fa-sharp; // define Sharp family
&:before { content: fa-content($fa-var-trash); }}
// Classic Solid style of trash.trash { @extend %fa-icon; @extend .fa-solid; @extend .fa-classic; // explicity setting Classic family (optional since this is Font Awesome's default)
&:before { content: fa-content($fa-var-trash); }}
包含的混合
实用程序 | 它能做什么 |
---|---|
fa-sr-only() | 一个 mixin,它在视觉上隐藏包含图标文本等效项的元素,同时保持辅助技术可访问。 |
fa-sr-only-focusable() | 一个 mixin,与 fa-sr-only() 一起使用,当元素被聚焦时(例如,由键盘用户)显示该元素。 |
fa-icon() | 一个 mixin,包含图标的基本样式设置(不包括图标的样式和特定名称/unicode)编译。 |
fa-icon-solid() fa-icon-regular() fa-icon-light() fa-icon-thin() fa-icon-duotone() fa-icon-brands() | 包含图标的基本样式设置以及特定样式以在其中渲染图标的 mixin。 |
fa-family-sharp() fa-family-classic() | 当与 fa-icon- 样式 mixin 结合使用时,定义要使用的风格系列的 mixin。 |
fa-content() | 一个 mixin,使通过 CSS content 属性引用图标变得更容易。必须传入图标的变量(例如 fa-content($fa-var-user); )。 |
使用变量简化和自定义
Font Awesome 的 SCSS 版本还利用了几个 SCSS 变量,这些变量允许更轻松地设置和自定义我们的样式工具包。
变量 | 它能做什么 |
---|---|
$fa-css-prefix | 设置所有样式工具包 CSS 规则(例如 fa-lg )+ 图标引用类(例如 fa-user )中使用的前缀(默认设置为 fa )。 |
$fa-style | 设置默认图标样式(使用@font-face 权重)。 |
$fa-style-family | 设置使用的默认 font-family 。 |
$fa-display | 设置display 属性(默认设置为 inline-block )以渲染图标。 |
$fa-font-display | 为 Font Awesome 的图标字体设置font-display 属性。 |
$fa-fw-width | 为所有固定宽度 图标设置width 属性。 |
$fa-inverse | 设置 .fa-inverse 的color 属性。 |
$fa-border-color | 设置带边框的图标 中使用的border-color 属性。 |
$fa-border-padding | 设置带边框的图标 中使用的padding 属性。 |
$fa-border-radius | 设置带边框的图标 中使用的border-radius 属性。 |
$fa-border-style | 设置带边框的图标 中使用的border-style 属性。 |
$fa-border-width | 设置带边框的图标 中使用的border-width 属性。 |
$fa-li-width | 为 fa-li 元素设置width 属性,用于样式化列表中的图标 图标。 |
$fa-li-margin | 为 fa-li 元素设置margin-right 属性,用于样式化列表中的图标 图标。 |
$fa-pull-margin | 为拉伸的图标 图标设置margin-left /margin-right 属性。 |
$fa-primary-opacity | 设置双色调 图标的主层不透明度。 |
$fa-secondary-opacity | 设置双色调 图标的次层不透明度。 |
$fa-size-scale-base | 设置所有其他相对大小 步骤所基于的基本步长。 |
$fa-size-scale-2xs | 使用 .fa-2xs 相对调整图标大小 时,设置使用的步长大小。 |
$fa-size-scale-xs | 使用 .fa-xs 相对调整图标大小 时,设置使用的步长大小。 |
$fa-size-scale-sm | 使用 .fa-sm 相对调整图标大小 时,设置使用的步长大小。 |
$fa-size-scale-lg | 使用 .fa-lg 相对调整图标大小 时,设置使用的步长大小。 |
$fa-size-scale-xl | 使用 .fa-xl 相对调整图标大小 时,设置使用的步长大小。 |
$fa-size-scale-2xl | 使用 .fa-2xl 相对调整图标大小 时,设置使用的步长大小。 |
$fa-stack-vertical-align | 设置堆叠图标 的vertical-align 属性。 |
$fa-stack-width | 设置堆叠图标 的width 属性。 |
$fa-stack-z-index | 设置堆叠图标 的z-index 属性。 |
$fa-font-path | 设置 Font Awesome 的webfonts 文件夹和资产的位置。 |