Legacy article
This article describe how you can force a browser to use a specific font while displaying a question in a survey; or as specific question in an HTML formatted email invitation.
The trick is to use the command @font-face
in the CSS style sheet. (Usually the layout template).
The @font-face
rule allows custom fonts to be loaded on a webpage. Once added to a stylesheet, the rule instructs the browser to download the font from where it is hosted, then display it as specified in the CSS.
Without the rule, our designs are limited to the fonts that are already loaded on a user's computer, which vary depending on the system being used.
Deepest Possible Browser Support
This is the method with the deepest support possible right now. The @font-face
rule should be added to the stylesheet before any styles.
@font-face {
font-family: 'MyWebFont';
src: url('webfont.eot'); /* IE9 Compat Modes */
src: url('webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('webfont.woff2') format('woff2'), /* Super Modern Browsers */
url('webfont.woff') format('woff'), /* Pretty Modern Browsers */
url('webfont.ttf') format('truetype'), /* Safari, Android, iOS */
url('webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
}
Then use it to style elements like this:
body {
font-family: 'MyWebFont', Fallback, sans-serif;
}
Alternative Techniques
@import
While @font-face
is excellent for fonts that are hosted on our own servers, there may be situations where a hosted font solution is better. Google Fonts offers this as a way to use their fonts. The following is an example of using @import
to load the Open Sans font from Google Fonts:
@import url(//fonts.googleapis.com/css?family=Open+Sans);
Then we can use it to style elements:
body {
font-family: 'Open Sans', sans-serif;
}
If you open the URL for the font, you can actually see all the @font-face
work being done behind the scenes.
A benefit of using a hosted service is that it is likely to include all the font file variations, which ensures deep cross-browser compatibility without having to host all those files ourselves.
Comments
0 comments
Article is closed for comments.