Overriding #font-face src URL? - css

We are using FontAwesome with Bootstrap. However, when we try to use FA with our custom minifier, it attempts to load the fonts from a relative path, which returns a 404, due to the way the minified URL structure is setup.
So we figured the best way to fix this was to add an additional CSS file to our minify list that would override the #font-face src URLs that FontAwesome's font uses. We basically just copied the #font-face definition from FontAwesome, and specified absolute URL locations.
However, now what happens is our correct URLs load the fonts AND the originally specified URLs from the FontAwesome CSS are attempted (resulting in the same 404 errors as before).
Are we doing something wrong, or is there really no way to override the #font-face src URLs so that 'upstream' definitions are totally ignored?

Simple override the font-family of the base CSS class:
.fa {
font-family: 'FontAwesome2' !important;
}
Then, paste/include and edit the font definition:
#font-face {
font-family: 'FontAwesome2';
src: url('//host.domain/yourpath/fontawesome-webfont.eot?v=3.1.0');
...
font-style: normal;
}

UPDATE: The "solution" below did not actually work... we did in fact have a typo, but in subsequent testing, this was still not the root cause, and we are still facing the issue.
The solution is to be VERY CAREFUL when overriding the #font-face, making sure to provide all of the same formats used in the original #font-face. Otherwise it appears the browser sees it as a different definition, and will try to download files referenced in both, rather than overriding it.
So here is the definition in FontAwesome's CSS, which is referenced first.
#font-face {
font-family: 'FontAwesome';
src: url('../font/fontawesome-webfont.eot?v=3.1.0');
src: url('../font/fontawesome-webfont.eot?#iefix&v=3.1.0') format('embedded-opentype'),
url('../font/fontawesome-webfont.woff?v=3.1.0') format('woff'),
url('../font/fontawesome-webfont.ttf?v=3.1.0') format('truetype'),
url('../font/fontawesome-webfont.svg#fontawesomeregular?v=3.1.0') format('svg');
font-weight: normal;
font-style: normal;
}
And when we tried to override, we accidentally dropped the "format('svg')" definition:
#font-face {
font-family: 'FontAwesome';
src: url('//ourdomain.com/includes/font-awesome-3.1.x/font/fontawesome-webfont.eot?v=3.0.1');
src: url('//ourdomain.com/includes/font-awesome-3.1.x/font/fontawesome-webfont.eot?#iefix&v=3.0.1') format('embedded-opentype'),
url('//ourdomain.com/includes/font-awesome-3.1.x/font/fontawesome-webfont.woff?v=3.0.1') format('woff'),
url('//ourdomain.com/includes/font-awesome-3.1.x/font/fontawesome-webfont.ttf?v=3.0.1') format('truetype');
font-weight: normal;
font-style: normal;
}
Once we added the format('truetype') definition, we no longer experienced the additional hits that had resulted in 404s.

If you are using SCSS you likely have a main scss file in your src folder which contains an #import "font-awesome" that imports font-awesome from node_modules/font-awesome/scss/font-awesome.scss.
You can duplicate this font-awesome.scss file into your src folder and customise it. Then you can also duplicate font-awesome/scss/path.scss into your src folder too and customise that.
in main.scss (or whatever your initial scss file is called)
#import "path/custom-fontawesome";
in custom-fontawesome.scss
#import "~font-awesome/scss/variables";
#import "~font-awesome/scss/mixins";
#import "custom-fontawesome-path";
#import "~font-awesome/scss/core";
#import "~font-awesome/scss/larger";
#import "~font-awesome/scss/fixed-width";
#import "~font-awesome/scss/list";
#import "~font-awesome/scss/bordered-pulled";
#import "~font-awesome/scss/animated";
#import "~font-awesome/scss/rotated-flipped";
#import "~font-awesome/scss/stacked";
#import "~font-awesome/scss/icons";
#import "~font-awesome/scss/screen-reader";
in custom-fontawesome-path.scss
$fa-font-path: "../fonts-path";
#font-face {
font-family: 'FontAwesome';
src: url('#{$fa-font-path}/fontawesome-webfont.woff2?v=#{$fa-version}') format('woff2');
font-weight: normal;
font-style: normal;
}
You can customise the path to the font files by setting the $fa-font-path: "../fonts-path";

Related

Preloading fonts in CSS File

