font-family browser selection - css

I'm trying to set the browser to use the Quicksand font which I am pulling directly from a file in the same folder. When I format the code as follows the browser uses the Quicksand font:
#charset "utf-8";
#font-face {
font-family: Quicksand;
src: url('Quicksand-Regular.woff') format ('woff'),
url('Quicksand-Regular.tff') format('truetype');
}
h1, h2 {
font-family: Quicksand;
}
When I add more options for the font-family the browser defaults to the sans-serif font rather than using the Quicksand font.
#charset "utf-8";
#font-face {
font-family: Quicksand;
src: url('Quicksand-Regular.woff') format ('woff'),
url('Quicksand-Regular.tff') format('truetype');
}
h1, h2 {
font-family: Quicksand, sans-serif;
}
I'm trying to get the browser to default to Quicksand font with sans-serif as backup for an assignment and I can't figure out what exactly I'm doing wrong in the above code.

The font family of a text is set with the font-family property.
The font-family property should hold several font names as a "fallback" system. If the browser does not support the first font, it tries the next font, and so on.
Start with the font you want, and end with a generic family, to let the browser pick a similar font in the generic family if no other fonts are available.
Note: If the name of a font family is more than one word, it must be in quotation marks, like "Times New Roman".
More than one font family is specified in a comma-separated list:
p {
font-family: "Quicksand", sans-serif;
}

Related

Google Webfonts in PDF generated by DOMPDF

I am using two webfonts in a page that I convert to a PDF using dompdf. I have this in the header:
<link href='http://fonts.googleapis.com/css?family=Signika:600|Roboto+Condensed' rel='stylesheet' type='text/css'>
I then use them in CSS rules like
body {
font-family: "Roboto Condensed", sans-serif;
[ ... ]
}
h1 {
font-family:'Signika', Arial, Helvetica, sans-serif;
[ ... ]
}
Now, when I generate the PDF, the h1 is displayed with the "Signika" font, but "Roboto Condensed" is replaced by Helvetica or some other standard sans-serif font.
If I open the "preview" file (i.e. the php page which I then include in the PDF generation script), "Roboto Condensed" is displayed as expected, but it doesn't make it into the PDF. But as I wrote, "Signika" is there in the PDF, and that's somehow odd to me. BTW, I also tried to include the font-face rule directly in CSS rules for p, div, li etc. but that wouldn't change anything.
Any suggestions how I could fix that?
EDIT/ADDITION:
Thinking about it, a difference between the two fonts is that Roboto Condensed has a space in its name. I wonder if that could cause the problem (i.e. dompdf not being able to handle such a font name)? But I can't change that as long as I am fetching the fonts from the Google server.
I found the solution myself:
As I had added to my question in an edit, the reason obviously was that the font-family name "Roboto Condensed" contains a space, which dompdf doesn't seem to like.
I downloaded the font, created three versions of it with the font generator on Fontsquirrel and put them on my server, together with this stylesheet:
#font-face {
font-family: 'roboto_condensedregular';
src: url('robotocondensed-regular-webfont.woff2') format('woff2'),
url('robotocondensed-regular-webfont.woff') format('woff'),
url('RobotoCondensed-Regular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
Then, in my CSS rules I used that new font name roboto_condensedregular in font-family: roboto_condensedregular, sans-serif;
Now it works, also in the PDF.
You don't need to actually do all of this. Simply use the #importoption to embed the font in your html. Works like a charm using laravel-dompdf.
screenshot

Adding fallback fonts to the #font-face definition

Is it possible to add a fallback font directly to the definition of the font-face?
Example:
#font-face {
font-family: 'MyWebFont', Arial, Helvetica, sans-serif;
src: url('fonts/MyWebFont.eot');
src: url('fonts/MyWebFont.eot?#iefix') format('embedded-opentype'),
url('fonts/MyWebFont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
And then using it as font-family value with automatic fallback, like so:
p {
font-family: MyWebFont;
}
My goal is not to having to define the fallback fonts everywhere I define a new font-family. If not like above, can I somehow achieve this without JavaScript? Thanks for your help!
No, you cannot specify any fallback fonts inside a #font-face rule, because such a rule defines a font face and assigns a name to it. Inside the rule, the font-family part can contain only one name, the name you choose to assign. It would be pointless list several names there, since only the first one can possibly matter (and, besides, in this context no name has any predefined meaning, e.g. Arial would not mean the Arial font but be just an arbitrary assigned name).
Fallback fonts can be specified only in normal font-family rules.
Consider organizing your style sheet so that the relevant font-family list appears only once, using a suitable list of selectors, like
p, blockquote, .foobar, .something {
font-family: MyWebFont, Arial, Helvetica, sans-serif;
}
You can totally add fallback fonts to a #font-face rule!* You don't add them to the font-family descriptor (that's for giving your font family a name); you add them to the src descriptor, which accepts multiple values. If the browser can't find (or doesn't support) the first font, it will try loading the next one, and so on. You can have it look for fonts installed on the user's system using the local() function:
#font-face {
font-family: bodytext;
src: url(fonts/MyWebFont.woff) format("woff"),
local(Arial),
local(Helvetica);
}
Some people may argue that url() and local() weren't designed to be used this way. Typically, they're used to provide local and remote versions of the same font, with the web-font functioning as a fallback if the local font can't be found. Here's such an example from the W3C specs:
#font-face {
font-family: MyGentium;
src: local(Gentium), /* use locally available Gentium */
url(Gentium.woff); /* otherwise, download it */
}
But there's nothing to say you can't use it in place of a regular font stack. Check out this W3C example:
Create an alias for local Japanese fonts on different platforms:
#font-face {
font-family: jpgothic;
src: local(HiraKakuPro-W3), local(Meiryo), local(IPAPGothic);
}
*There are some caveats though. Don't expect this to work exactly like the familiar font-family stack that you're used to. Firstly, there's no fallback for individual characters that may not be supported by your font. Secondly, you can't refer to generic font-families (like sans-serif, system-ui, etc). For those features, you're stuck with the classic font stack. But you can happily use both features, encapsulating all your named fonts in the #font-face rule, and adding the generic font as your last-resort fallback in the font-family declaration:
p {
font-family: bodytext, sans-serif;
}
CSS Variables is one solution to stay DRY
:root {
--MainFont: "Gotham", "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
--HeavyFont: "Gotham Black", "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
}
body {
font-family: $MainFont;
}
h1 {
font-family: $HeavyFont;
}

