Properly defining font-family in #font-face CSS rules - css

I recently came across the #font-family CSS rule as I was looking to use web fonts on my website.
Coming to the point, I've seen two variants in #font-family CSS code, which you can see below:
#font-face {
font-family: 'Droid Serif'; /* NOTE THIS LINE */
font-weight: normal; /* NOTE THIS LINE */
font-style: normal; /* NOTE THIS LINE */
src: url('DroidSerif-Regular-webfont.eot');
src: url('DroidSerif-Regular-webfont.eot?#iefix') format('embedded-opentype'),
local('Droid Serif'), local('DroidSerifRegular'),
url('DroidSerif-Regular-webfont.woff') format('woff'),
url('DroidSerif-Regular-webfont.ttf') format('truetype'),
url('DroidSerif-Regular-webfont.svg#DroidSerifRegular') format('svg');
}
#font-face {
font-family: 'Droid Serif'; /* NOTE THIS LINE */
font-weight: normal; /* NOTE THIS LINE */
font-style: italic; /* NOTE THIS LINE */
src: url('DroidSerif-Italic-webfont.eot');
src: url('DroidSerif-Italic-webfont.eot?#iefix') format('embedded-opentype'),
local('Droid Serif'), local('DroidSerifItalic'),
url('DroidSerif-Italic-webfont.woff') format('woff'),
url('DroidSerif-Italic-webfont.ttf') format('truetype'),
url('DroidSerif-Italic-webfont.svg#DroidSerifItalic') format('svg');
}
and this is another:
#font-face {
font-family: 'DroidSerifRegular'; /* NOTE THIS LINE */
font-weight: normal; /* NOTE THIS LINE */
font-style: normal; /* NOTE THIS LINE */
src: url('DroidSerif-Italic-webfont.eot');
src: url('DroidSerif-Italic-webfont.eot?#iefix') format('embedded-opentype'),
local('Droid Serif'), local('DroidSerifItalic'),
url('DroidSerif-Italic-webfont.woff') format('woff'),
url('DroidSerif-Italic-webfont.ttf') format('truetype'),
url('DroidSerif-Italic-webfont.svg#DroidSerifItalic') format('svg');
}
#font-face {
font-family: 'DroidSerifItalic'; /* NOTE THIS LINE */
font-weight: normal; /* NOTE THIS LINE */
font-style: normal; /* NOTE THIS LINE */
src: url('DroidSerif-Italic-webfont.eot');
src: url('DroidSerif-Italic-webfont.eot?#iefix') format('embedded-opentype'),
local('Droid Serif'), local('DroidSerifItalic'),
url('DroidSerif-Italic-webfont.woff') format('woff'),
url('DroidSerif-Italic-webfont.ttf') format('truetype'),
url('DroidSerif-Italic-webfont.svg#DroidSerifItalic') format('svg');
}
Compare the lines that I've commented with /* NOTE THIS LINE */
The first variant is by Google Web Fonts, while the second one is by Font Squirrel. So, is one of these two wrong? (Just wanted to confirm, although both are very reliable sources.)
If acceptable, which one of the two would I be better off with?

the first example, though hard to believe, is correct; notice how the Droid Serif Regular is being declared as font weight normal and font style normal...how you'd expect a regular font to be displayed. the second declaration, it's calling in Droid Serif Italic, and setting it to to font-style:italic; this allows you to use multiple fonts within a family. If you wanted to add a bold font, you'd apply the same #font-face rule except you'd change font-weight:bold, while leaving font-style:normal and declaring the same font family.
fontsquirrel really does a great job with rendering #font-face rules, actually i've read about this technique there. surprised it's not being implemented. you can read more about this here: http://www.456bereastreet.com/archive/201012/font-face_tip_define_font-weight_and_font-style_to_keep_your_css_simple/

Related

What's the correct way of using CSS3 font-face?

