例如- 如果我想要使用这个图标: http://fortawesome.github.io/Font-Awesome/icon/pencil/ 我了解你可以像这样在HTML中使用它:
<i class="fa fa-pencil"></i>
然而,我希望能够做到以下类似的事情:
.class-name {
@include: fa-icon(fa-pencil);
}
这该怎么做才是默认/正确的方式呢?
<i class="fa fa-pencil"></i>
.class-name {
@include: fa-icon(fa-pencil);
}
我不确定是否有人会发现这个有用,但是我创建了一个Mixin来扩展.fa
类而不是编辑现有的@mixin fa-icon()
自定义Mixin
@mixin icon($icon) {
@include fa-icon;
@extend .fa;
@extend .fa-#{$icon}:before;
}
用法
.foo:before {
@include icon(pencil);
}
< p > < em > 这里引用了一个图标,它的名称为“pencil”(不包括< code >fa -):https://fortawesome.github.io/Font-Awesome/icons/
Font Awesome 5 的更新:
@mixin icon($icon) {
@include fa-icon;
@extend .fas;
@extend .fa-#{$icon};
}
我最终解决了这个问题。有几种方法可以实现,非常相似:
选项一
通过在现有的@mixin fa-icon()
中添加一个参数来进行编辑,例如它会像这样:
@mixin fa-icon($icon) {
display: inline-block;
font: normal normal normal #{$fa-font-size-base}/1 FontAwesome; // shortening font declaration
font-size: inherit; // can't have font-size inherit on line above, so need to override
text-rendering: auto; // optimizelegibility throws things off #1094
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0); // ensures no half-pixel rendering in firefox
@extend .fa;
@extend .fa-#{$icon};
}
用法:
.class-name {
@include fa-icon(facebook);
font-size: 50px;
}
选项2:
使用这个自定义的mixin: http://genesy.me/font-awesome-mixin/
@import "path_name/@fortawesome/fontawesome-free/scss/fontawesome";
这行代码涉及编程相关内容。
.class-name{
&::before {
@include fa-icon;
font-family: "Font Awesome 5 Free";
content: fa-content($fa-var-pencil);
}
}
在 fontawesome.scss 中:mixin @include fa-icon 将所有必要的图标属性放置。
在 fontawesome 的 variables.scss 中,fa-content 函数格式化未引用的字体代码:
在 variables.scss 中,每个图标都有一个变量内容,例如铅笔
$fa-var-pencil: \f040;
只需添加字体系列: font-family: "Font Awesome 5 Free"; 或 font-family: "Font Awesome 5 Pro"; 或 font-family: "Font Awesome 5 solid"; 或 font-family: "Font Awesome 5 regular"; 或 font-family: "Font Awesome 5 light"; 或 font-family: "Font Awesome 5 brands"; 或
要查看所有可用字体,请查看 fontawesome 的 webfonts 目录。
从Font Awesome 5.x开始,使用npm包(@fortawesome/fontawesome-free)的实施者可以利用该分发中包含的SCSS文件来实现此目标:
/node_modules/@fortawesome/fontawesome-free/scss/
_mixins.scss
: 提供了一个 fa-icon
mixin,包含所需的基础样式。_variables.scss
: 为每个图标提供变量,对应其 Unicode 字符(例如,$fa-var-stack-overflow
对应 \f16c
)。regular.scss
、solid.scss
等:为每种字体提供 @font-face
声明。首先,导入相应的文件:
@import "../node_modules/@fortawesome/fontawesome-free/scss/_mixins.scss";
@import "../node_modules/@fortawesome/fontawesome-free/scss/_variables.scss";
@import "../node_modules/@fortawesome/fontawesome-free/scss/regular.scss";
fa-icon
mixin、适当的字体系列和要实现的图标的变量:.selector {
&::before {
@include fa-icon;
font-family : 'Font Awesome 5 Free';
content : fa-content($fa-var-stack-overflow);
}
}