font-face for local fonts

I have a framework of html/css pages using a font-face declaration with a otf. Now I want to switch it to a common font like Verdana. How can I assign Verdana to the font-face declaration avoiding a numerous replacement of font-family declaration? In other words: How can I use the font name declarated by font-face as a font variable?
#font-face {
font-family: 'bauer-bodoni.otf';
src: url(../fonts);
Should be something like this (which is not working in this form):
#font-face {
font-family: 'Verdana', 'Arial', sans-serif;
Thank you very much in advance.
-R.
EDIT/ANSWER: I've found out on my own. The trick is not to list the fonts the way you do in a normal font declaration, separated by comma, but with a "local()" for each:
#font-face {
font-family: 'myOwnFontSet';
src: local('Verdana');
src: local('Arial');
src: local(sans-serif);
Your update is still wrong. You need the following:
#font-face {
font-family: 'myOwnFontSet';
src: local('Verdana'), local('Arial'), local(sans-serif); }

Custom CSS font won't work

For some reason the font I'm trying to add won't add itself to my website. I'd rather not do this with an image, so is it possible the font is broken? Would it be possible to fix it with just the otf or ttf?
My code (in case I'm missing something):
#font-face {
font-family: urbanJungle;
src: url('UrbanJungleDEMO.ttf');
}
h1 {
font-family: urbanJungle;
font-size: 100px;
color: #34495e;
}
Additional details: This is in the latest Chrome, other custom fonts work.
In the network console the font is red and it says cancelled.
Live URL: http://codestack.co.uk/website/
The font was from Dafont, no extra processing applied by myself, it's in the same directory as the index page. All the relevant CSS is included.
You should use Font Squirrel font-face generator for this: http://www.fontsquirrel.com/tools/webfont-generator
Different browsers need different font formats, you only provided one. The generator will convert your font to all the formats needed and give you a CSS file too, with no hassles.
You are using only TrueType font, IE support only *.eot fonts. And you are missing a lot informations. It is always better to use font stack instead of using single font, if first font went missing css use immediate next font on the list (called font-stack).
Here is an interesting article about #font-face by Paul Irish : Bulletproof #font-face Syntax
#font-face{
font-family:MyFont;
src:url(../font/MyFont.eot);
src:local('?'),
url(../font/MyFont.woff) format("woff"),
url(../font/MyFont.otf) format("opentype"),
url(../font/MyFont.ttf) format("Truetype"),
url(../font/MyFont.svg#myfont) format("svg");
font-weight: normal;
font-size:normal;
}
body{
font-family: "MyFont", Verdana, sans-serif; /* Font stack */
}