Could someone please help me understand what's the correct way of using CSS3 font-face. Below are some font-face declarations, which one is correct and why?
/* START OF SAMPLE CODE # 1 */
#font-face {
font-family: 'WebFont';
src: url('webfont.eot');
src: url('webfont.eot?#iefix') format('embedded-opentype'),
url('webfont.woff') format('woff'),
url('webfont.ttf') format('truetype'),
url('webfont.svg#WebFont') format('svg');
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: 'WebFont';
src: url('webfont-bold.eot');
src: url('webfont-bold.eot?#iefix') format('embedded-opentype'),
url('webfont-bold.woff') format('woff'),
url('webfont-bold.ttf') format('truetype'),
url('webfont-bold.svg#WebFont-Bold') format('svg');
font-weight: bold;
font-style: normal;
}
h1 {
font-family: 'WebFont', blah, blah, blah;
font-weight: bold;
}
h2 {
font-family: 'WebFont', blah, blah, blah;
font-weight: normal;
}
/* END OF SAMPLE CODE # 1 */
=========
/* START OF SAMPLE CODE # 2 */
#font-face {
font-family: 'WebFont';
src: url('webfont.eot');
src: url('webfont.eot?#iefix') format('embedded-opentype'),
url('webfont.woff') format('woff'),
url('webfont.ttf') format('truetype'),
url('webfont.svg#WebFont') format('svg');
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: 'WebFontBold';
src: url('webfont-bold.eot');
src: url('webfont-bold.eot?#iefix') format('embedded-opentype'),
url('webfont-bold.woff') format('woff'),
url('webfont-bold.ttf') format('truetype'),
url('webfont-bold.svg#WebFont-Bold') format('svg');
font-weight: normal;
font-style: normal;
}
h1 {
font-family: 'WebFontBold', blah, blah, blah;
font-weight: normal;
}
h2 {
font-family: 'WebFont', blah, blah, blah;
font-weight: normal;
}
/* END OF SAMPLE CODE # 2 */
=========
/* START OF SAMPLE CODE # 3 */
#font-face {
font-family: 'WebFont';
src: url('webfont.eot');
src: url('webfont.eot?#iefix') format('embedded-opentype'),
url('webfont.woff') format('woff'),
url('webfont.ttf') format('truetype'),
url('webfont.svg#WebFont') format('svg');
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: 'WebFontBold';
src: url('webfont-bold.eot');
src: url('webfont-bold.eot?#iefix') format('embedded-opentype'),
url('webfont-bold.woff') format('woff'),
url('webfont-bold.ttf') format('truetype'),
url('webfont-bold.svg#WebFont-Bold') format('svg');
font-weight: bold;
font-style: normal;
}
h1 {
font-family: 'WebFontBold', blah, blah, blah;
font-weight: bold;
}
h2 {
font-family: 'WebFont', blah, blah, blah;
font-weight: normal;
}
/* END OF SAMPLE CODE # 3 */
The difference in these code samples, is the way "Bold" font is being declared and used. Please advise, which one is correct from these and why?
PS: I am sorry for such a long question/sample. I just wanted to provide as much information as I could to avoid confusion.
Edit 1: Even though as mentioned by #Jukka in one of the answers below, the logical approach is to use "Code #1", but I just checked Safari and the bold font renders way too much bolder on Safari as compared to other browsers, but if I use "Code #2" all browsers render the same, so it does appear that there is something going on behind the scenes when using different font-face declarations. So at this moment I seem "Code #2" is the way to go, what say!
None of the codes is correct, since quotation marks are not allowed around the keywords bold and normal according to CSS syntax. Otherwise, code 1 is based on logical principles, since it specifies two typefaces, normal and bold, as being of the same font family, as they apparently are. The other codes declare each typeface as if it were a font family. Used with due caution, this approach works, too.
The main practical difference is that using the logical approach (code 1), you could simply declare font-family: WebFont, Foobar, even for all elements if you like, and the bold typeface would automatically be used for those elements that have font-weight: bold set on them (by browser defaults, like for h1, or by explicit setting). This also means that if the WebFont font family is not used for some reason (like browser setting that rejects downloadable fonts), the appropriate typeface (normal or bold) of the Foobar font would be used.