i have a font-awesome CSS-Fille which i load in the footer of my website. In the CSS File the fonts are loaded kike this:
font-display:swap;src:url(../webfonts/fa-brands-400.eot);src:url(../webfonts/fa-brands-400.eot?#iefix)
format("embedded-opentype"),url(../webfonts/fa-brands-400.woff2) format("woff2"),url(../webfonts/fa-
brands-400.woff) format("woff"),url(../webfonts/fa-brands-400.ttf)......
not i look for an solution to preload these fonts. Is there an easy way to do that?
Not sure if thats what you mean, but you can define a font-face in a seperate css file, e.g.:
#font-face {
font-family: faBrands;
font-weight: 400;
src: url('/webfonts/fa-brands-400.eot');
}
#font-face {
font-family: faBrands;
font-weight: 400;
src: url('/webfonts/fa-brands-500.eot');
}
Assuming the folder webfonts is under public, you can then write in a css file:
font-family: faBrands;
Just make sure to import the font face css file first.

SASS - font face not working with right path

I recently started using SASS and I want to import 2 fonts, Montserrat and Open Sans. Normally in CSS, you do something along the lines of
#font-face {
font-family: 'Montserrat'
src: url('../../webfonts/Montserrat.ttf');
}
And it works just fine. If my file structure looks like the following
CSS
Base
typography.sass
Webfonts
Montserrat.ttf
But I put the following code in my SASS file.
#font-face
font-family: 'Montserrat'
src: url("../../webfonts/Montserrat.ttf")
#font-face
font-family: 'Open Sans'
src: url("../../webfonts/OpenSans.ttf")
src: url("../../webfonts/OpenSans.ttf?iefix") format("embedded-opentype")
But the font's don't load. I tried similar question on this topic but none of them were succesfull for me. What could this problem be?
If your folder name is Webfonts and you reference it with url("../../webfonts/..., then you have your answer there (lowercase vs. uppercase w/W).
Also, your code example results in a double src attribute. I don't know if it is related to the problem.
#font-face
font-family: 'Open Sans'
src: url("../../webfonts/OpenSans.ttf")
src: url("../../webfonts/OpenSans.ttf?iefix") format("embedded-opentype")
The code above compiles to this, where the src overwrites itself for each time:
#font-face {
font-family: "Open Sans";
src: url("../../webfonts/OpenSans.ttf");
src: url("../../webfonts/OpenSans.ttf?iefix") format("embedded-opentype");
}
The src attribute should only be set once, but with multiple values instead. I think this will work in Sass:
#font-face
font-family: 'Open Sans'
src: url("../../webfonts/OpenSans.ttf"), url("../../webfonts/OpenSans.ttf?iefix") format("embedded-opentype")

Ionic 3, need help importing custom font (brand new project)

I'm trying to import a custom icon font called "Picons Thin" into my Ionic project. I put the font formats titled piconsthin-webfont.ttf, piconsthin-webfont.woff, and piconsthin-webfont.woff2 into the www/assets/fonts folder, and created a piconsthin-webfont.scss file in the same folder which contains the following:
$piconsthin-webfont-font-path: $font-path !default;
#font-face {
font-family: "piconsthin";
src: local("piconsthin"),
url('#{$piconsthin-webfont-font-path}/piconsthin-webfont.woff2') format('woff2'),
url('#{$piconsthin-webfont-font-path}/piconsthin-webfont.woff') format('woff'),
url('#{$piconsthin-webfont-font-path}/piconsthin-webfont.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
I then went to variables.scss and saw the fonts section at the bottom that was importing roboto and noto-sans, and assumed this was calling the scss files in the fonts folder. So I added my own line:
#import "piconsthin-webfont";
But that just gives an error in Ionic that says "File to import not found or unreadable: piconsthin-webfont".
I've tried just putting the picons #font-face css into one of the other font scss files that I knew could be found, and I've tried putting the #font-face css into the main src/app/app.scss file, but nothing I do makes the font show up on the front end... what am I doing wrong?? What step am I missing??
Put your fonts in the source folder (src/assets/fonts/CHESSMASTER8000_BOLD.ttf) then connect it in the variables.scss file like this
#font-face {
font-family: "Chessmaster-8000";
font-style: bold;
src: url('../assets/fonts/CHESSMASTER8000_BOLD.woff');
}

Custom fonts not showing