font-family of a font, what is it?

I'm trying to find the CSS font-family name of the Helvetica Neue LT Std 97 Black Condensed font located here: http://fontscore.com/fonts/Helvetica-Neue-LT-Std-97-Black-Condensed_22554.html
I'm using #font-face to load the font onto the webpage, if not already on the users computer, however, I would like to be able to load the font, if found on the users computer first. I've tried several methods here, but none of these work:
font-family: HelveticaNeueLT Std Blk Cn
font-family: HelveticaNeueLT Std ExtBlk Cn
What is the name of this font that I should use when calling font-family? I probably have to put 97 somewhere in there also, but nothing works, have tried a ton of different ways to call it.
Here's what I'm using in the CSS:
#font-face {
font-family: hnc_font;
src: local('HelveticaNeueLT Std Blk Cn'), local('HelveticaNeueLTStd-BlkCn'), url('../fonts/hnc.otf') format('opentype'), url('../fonts/hnc.woff') format('woff'), url('../fonts/hnc.ttf') format('truetype'), url('../fonts/hnc.svg') format('svg');
font-weight: normal;
}
#font-face{
font-family: MyFont_IE;
src: url('../fonts/swiss_bc.eot');
}
.big_text
{
padding-top: .4em;
font-size: 5.5em;
font-family: HelveticaNeueLT Std Blk Cn, HelveticaNeueLTStd-BlkCn, hnc_font, MyFont_IE;
}
First of all try all your combinations by wrapping the name of the font in " quotes.
For example font-family: "Helvetica Neue LT Std 97 Black Condensed"
Then, the name that you call is defined by your #font-face declaration before the font-family. You can call it what ever you want.
For example if you first declare it like this
#font-face{
font-family: "MY_FONT_NAME";
src: url('PATH_TO/FONT_FILENAME.ttf');
}
you can later refer to it like this
font-family: "MY_FONT_NAME";
Edit based on the added CSS
try this
.big_text
{
padding-top: .4em;
font-size: 5.5em;
font-family: "HelveticaNeueLT Std Blk Cn", "HelveticaNeueLTStd-BlkCn", "hnc_font", "MyFont_IE";
}
Cheat with some javascript: http://jsfiddle.net/ucd2L/
var x = $("#test").css('font-family');
$("#test").append(x);
HTML
<p id="test">Text</p>
When using #font-face, there are two separate questions about font names.
The name you use in font-family declaration inside an #font-face rule is up to you, as long as it is a syntactically valid font name and you use that very same name in font-family declarations in normal CSS rules. So it can be font-family: foobar.
The name you use in the local(...) constructor when setting src in an #font-family rule must match the name under which the font is known in the system. It should be name you would use to get a local font in a font-family rule without any #font-face, and in principle it should be the PostScript name or the full name of the font, though browsers may accept other names too; see
css - machine specific font-family.

Resources