How to properly add '?i#efix' to a font path in rails?

It is recommended to add ?#iefix to the end of .eot font paths to fix yet another IE erratic behaviour :
#font-face {
font-family: 'MyWebFont';
src: url('webfont.eot'); /* IE9 Compat Modes */
src: url('webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('webfont.woff') format('woff'), /* Modern Browsers */
url('webfont.ttf') format('truetype'), /* Safari, Android, iOS */
url('webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
}
Is it possible to do that using asset_path, but without an ugly + "?#iefix" hack ?
This is how I use it in my projects. Works great in all browsers. It is important to break it to two #font-face declarations.
My fonts.scss:
#font-face {
font-family: 'Webfont';
src: font_url('webfont.eot?') format('embedded-opentype');
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: 'Webfont';
src: font_url('webfont.woff') format('woff'),
font_url('webfont.ttf') format('truetype'),
font_url('webfont.svg#Webfont') format('svg');
font-weight: normal;
font-style: normal;
}
Please note that if you want to use the SASS helper font_url, you have to have your fonts placed under the assets/fonts directory.
From my experience adding just ? to the font path for >=IE8 instead of anything starting ?# works. But, as mentioned in the beginning, keep the #font-face declaration for IE separate. For the use in SASS helper, just place it at the end of the asset name. It does not break the helper processing.
It appears that asset_path already takes this into account. So:
asset_path 'webfont.eot?#iefix'
does work out-of-the-box ! My bad...
Please try this:
Important:
Please verify your font name is added correctly in all places.
And please verify whether all font face files are available.
Add in Production.rb:
config.host_path = "http://---site url--"
Add following in CSS:
#font-face {
font-family: 'Webfont';
src: url('<%= Rails.application.config.host_path %>/assets/fonts/Webfont.eot');
src: url('<%= Rails.application.config.host_path %>/assets/fonts/Webfont.eot?#iefix') format('embedded-opentype'),
url('<%= Rails.application.config.host_path %>/assets/fonts/Webfont.woff') format('woff'),
url('<%= Rails.application.config.host_path %>/assets/fonts/Webfont.ttf') format('truetype'),
url('<%= Rails.application.config.host_path %>/assets/fonts/Webfont.svg#Webfont') format('svg');
font-weight: normal;
font-style: normal;
}
if you dont want hard coded font files path, then just use following in css.
#font-face {
font-family: 'Webfont';
src: url('Webfont.eot');
src: url('Webfont.eot?#iefix') format('embedded-opentype'),
url('Webfont.woff') format('woff'),
url('Webfont.ttf') format('truetype'),
url('Webfont.svg#Webfont') format('svg');
font-weight: normal;
font-style: normal;
}
We need to specify full URL path for access font files.
I hope it will help..

Is there a trigger I can use if a fallback font is used in CSS?