I have added new fonts to my app/assets/fonts/NewFont folder.
I have also added font files there: 1234.eot, 1234.woff2, 1234.woff, 1234.ttf
In my fonts.scss.css I have added:
#font-face {
font-family: 'NewFont';
src: font-url('NewFont/1234.eot');
src: font-url('NewFont/1234.eot?#iefix') format('embedded-opentype'),
font-url('NewFont/1234.woff2') format('woff2'),
font-url('NewFont/1234.woff') format('woff'),
font-url('NewFont/1234.ttf') format('truetype');
}
And when I use it for my body:
html, body {
font-family: 'NewFont', sans-serif;
}
And nothing happens. My font on the web-site is still showing the same. What am I missing?
I am using Ubuntu, but this shouldn't be dependent on the system in any-case.
I have tried replacing the font-url with assets-url, but no luck.
Should the font-family match some kind of font family that is already defined in the font files?
config/initializers/assets.rb:
Rails.application.config.assets.precompile += %w(1234.eot
1234.ttf
1234.woff
1234.woff2
fonts.css.scss)
You need to link to your assets folder:
#font-face {
font-family: '1234';
src: url('assets/fonts/NewFont/1234.eot');
...
...
}
More info on asset_path here: http://guides.rubyonrails.org/asset_pipeline.html
The answer was that I forgot to require the fonts file itself to the application.css file at the very top of the file:
*= require fonts
Side note:
Make sure that the fonts are in the public folder so production web-site can pick the fonts. For that purpose, update the:
src: font-url('NewFont/1234.eot')
to
src: url('/fonts/NewFont/1234.eot')
(in case you have moved the new font to the public/fonts/ directory)

custom font not loading on heroku with rails pipeline

Following this SO post, I changed all of my #font-face definitions to look like the following:
#font-face {
font-family: 'Pe-icon-7-stroke';
src:font-url('/assets/Pe-icon-7-stroke.eot?d7yf1v');
src:font-url('/assets/Pe-icon-7-stroke.eot?#iefixd7yf1v') format('embedded-opentype'),
font-url('/assets/Pe-icon-7-stroke.woff?d7yf1v') format('woff'),
font-url('/assets/Pe-icon-7-stroke.ttf?d7yf1v') format('truetype'),
font-url('/assets/Pe-icon-7-stroke.svg?d7yf1v#Pe-icon-7-stroke') format('svg');
font-weight: normal;
font-style: normal;
}
Where all of the font files are plassed in /assets/fonts/.
I also changed the file in which the above #font-face is declared from type .css to .scss. The fonts are still loading in development, but when I added the changes to git and pushed to my production site on heroku, the fonts still are not loading.
This may not be indicative of the problem I am having, but when I view my compiled assets on the live site, I see that for the definition of the font, there is written:
font-family:'Pe-icon-7-stroke';
src:url(/assets/Pe-icon-7-stroke.eot?d7yf1v);
as opposed to font-url it uses url (which is maybe how sass is converted into css, but may also mean that the old application.css is loading?)
https://kaf.herokuapp.com/assets/application-490d8d687dc0c9b526bf1348ca9a3a6f.css
For Reference, here is my complete assets directory on Github
https://github.com/ebbnormal/kaf/tree/master/app/assets/
Check out SASS asset helpers, specifically, this part:
Returns a url reference to the asset.
asset-url("rails.png") returns url(/assets/rails.png) As a
convenience, for each of the following asset classes there are
corresponding -path and -url helpers: image, font, video, audio,
javascript, stylesheet.
image-path("rails.png") returns "/assets/rails.png"
image-url("rails.png") returns url(/assets/rails.png)
It looks like you need to remove the /assets part in your font-url.
asset-url($relative-asset-path)
Returns a url reference to the asset.
asset-url("rails.png") returns url(/assets/rails.png)
As a convenience, for each of the following asset classes there are
corresponding -path and -url helpers:
image, font, video, audio,
javascript, stylesheet.
image-path("rails.png") returns "/assets/rails.png"
image-url("rails.png") returns url(/assets/rails.png)
https://github.com/rails/sass-rails
#font-face {
font-family: 'Pe-icon-7-stroke';
src: font-url('Pe-icon-7-stroke.eot?d7yf1v');
src: font-url('Pe-icon-7-stroke.eot?#iefixd7yf1v') format('embedded-opentype'),
font-url('Pe-icon-7-stroke.woff?d7yf1v') format('woff'),
font-url('Pe-icon-7-stroke.ttf?d7yf1v') format('truetype'),
font-url('Pe-icon-7-stroke.svg?d7yf1v#Pe-icon-7-stroke') format('svg');
font-weight: normal;
font-style: normal;
}
Although you might want to consider if you really need all those fallback formats today.
https://css-tricks.com/snippets/css/using-font-face/
http://caniuse.com/#feat=woff

Resources