Keywords: SASS | @font-face | cross-browser compatibility
Abstract: This article explores the correct implementation of @font-face declarations in SASS, analyzing common issues in converting CSS to SASS and providing best practices for cross-browser compatibility. By comparing solutions from different answers, it details SASS syntax features, font format compatibility handling, and how to optimize code structure with mixins, helping developers avoid pitfalls and improve efficiency.
Basic Syntax Conversion of @font-face in SASS
In CSS, the @font-face rule defines custom fonts, with standard syntax including properties like font-family and src. When converting such CSS code to SASS, developers often encounter syntax errors, typically due to misunderstandings of SASS indentation syntax and property declarations. An example of original CSS code is:
@font-face {
font-family: 'bingo';
src: url("bingo.eot");
src: local('bingo'),
url("bingo.svg#bingo") format('svg'),
url("bingo.otf") format('opentype');
}
In SASS, indentation should replace braces and semicolons, with the correctly converted code as follows:
@font-face
font-family: "bingo"
src: url('bingo.eot')
src: local('bingo')
src: url('bingo.svg#bingo') format('svg')
src: url('bingo.otf') format('opentype')
This SASS code compiles to output functionally equivalent to the original CSS, but note that multiple src declarations are allowed in SASS, providing a basis for cross-browser compatibility adjustments.
Challenges and Solutions for Cross-Browser Compatibility
While the above conversion works in many cases, font rendering may be inconsistent in real cross-browser environments, especially in older browsers like OSX Firefox 23. This highlights the need for more robust @font-face implementations. A widely accepted cross-browser solution from Font Squirrel has CSS code like:
@font-face {
font-family: 'fontname';
src: url('fontname.eot');
src: url('fontname.eot?#iefix') format('embedded-opentype'),
url('fontname.woff') format('woff'),
url('fontname.ttf') format('truetype'),
url('fontname.svg#fontname') format('svg');
font-weight: normal;
font-style: normal;
}
When implementing this in SASS, the key is handling comma-separated values in the src property. Since SASS does not support line breaks within property values, all values must be written on one line:
@font-face
font-family: 'fontname'
src: url('fontname.eot')
src: url('fontname.eot?#iefix') format('embedded-opentype'), url('fontname.woff') format('woff'), url('fontname.ttf') format('truetype'), url('fontname.svg#fontname') format('svg')
font-weight: normal
font-style: normal
This method ensures compatibility but may result in overly long lines, affecting readability.
Optimizing @font-face Declarations with Mixins
To improve code maintainability and reusability, a SASS mixin can be defined to encapsulate @font-face logic. For example:
=font-face($family, $path, $svg, $weight: normal, $style: normal)
@font-face
font-family: $family
src: url('#{$path}.eot')
src: url('#{$path}.eot?#iefix') format('embedded-opentype'), url('#{$path}.woff') format('woff'), url('#{$path}.ttf') format('truetype'), url('#{$path}.svg##{$svg}') format('svg')
font-weight: $weight
font-style: $style
Using this mixin, developers can concisely declare fonts, such as:
+font-face('frutigerlight', '../fonts/frutilig-webfont', 'frutigerlight')
This reduces code duplication, enhances development efficiency, and maintains cross-browser compatibility.
Conclusion and Best Practice Recommendations
When implementing @font-face in SASS, follow these best practices: first, ensure correct use of SASS indentation syntax to avoid common conversion errors; second, adopt a cross-browser compatible font format order, such as EOT, WOFF, TTF, and SVG, to cover diverse environments; finally, utilize mixins or functions to encapsulate complex logic, improving code readability and maintainability. By combining these methods, developers can effectively integrate custom fonts in SASS projects while addressing varied browser requirements.