A mixin for writing @font-face rules in SASS. Embedded OpenType, WOFF2, WOFF, TrueType, and SVG files are automatically sourced.
Simplest scenario using default values:
$font_base_path: '/your/font/directory';
@include font-face(
Samplino,
$font_base_path + '/Samplino - Book',
);Rendered as CSS:
@font-face {
font-family: "Samplino";
src: url("/your/font/directory/Samplino - Book.eot");
src: url("/your/font/directory/Samplino - Book.eot?#iefix") format("eot"), url("/your/font/directory/Samplino - Book.woff2") format("woff2"), url("/your/font/directory/Samplino - Book.woff") format("woff"), url("/your/font/directory/Samplino - Book.ttf") format("truetype");
font-style: normal;
font-weight: normal;
display: swap;
}More complex – create a @font-face rule that applies to bold and italic text, omits the eot format, and uses a fallback display strategy:
@include font-face(
"Samplina Neue",
$font_base_path + '/Samplina Neue - Bold Oblique',
bold,
italic,
fallback,
woff2 woff ttf svg
);Rendered as CSS:
@font-face {
font-family: "Samplina Neue";
src: url("/your/font/directory/Samplina Neue - Bold Oblique.woff2") format("woff2"), url("/your/font/directory/Samplina Neue - Bold Oblique.woff") format("woff"), url("/your/font/directory/Samplina Neue - Bold Oblique.ttf") format("truetype"), url("/your/font/directory/Samplina Neue - Bold Oblique.svg#Samplina_Neue") format("svg");
font-style: italic;
font-weight: bold;
display: fallback;
}You can pass null values to prevent properties from being renderd. Example – create a @font-face rule that only sources a WOFF and SVG file.
@include font-face(
Samplinoff,
$font_base_path + '/Samplinoff', null, null, null, woff svg);Rendered as CSS:
@font-face {
font-family: "Samplinoff";
src: url("/your/font/directory/Samplinoff.woff") format("woff"),
url("/your/font/directory/Samplinoff.svg") format("svg");
}This snippet is forked from the original (props for getting it started) and has the following improvements:
-
Fixed support for legacy browsers all the way back to IE6. God help you if you actually need to support that browser.
-
Added the
displayproperty. Read more about it here.
Notes from the original snippet:
-
IE≥9 prioritizes valid font formats over invalid ones. Therefore, while
embedded-opentypeis the correct format for an .eot font,eotis used to fool modern IE into prioritizing other, newer font formats. -
IE≤8 only supports .eot fonts and parses the
srcproperty incorrectly, interpreting everything between the first opening parenthesis(and the last closing parenthesis)as a single URL. Therefore, a?is appended to the .eot’s URL, fooling older IE into reading all other sources as query parameters.