I'm using this service to create #font-face rules in my CSS. What I've done is created two rules, one for the normal weight font and another for the bold weight version. Like so:
#font-face
{
font-family: 'CenturyGothicRegular';
src: url('/static/fonts/gothic_2-webfont.eot');
src: url('/static/fonts/gothic_2-webfont.eot?#iefix') format('embedded-opentype'),
url('/static/fonts/gothic_2-webfont.woff') format('woff'),
url('/static/fonts/gothic_2-webfont.ttf') format('truetype'),
url('/static/fonts/gothic_2-webfont.svg#CenturyGothicRegular') format('svg');
font-weight: normal;
font-style: normal;
}
... and another for 'CenturyGothicBold'
I've then made the overall font for my site default back to Verdana like so:
body
{
font-family: "CenturyGothicRegular", Verdana;
font-size: 14px;
}
And made a little rule for <strong> so that rather than making the normal weight version bold (which just seems to stretch it), the bold weight version will be used:
strong
{
font-weight: normal;
font-family: 'CenturyGothicBold';
}
An issue I can foresee here is that if the font has defaulted to Verdana, bold won't be present.
Is there a way that I can specify a new set of rules for <strong> that only apply if the font has defaulted to Verdana?
There is no need to find a trigger to see if a fall back font has been used.
What you need to do is set the the font-weight in the #font-face rule, using the same family name. So you would now call it CenturyGothic:
#font-face {
font-family: 'CenturyGothic'; /* this used to be CenturyGothicRegular */
src: url('/static/fonts/gothic_2-webfont.eot');
src: url('/static/fonts/gothic_2-webfont.eot?#iefix') format('embedded-opentype'),
url('/static/fonts/gothic_2-webfont.woff') format('woff'),
url('/static/fonts/gothic_2-webfont.ttf') format('truetype'),
url('/static/fonts/gothic_2-webfont.svg#CenturyGothicRegular') format('svg');
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: 'CenturyGothic'; /* this used to be CenturyGothicBold but the urls still need to point to the files they were pointing at */
src: url('/static/fonts/gothic_2-webfont.eot');
src: url('/static/fonts/gothic_2-webfont.eot?#iefix') format('embedded-opentype'),
url('/static/fonts/gothic_2-webfont.woff') format('woff'),
url('/static/fonts/gothic_2-webfont.ttf') format('truetype'),
url('/static/fonts/gothic_2-webfont.svg#CenturyGothicRegular') format('svg');
font-weight: bold;
}
body{
font-family: "CenturyGothic", Verdana;
font-size: 14px;
}
strong{
font-weight: bold;
}
This will combine the two fonts into one font-family allowing it to act like any other font-family i.e. when you make it bold it will display the bold version of the font etc.
Using font-weight only with the same font-family will not work when you have several weight, like Light, ultralight, condensed bold, demi bold etc.

Why doesn't this #font-face style apply to my HTML?

I am hoping someone can please point out what's not working about this font-face implementation. I've testing this using the body tag so there's no question as to whether the issue is with the HTML (yes, I do have a body tag.)
I have obtained the fonts and the implementation CSS from FontSquirrel. Thanks for looking at this!
CSS:
#font-face {
font-family: 'DymaxionScriptRegular';
src: url('DymaxionScript-webfont.eot');
src: url('DymaxionScript-webfont.eot?#iefix') format('embedded-opentype'),
url('DymaxionScript-webfont.woff') format('woff'),
url('DymaxionScript-webfont.ttf') format('truetype'),
url('DymaxionScript-webfont.svg#DymaxionScriptRegular') format('svg');
font-weight: normal;
font-style: normal;
}
body {
font-family: DymaxionScriptRegular, courier;
}
File tree structure:
Screenshot of Courier fallback font:
Answer: Apparently if one leaves "Regular" off of the font-family declaration, everything works fine. Answered my own question by playing with it.
Real Answer: My first "answer" only worked in Safari. What worked in all of them was changing "'DymaxionScript-webfont..." to '../DymaxionScript-webfont... After I did that, everything worked. It was bad pathing.
Here is a more concise #font-face implementation, that I know works across the board:
#font-face {
font-family: 'DymaxionScriptRegular';
src: url('/path/to/font/webfont.eot?') format('eot'),
url('/path/to/font/webfont.woff') format('woff'),
url('/path/to/font/webfont.ttf') format('truetype'),
url('/path/to/font/webfont.svg#webfont') format('svg');
font-weight:normal;
font-style:normal;
}
For various web fonts you can check out google.com/webfonts which has a bunch of different fonts for use, but you just add a link to google's site and it will return the proper formatting for most browsers as far back as ie6
#font-face {
font-family: 'DymaxionScriptRegular';
src: url('webfont.eot');
src: url('webfont.eot?#iefix') format('embedded-opentype'),
url('webfont.woff') format('woff'),
url('webfont.ttf') format('truetype'),
url('webfont.svg#webfont') format('svg');
font-weight: normal;
font-style: normal;
}
body {
font-family: DymaxionScriptRegular, courier;
}

