Font Face
gls- namespace (e.g. @include gls-font-face();).Important: The mixin must be called at the root level of your style sheet.
Arguments
| Name | Type | Description |
|---|---|---|
$font-family | string | Sets the name of the |
$file-path | string | Sets the path and the name of the font file. The file name must be written without the extension. |
$font-style | string | Sets the |
$font-weight | number | Sets the weight of the fonts. The default value is set to |
$file-formats | string | list | Sets the font file formats that you would like to include. The default value is set to |
* To learn more about the font-weight property values checkout the links at the end of the article.
Examples
Let’s call the mixin and pass values for the required arguments: $font-family and $file-path.
@include font-face("Fanwood Text", "fonts/fanwood-text/fanwood-text-regular");//CSS Output
@font-face {
font-family: "Fanwood Text";
src: url("fonts/fanwood-text/fanwood-text-regular.eot");
src: url("fonts/fanwood-text/fanwood-text-regular.eot?#iefix") format("embedded-opentype"),
url("fonts/fanwood-text/fanwood-text-regular.woff2") format("woff2"),
url("fonts/fanwood-text/fanwood-text-regular.woff") format("woff"),
url("fonts/fanwood-text/fanwood-text-regular.ttf") format("truetype"),
url("fonts/fanwood-text/fanwood-text-regular.svg#FanwoodText") format("svg");
font-style: normal;
font-weight: 400;
}Now let’s add the italic version of the family.
@include font-face("Fanwood Text", "fonts/fanwood-text/fanwood-text-regular", italic); | |
Now let’s add the bold version of the Fanwood Text font-family (if there is one) by passing 700 for the $font-weight argument.
@include font-face("Fanwood Text", "fonts/fanwood-text/fanwood-text-bold", 700); | |
If you pass values for both $font-style and $font-weight arguments, the value for $font-weight argument should always be at the end unless you pass values for the $file-formats.
@include font-face("Fanwood Text", "fonts/fanwood-text/fanwood-text-bold-italic", italic, 700);//CSS Output
@font-face {
font-family: "Fanwood Text";
src: url("fonts/fanwood-text/fanwood-text-bold-italic.eot");
src: url("fonts/fanwood-text/fanwood-text-bold-italic.eot?#iefix") format("embedded-opentype"),
url("fonts/fanwood-text/fanwood-text-bold-italic.woff2") format("woff2"),
url("fonts/fanwood-text/fanwood-text-bold-italic.woff") format("woff"),
url("fonts/fanwood-text/fanwood-text-bold-italic.ttf") format("truetype"),
url("fonts/fanwood-text/fanwood-text-bold-italic.svg#FanwoodText") format("svg");
font-style: italic;
font-weight: 700;
}Things are getting trickier when you want to pass a value for$file-formats. In the below examples you can see the ways to pass a value for$file-formats.
Suppose you only have three file formats: woff2, woff, and ttf.
@include font-face("Fanwood Text", "fonts/fanwood-text/fanwood-text-regular", woff2 woff ttf);//CSS Output
@font-face {
font-family: "Fanwood Text";
src: url("fonts/fanwood-text/fanwood-text-regular.woff2") format("woff2"),
url("fonts/fanwood-text/fanwood-text-regular.woff") format("woff"),
url("fonts/fanwood-text/fanwood-text-regular.ttf") format("truetype");
font-style: normal;
font-weight: 400;
}If you want to pass values for all the arguments the order must be like this: $font-name, $file-path, font-style, $font-weight, $file-formats like in the example below.
@include font-face("Fanwood Text", "fonts/fanwood-text/fanwood-text-bold-italic", italic, 700, woff2 woff);//CSS Output
@font-face {
font-family: "Fanwood Text";
src: url("fonts/fanwood-text/fanwood-text-bold-italic.woff2") format("woff2"),
url("fonts/fanwood-text/fanwood-text-bold-italic.woff") format("woff");
font-style: italic;
font-weight: 700;
}If you are having trouble with passing the arguments by their position try named arguments.
Tip: Do you know what’s beautiful about named arguments? They’re order-independent, therefore so useful in such cases where it is difficult to stick to the order of arguments.@include font-face (
$font-family: "Fanwood Text",
$file-path: "fonts/fanwood-text/fanwood-text-bold-italic",
$font-style: italic,
$font-weight: 700,
$file-formats: eot woff2 woff
);//CSS Output
@font-face {
font-family: "Fanwood Text";
src: url("fonts/fanwood-text/fanwood-text-bold-italic.eot");
src: url("fonts/fanwood-text/fanwood-text-bold-italic.eot?#iefix") format("embedded-opentype"),
url("fonts/fanwood-text/fanwood-text-bold-italic.woff2") format("woff2"),
url("fonts/fanwood-text/fanwood-text-bold-italic.woff") format("woff");
font-style: italic;
font-weight: 700;
}Try to change the order of the arguments (whatever the order you use, the result will be the same).
@include font-face (
$font-weight: 700,
$font-style: italic,
$file-formats: eot woff2 woff,
$font-family: "Fanwood Text",
$file-path: "fonts/fanwood-text/fanwood-text-bold-italic",
);//CSS Output
@font-face {
font-family: "Fanwood Text";
src: url("fonts/fanwood-text/fanwood-text-bold-italic.eot");
src: url("fonts/fanwood-text/fanwood-text-bold-italic.eot?#iefix") format("embedded-opentype"),
url("fonts/fanwood-text/fanwood-text-bold-italic.woff2") format("woff2"),
url("fonts/fanwood-text/fanwood-text-bold-italic.woff") format("woff");
font-style: italic;
font-weight: 700;
}Now let’s use the @content directive to pass a content block into the mixin.
@include font-face ("Fanwood Text", "fonts/fanwood-text/fanwood-text-regular") {
font-display: fallback;
} | |
If you don’t have all the font varieties, or you have a font and you want to convert it to a web font (@font-face embeddable), try Font Squirrel‘s Webfont Generator.