Last active
August 29, 2015 14:13
-
-
Save jessehattabaugh/c8eafb4e54cd84f23e0b to your computer and use it in GitHub Desktop.
Stylus Mixin for defining and using webfonts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Esplaination | |
**************************************************************************** | |
This include represents a pattern for handling fonts which I have settled on after a lot of time | |
struggling to get fonts to render consistently across browsers. The jist of it is this; I don't | |
use font-weight or font-style. Instead I use a different font-family for each variation of the | |
font (ie. font-family: "FiraSans-Regular"; font-family: "FiraSans-BoldItalic"). The reason I do | |
this is that browsers don't always use the appropriate @font-face even when you declare them | |
with font-weight and font-size. This method allows me to control EXACTLY which font file is used. | |
*/ | |
/* Define the names of the font-families and corresponding files | |
* do not use these variables outside of this file | |
*****************************************************************************/ | |
$font = "FiraSans"; | |
$fontGeneric = sans-serif; | |
$fontFamilies = { | |
regular: $font + "-Regular", | |
bold: $font + "-Bold", | |
italic: $font + "-Italic", | |
boldItalic: $font + "-BoldItalic" | |
}; | |
/* font() mixin | |
* sets font related properties in a consistent way | |
* this is the only thing that gets used outside of this file | |
*****************************************************************************/ | |
font($weight = normal, $style = normal) { | |
// Decide which font-family to use | |
if ($weight == bold) { | |
if ($style == italic) { | |
font-family: $fontFamilies.boldItalic, $fontGeneric; | |
} else { | |
font-family: $fontFamilies.bold, $fontGeneric; | |
} | |
} else { | |
if ($style == italic) { | |
font-family: $fontFamilies.italic, $fontGeneric; | |
} else { | |
font-family: $fontFamilies.regular, $fontGeneric; | |
} | |
} | |
// the fonts already have the appropriate weight and style | |
// so these should always be normal to prevent double bolding | |
font-weight: normal; | |
font-style: normal; | |
letter-spacing: normal; | |
} | |
/* Create the @font-face declarations for each weight/style of font | |
* do not use these font-families manually, use the font() mixin instead | |
*****************************************************************************/ | |
for $i, $fontFamily in $fontFamilies { | |
// http://css-tricks.com/snippets/css/using-font-face/ | |
@font-face { | |
font-family: $fontFamily; | |
src: url($fontFamily + '.eot'); | |
src: url($fontFamily + '.eot?#iefix') format('embedded-opentype'), | |
url($fontFamily + '.woff') format('woff'), | |
url($fontFamily + '.ttf') format('truetype'); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment