Define one font face for several font files with different weight - css

I came across this issue using custom fonts. Many offer several files to support different font weights and italics, so you will define a font face like:
#font-face {
font-family: 'WebFont Bold';
src: url('myfont.woff') format('woff'), /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
url('myfont.ttf') format('truetype'); /* Chrome 4+, Firefox 3.5, Opera 10+, Safari 3—5 */
}
Where Bold will be replaced with different font specific words like italic or light. I was wondering is it possible to define one font face that will use specific font, say bold if somewhere in css font-weight: bold; is set?

The correct way to do it is like this:
#font-face {
font-family: 'WebFont';
src: url('myfont_regular.woff')....
}
#font-face {
font-family: 'WebFont';
font-weight: bold;
src: url('myfont_bold.woff')....
}

You can set the same name for different font like:
#font-face {
font-family: 'MyFont';
src: url('myfont.woff') format('woff'),
url('myfont.ttf') format('truetype');
font-weight: normal;
}
#font-face {
font-family: 'MyFont';
src: url('myfont-bold.woff') format('woff'),
url('myfont-bold.ttf') format('truetype');
font-weight: bold;
}
And using like:
p {
font-family: 'MyFont';
}
strong {
font-family: 'MyFont';
font-weight: bold;
}

Related

How to override the default font using locally stored fonts in themes?

I am using Quarto to build a website and try to override the default fonts within a theme. (My overall goal is to use local google fonts instead of using the google api).
my _quarto.yml looks like that:
project:
type: website
format:
html:
theme:
light: [flatly, light.scss]
the light.scss does look like that. All fonts are in fonts/
/*-- scss:defaults --*/
/* lato-regular - latin-ext_latin */
#font-face {
font-display: swap;
font-family: 'Lato';
font-style: normal;
font-weight: 400;
src: url('fonts/lato-v23-latin-ext_latin-regular.woff2') format('woff2'), /* Chrome 36+, Opera 23+, Firefox 39+ */
url('fonts/lato-v23-latin-ext_latin-regular.woff') format('woff'); /* Chrome 5+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}
I am using the developer mode in chromium to investigate, if the local files are used. Unfortunately, my custom.scss i.e.,(light.scss) is not able to override the default configuation.
How is it possible to override the use of the api and use local fonts instead?
updated answer based on this discussion
At first, explicitly disable the path to webfonts that flatly theme is using by overriding the Sass variable $web-font-path (by giving a bad value to it like $web-font-path: "No").
Secondly, though You have defined a #font-face, you have not used that. You need to tell quarto to use that font and you can do it by defining Sass variables $font-family-sans-serif (use it to define the sans-serif font family for the page) or $font-family-monospace (use it to define monospace font family for the page) in the light.scss file.
Finally, an important thing to note that, Sass variable declaration need to go under the /*-- scss:defaults --*/ layer and #font-face declaration need to go under the /*-- scss:rules --*/ layer.
Therefore the light.scss file should look like,
/*-- scss:defaults --*/
$font-family-sans-serif: "Lato";
$font-family-monospace: "Lato";
$bs-body-font-family: "Lato";
$web-font-path: "No";
/*-- scss:rules --*/
/* lato-regular - latin-ext_latin */
#font-face {
font-display: swap;
font-family: "Lato";
font-style: normal;
font-weight: 400;
src: url("./fonts/lato-v23-latin-ext_latin-regular.woff2") format("woff2"),
url("./fonts/lato-v23-latin-ext_latin-regular.woff") format("woff");
}
/* lato-italic - latin-ext_latin */
#font-face {
font-display: swap;
font-family: "Lato";
font-style: italic;
font-weight: 400;
src: url("./fonts/lato-v23-latin-ext_latin-italic.woff2") format("woff2"),
url("./fonts/lato-v23-latin-ext_latin-italic.woff") format("woff");
}
/* lato-700 - latin-ext_latin */
#font-face {
font-display: swap;
font-family: "Lato";
font-style: normal;
font-weight: 700;
src: url("./fonts/lato-v23-latin-ext_latin-700.woff2") format("woff2"),
url("./fonts/lato-v23-latin-ext_latin-700.woff") format("woff");
}
/* lato-700italic - latin-ext_latin */
#font-face {
font-display: swap;
font-family: "Lato";
font-style: italic;
font-weight: 700;
src: url("./fonts/lato-v23-latin-ext_latin-700italic.woff2") format("woff2"),
url("./fonts/lato-v23-latin-ext_latin-700italic.woff") format("woff");
}
The scss code for the Flatly theme starts with:
$web-font-path: "https://fonts.googleapis.com/css2?family=Lato:ital,wght#0,400;0,700;1,400&display=swap" !default;
#if $web-font-path {
#import url($web-font-path);
}
Iterating on #shafee's answer, by putting the following in light.scss
/*-- scss:defaults --*/
#font-face {
font-display: swap;
font-family: 'Lato';
font-style: normal;
font-weight: 400;
src: url('fonts/Lato-Regular.woff2') format('woff2'), /* Chrome 36+, Opera 23+, Firefox 39+ */
url('fonts/Lato-Regular.woff2') format('woff'); /* Chrome 5+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}
$font-family-sans-serif: 'Lato';
$web-font-path: "fonts/Lato-Regular.woff2";
everything gets loaded locally.
You can use embed-resource: TRUE in the header of index.qmd:
This will include everything, javascript, css etc. in a stand-alone version, and there will be no CSS linking to Google fonts.
---
title: "GoogleFontIssue"
embed-resources: true
---

#font-face is not working on IE 11. #import is not working well in Chrome. Why?

I'm trying to get Titillium-Wen, a Google Font, to work on all broswers. I prefer to self-host the font files.
The following code works well on Edge, Safari and Chrome, but not on IE 11. Text is displayed in a backup font as if IE 11 can not find the font:
#font-face {
font-family: 'Titillium Web';
src: url('/fonts/titillium/TitilliumWeb-Regular.ttf') format('truetype');
font-weight: 400;
font-style: normal;
}
#font-face {
font-family: 'Titillium Web';
src: url('/fonts/titillium/TitilliumWeb-Italic.ttf') format('truetype');
font-weight: 400;
font-style: italic;
}
#font-face {
font-family: 'Titillium Web';
src: url('/fonts/titillium/TitilliumWeb-SemiBold.ttf') format('truetype');
font-weight: 600;
font-style: normal;
}
#font-face {
font-family: 'Titillium Web';
src: url('/fonts/titillium/TitilliumWeb-Bold.ttf') format('truetype');
font-weight: 700;
font-style: normal;
}
The code below works on IE 11 but does not provide bold and italic fonts in Chrome. And anyway, I prefer to host the fonts myself:
#import url(https://fonts.googleapis.com/css?family=Titillium+Web);
Can I get the #font-face code to work on IE 11?
Try to use different font formats, as not all browsers support TTF.
#font-face{
font-family:'My Font';
src:url('../fonts/myfont.woff2') format('woff2'),
url('../fonts/myfont.woff') format('woff'),
url('../fonts/myfont.ttf') format('truetype');
font-weight:normal;
font-style:normal;
}
There are web services that will convert the font to different file formats for you. As far as I am concerned, using WOFF2, WOFF and TT covers all the browser versions I need to support.

Roboto Condensed Font not rendering properly in mozilla firefox

I am using RobotoCondensed downloaded from google Web Fonts. I am using the font from my server. I have created the css like below.
#font-face {
font-family: 'Roboto Condensed';
src: url('../fonts/RobotoCondensed-Light.eot');
src: url('../fonts/RobotoCondensed-Light.eot?#iefix') format('embedded-opentype'),
url('../fonts/RobotoCondensed-Light.woff') format('woff'),
url('../fonts/RobotoCondensed-Light.ttf') format('truetype'),
url('../fonts/RobotoCondensed-Light.svg#svgFontName') format('svg');
}
All the font types specified are present in the folder that I have specified in the url.
I am using the font family like this
font-family: 'Roboto Condensed', sans-serif;
The things are going smooth with respect to IE9 and Chrome. But when it comes to Mozilla Firefox it is not so.
Is there any solution for this?
I'm using the one off the Google Fonts Api and it works in Firefox:
#font-face {
font-family: 'Roboto Condensed';
font-style: normal;
font-weight: 400;
src: local('Roboto Condensed Regular'), local('RobotoCondensed-Regular'), url(http://themes.googleusercontent.com/static/fonts/robotocondensed/v7/Zd2E9abXLFGSr9G3YK2MsNxB8OB85xaNTJvVSB9YUjQ.woff) format('woff');
}
Maybe try specifying style and weight.
just remove : font-weight: 300; from woff

Why Firefox ignores custom font from #font-face?

Let’s say we have a font-face section like that
#font-face {
font-family: 'F';
src: url("/fonts/F.eot?") format("embedded-opentype");
src: local('?'),
url("/fonts/F.woff") format("woff"),
url("/fonts/F.ttf") format("truetype"),
url("/fonts/F.svg") format("svg");
font-weight: normal;
font-style: normal;
}
and a body style
body {
font-family: 'F' /*, sans-serif*/;
}
Now, if I uncomment sans-serif it will take priority over custom font despite the fact it is mentioned at the end. Why? How do I specify a back-up variant for those who can’t use downloadable webfonts?
===upd===
If in doubt why local('?') is used, look here http://paulirish.com/2009/bulletproof-font-face-implementation-syntax/#smiley
I was wrong in my thinking that it is sans-serif breaking my CSS, here is actual code that shows custom 'F' font. If 'Trebuchet MS' will be uncommented, font-family drops to sans.
body {
font-family: 'F' /*, 'Trebuchet MS'*/ , sans-serif;
}
Your syntax seems to be slightly incorrect. The '?' at the end of the first 'src' property value is aimed towards fixing IE versions 6 through 8, but could actually be affecting Firefox. The correct syntax to use is:
#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 */
}
Source: http://www.fontspring.com/blog/further-hardening-of-the-bulletproof-syntax

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.

Resources