Can I group font's in my CSS file like this:
#font-group {
font-family: custom-group;
fonts: custom_font1, custom_font2, fallback1, fallback2;
}
At the moment, I've copied font-family: custom_font1, custom_font2, fal... onto many element's which is a pain to maintain and also bugs me how bad practice it is.
No, I don't think you can. However, with SCSS, you can use #extend feature to do this, which will only output a list of selectors that use this font.
%fonts {
font-family: helvetica, arial, sans-serif;
}
.abc {
#extend %fonts;
}
.xyz {
#extend %fonts;
}
Check out this example:
http://codepen.io/anon/pen/Zpoxap
The output of the CSS is:
.abc, .xyz {
font-family: helvetica, arial, sans-serif;
}
The %fonts is what's called a silent class or placeholder. It is only output if it is used. Placeholder's get a lot of criticism, because if you nest selectors, it can get very bloated in your output. I feel this example is a good use-case.
With regard to #font-face, you only need give it a name, like "GT-Pressura" and then it will become available throughout your CSS under that name.
#font-face{
font-family: "GT-Pressura";
font-style: normal;
font-weight: normal;
src:
url("../fonts/GT-Pressura-Regular.eot?#iefix") format("embedded-opentype"),
url("../fonts/GT-Pressura-Regular.woff") format("woff"),
url("../fonts/GT-Pressura-Regular.ttf") format("truetype"),
url("../fonts/GT-Pressura-Regular.svg#GT-Pressura") format("svg")
}
Then you can simply do what I have shown above in SCSS:
%fonts {
font-family: "GT-Pressura", helvetica, arial, sans-serif;
}
.abc {
#extend %fonts;
}
If you are not using SCSS, I recommend it because it's easy to port an existing CSS codebase to use it, as any valid CSS file is also valid SCSS. You could run everything through SCSS and only patch in the bits you need.
Extra special bonus addition:
If you do end up using SCSS, I very much recommend a SASS mixin library like Bourbon which makes writing #font-face rules that little bit nicer.
For example, you can simply do:
#include font-face("GT-Pressura", "../fonts/GT-Pressura-Regular", $file-formats: eot woff ttf svg);
Which is exactly what I use to output the above.
Related
This is my first time using font-face, and it's really hard for me to actually render the exact font, especially when it comes to *.ttc files.
Here is what I've done so far:
#font-face {
font-family: 'FontName';
src: url('../fonts/font.ttc') format('truetype');
font-weight: normal;
}
.header {
font-family: 'FontName;
}
When I check the network tab in Chrome's inpector, I can see that the font is loaded successfully, but the result text still uses another font.
What did I do wrong? Please help me to fix this problem. Thanks a lot in advance.
Update
One more thing that I figured out. When I style inline, the font is rendered correctly.
<p style="font-family:'FontName'">test 2</p>
Is there any delay in loading or something like that?
You can't use font collections for CSS #font-face declarations as the purpose of this syntax is to, unambiguously, specify which single font resource must be used by the browser when you specify some specific combination of font-{family, weight, style, etc} in your actual page CSS.
Specifying a font collection makes this impossible: there is no syntax to specify which font inside that collection you would need, so ttc are not supported by design. Extract the individual font assets you need (if legally allowed!) and then be explicit about which single font you need for which single #font-face declaration.
And remember that this is possible:
#font-face {
font-family: myfont;
font-weight: normal;
src: url('that-font-I-like-Regular.woff') format('WOFF');
}
#font-face {
font-family: myfont;
font-weight: bold;
src: url('that-font-I-like-Regular.woff') format('WOFF');
}
...
:root {
font-family: myfont;
}
h1 {
font-weight: normal; /* will use that-font-I-like-Regular.woff */
...
}
p {
font-weight: bold; /* will _also_ use that-font-I-like-Regular.woff */
...
}
I am using this css script :
#top_menu li a {
display:block;
margin-top:2px;
font-family: 'Federant', cursive;
font-size:16px;
color:#8f7a60;
padding:21px 30px;
border-right:1px solid #1e1a18;
border-left:1px solid #302a26;
}
for a text on my website but it doesn't not make it federant family. How to include it ?
Without any details on the context of the provided CSS, we can only guess the possible issues.
The most likely is that the Federant font is not a standard web font. If it is not installed on the visitor's system, the page cannot use it.
You can provide fallback standard fonts (you should in fact).
You can also, if the licence of the font allows it, embed it in your stylesheet. So the browser will load the font and apply it to the links.
Here's an example of code to embed a font :
#font-face {
font-family: 'Federant';
src: url('fonts/Federant.eot'),
url('fonts/Federant.ttf') format('truetype'),
url('fonts/Federant.svg') format('svg');
font-weight: normal;
font-style: normal;
}
It sounds like the CSS does not know what that font is, so it must be included.
See, there are webfonts which are built-in fonts in the browser, such has sans-serif, arial, serif, etc. If it is not a 'webfont' then it must be manually included.
To do so, you will need the font files, and then you can include them in your CSS like this:
#font-face Federant {
font-family: 'Federant';
src: url(../fonts/Federant.woff);
}
#top_menu li a {
font-family: Federant;
}
I am fairly new to SASS, I have used LESS for awhile but mainly just for variables and avoiding vendor prefixes. I usually define web fonts in CSS like this. Is it the same for SASS and LESS? It does not seem to work for me. In this example for testing I am setting all elements to the font Interstate.
/* Interstate font */
#font-face {
font-family: Interstate;
src: url(fonts/Interstate-Regular.otf);
}
* {
font-family: 'Interstate' !important;
}
You missed single quotes for name and uri
/* Interstate font */
#font-face {
font-family: 'Interstate';
src: url('fonts/Interstate-Regular.otf') format('opentype');
font-weight: normal;
font-style: normal;
}
I have a font namely SourceSansPro, and I include it in my css as follows:
#font-face {
font-family: "SourceSansPro";
src: url("../font/SourceSansPro-Bold.otf");
font-weight: bold;
font-style: normal;
}
#font-face {
font-family: "SourceSansPro";
src: url("../font/SourceSansPro-Regular.otf");
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: "SourceSansPro";
src: url("../font/SourceSansPro-Light.otf.otf");
font-weight: lighter;
font-style: normal;
}
It's rather redundant. Isn't there a neater way to do this?
Unfortunately the #font-face syntax is not very flexible. You're at the mercy of the browser developers in this case. You can, however, segment your fonts into a fonts.css file and just do an import:
#import url('css/fonts.css');
Another possible solution would be to add the font via Google's Font API. That way, you don't have to worry about the CSS in the first place. You just add
#import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro);
to your stylesheet. Or you can add
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'>
to the <head> of your document.
It is not essentially redundant, since you are using three typefaces and they need to be declared separately. You can, however, omit the declarations font-style: normal and font-weight: normal, since they correspond to defaults.
On the other hand, the code works only on browsers that support OTF as the format of downloadable fonts. Use e.g. http://www.fontsquirrel.com/tools/webfont-generator to generate other formats and code for taking them into use.
The font-weight: lighter probably works in most situations, but it is illogical (using relative keyword when you should specify the actual weight) and should be replaced by font-weight: 200, which corresponds to the actual weight of the typeface
Well, you're obviously going to need to change the font-family name for each one or I would think that having them all the same would make them clash. At least I would think that. I have never had it happen to me, but if it's not working, then do that. But if not, ignore this.
As for the #font-face simplicity, the only real specifications you need for the #font-face is the font-family and the src. That calls the font style, obviously. So any other web styling you can leave to either the html style or css.
#font-face {
font-family: "SourceSansPro";
src: url("../font/SourceSansPro-Light.otf.otf");
}
You can then style your font in either a span or css class.
<span style="font-family: SourceSansPro; font-weight: bold; font-style: italic;">Styled Font</span>
<div style="font-family: SourceSansPro; font-weight: bold; font-style: italic;">Styled Font</div>
<div class="myspecificstyle">Styled Font</div>
If you have a lot of fonts, I would just put them all in one css file and then link it to the page you're using them on.
<link rel="stylesheet" type="text/css" href="myfonts.css">
I am using a Grails rendering plugin to generate PDF.
I want to include CSS with my PDF to render it nicely. I found an example on the rendering plugin website
I put the CSS below in print media so that it works for PDF, but this does not work for me. I also do not know how to change the font from Arial to another font. Can someone explain this to me?
The plugin uses Arial font with this CSS:
#font-face {
src: url(path/to/arial.ttf);
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: cp1250;
}
body {
font-family: "Arial Unicode MS", Arial, sans-serif;
}
You need arialuni.ttf not just arial.ttf download it here: http://www.findthatfonts.com/search-2683324-hTTF/fonts-download-search-engine-ARIALUNI.ttf.htm
Then
You have to give your #font-face a font-family name like this:
#font-face {
font-family: "Font-Name";
src: url(path/to/font.ttf); // you have to add your font.ttf file to the server in a folder like assets/css/fonts or something.
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: cp1250;
}
body {
font-family: "Font-Name", Arial, sans-serif; // you called your font Font-Name in the #font-face so now you can use it as Font-Name everywhere else in you css.
}
Otherwise your arialuni.ttf has no name so you can't use it in other divs in your css.
The next is working for me good:
#font-face {
font-family: 'Calibri';
src: url(${grailsApplication.config.grails.serverURL}/fonts/calibri.ttf);
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
The -fs-pdf-font-encoding should be set in Identity-H.
And after it you can add something like
body {
font-family: 'Calibri';
}
It doesn't really matter what name you give the font, as long as you choose a name for the src you provide between te #font-face brackets, like font-family: "SomeName";.
Now, you can use the font everywhere using font-family: "SomeName".