#font-face src: local - How to use the local font if the user already has it?

What is the right way to use #font-face so that the browser will not download the font if the user already have it?
I am using #font-face to define DejaVu, which is already installed on my system (linux).
Firefox is not downloading the font, but Chromium is downloading it every time!
My CSS code, based on font squirrel and that question look like this:
#font-face {
font-family: 'DejaVu Serif';
src: url('DejaVuSerif-webfont.eot');
src: local('DejaVu Serif'), url('DejaVuSerif-webfont.woff') format('woff'), url('DejaVuSerif-webfont.ttf') format('truetype'), url('DejaVuSerif-webfont.svg#webfontCFu7RF0I') format('svg');
font-weight: normal;
font-style: normal;
}
/* ... #font-face definitions for italic and bold omitted ... */
#font-face {
font-family: 'DejaVu Serif';
src: url('DejaVuSerif-BoldItalic-webfont.eot');
src: local('DejaVu Serif Bold Italic'), url('DejaVuSerif-BoldItalic-webfont.woff') format('woff'), url('DejaVuSerif-BoldItalic-webfont.ttf') format('truetype'), url('DejaVuSerif-BoldItalic-webfont.svg#webfontQAewh7pf') format('svg');
font-weight: bold;
font-style: italic;
}
If you want to check for local files first do:
#font-face {
font-family: 'Green Sans Web';
src:
local('Green Web'),
local('GreenWeb-Regular'),
url('GreenWeb.ttf');
}
There is a more elaborate description of what to do here.
In case someone still need it:
Download the font you need from fonts.google.com, then set your CSS file:
#font-face { font-family: roboto-regular; src: url('./fonts/Roboto-Regular.ttf'); }
body {
font-family: roboto-regular;
}
Note: make sure the path to the TTF file is correct.
Rather an old discussion, but is someone comes to this place, here some more infos.
goto https://google-webfonts-helper.herokuapp.com/fonts
select there the font(s) you need/want
downlaod the selected fonts
copy the displayed css
The copied css code may not be 100% correct, so below what I have been using (and works perfectly):
/* open-sans-regular - vietnamese_latin_hebrew_greek_cyrillic */
#font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url('fonts/open-sans-v34-vietnamese_latin_hebrew_greek_cyrillic-regular.eot'); /* IE9 Compat Modes */
src: local('😡');
src: url('fonts/open-sans-v34-vietnamese_latin_hebrew_greek_cyrillic-regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('fonts/open-sans-v34-vietnamese_latin_hebrew_greek_cyrillic-regular.svg#OpenSans') format('svg'), /* Legacy iOS */
url('fonts/open-sans-v34-vietnamese_latin_hebrew_greek_cyrillic-regular.ttf') format('truetype'), /* Safari, Android, iOS */
url('fonts/open-sans-v34-vietnamese_latin_hebrew_greek_cyrillic-regular.woff') format('woff'), /* Modern Browsers */
url('fonts/open-sans-v34-vietnamese_latin_hebrew_greek_cyrillic-regular.woff2') format('woff2'); /* Super Modern Browsers */
}
Inside the edited code you can delete the comments (after each url(..) line.
And finally - as tipp: pack the source, in general the packed file is ~ 30% smaller (and your website will have the benefit).
Edit: updated code snippet to be actual.
The font ordering is based on the fact, that browsers read from bottom to top (see https://stackoverflow.com/a/22835957/3450837).
It includes also the "bulletproof" solution where a smiley (which one you want to use is up to you) is used (to prevent wasting bandwidth) - see https://www.paulirish.com/2009/bulletproof-font-face-implementation-syntax/

Resources