Unable to load fonts using #font-face - html

I am trying to add 'MyriadPro' font using the #font-face. Here's the fiddle
HTML code:
<h1> Test Heading </h1>
CSS Code:
#font-face {
font-family: 'MyriadPro Regular';
src: url('http://consumemarketing.com/thai-works/MyriadPro-Regular.woff') format('woff'), /* FF 3.6, Chrome 5, IE9 */
url('http://consumemarketing.com/thai-works/MyriadPro-Regular.ttf') format('truetype'), /* Opera, Safari */
url('http://consumemarketing.com/thai-works/MyriadPro-Regular.svg#font') format('svg'); /* iOS */
}
h1{
font-family: 'MyriadPro Regular', sans-serif;
}
I've tried to test this locally, doesn't work, not sure what's missing.

You spelled MyriadPro as MyriaedPro
h1{
font-family: 'MyriadPro Regular', sans-serif;
}
Edited
This is the error being displayed in the console.
Font from origin 'http://consumemarketing.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
and this is the error being displayed in your fiddle.
Mixed Content: The page at 'https://jsfiddle.net/krish7878/27tp756L/' was loaded over HTTPS, but requested an insecure font 'http://consumemarketing.com/thai-works/MyriadPro-Regular.woff'. This request has been blocked; the content must be served over HTTPS.
You are trying to load the fonts locally, which is not allowed as the requested resource doesn't have a header and in your JSFiddle, it doesn't allow the font to load as it is from an insecure origin (http). So instead download the fonts locally and try using them, which works surely.

The website that you are trying to load the font(s) from is blocking the loading of the fonts onto your page as web fonts are subject to Cross-Origin Resource Sharing (CORS). CORS is basically a way for a remote host to control access to certain types of resources, so without the owner of the server that you are trying to access the fonts from modifying their .htaccess file to include the header to allow the CORS, you are unable to utilize these fonts from that server.
More info on CORS can be found here if you want to learn some more.
The simplest thing to do would be to download the fonts you have specified by placing the URL you used in the #font-face property in your browser and then putting them in the folder where your site's folder, and modifying the #font-face URL's to direct to your local folder.
For example if you were to download the fonts to your site's folder in a subfolder called /fonts, you would modify the #font-face src to the following:
#font-face {
font-family: 'MyriadPro Regular';
src: url('fonts/MyriadPro-Regular.woff') format('woff'), /* FF 3.6, Chrome 5, IE9 */
url('fonts/MyriadPro-Regular.ttf') format('truetype'), /* Opera, Safari */
url('fonts/MyriadPro-Regular.svg#font') format('svg'); /* iOS */
}
Apart from avoiding your issue, the other thing is that by storing the files locally, it greatly reduces the loading time for the user as it is not relying on the external server to provide the files.
Hope this clears everything up.

Related

#font-face not working locally but works when linked externally

So, I have a .ttf font (plus others) that I'm using for a website, with the code formatted as follows:
#font-face {
font-family: DEP;
src: local("fonts/DEP/DEP.ttf") format("truetype");
src: local("fonts/DEP/DEP.woff") format("woff");
src: local("fonts/DEP/DEP.otf") format("opentype");
src: url(https://codehs.com/uploads/98892640bda8f3fe5654e3c6d20d5c8c) format("truetype");
}
I know it's good to have a fallback link in case the local files fail to load, but I noticed when I was editing the code offline that the local files weren't working. I triple-checked the folder structure, tested the code (minus the external link) on the main site to see if it was just my html previewer, etc etc. I don't understand why it won't link properly.
My code is also on github if you want to investigate this further. Thanks!
local() accepts locally installed fonts' system identifier, not path to file, see: https://developer.mozilla.org/en-US/docs/Web/CSS/#font-face#specifying_local_font_alternatives

CSS: Why is #font-face not working on firefox?

I have font in my local space and try to change my html font with css #font-face and it worked on any browser, except firefox. It's just a simple html and css file and i don't have any server like apache.
How should i fix it?
#font-face {
font-family: 'iransans';
src: url('../fonts/IRANSansWeb.woff2') format('woff2'),
url('../fonts/IRANSansWeb.woff') format('woff'),
url('../fonts/IRANSansWeb.eot') format('embedded-opentype'),
url('../fonts/IRANSansWeb.ttf') format('truetype');
}
i got this error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///P:/workspace/karAmuzi/karamuzi%20project%201/fonts/IRANSansWeb.woff2. (Reason: CORS request not http).
It is because of some security policy for Firefox browsers. It has a simple solution of disabling that policy. Search about:config in address bar. It will show a warning about voiding the browser's warranty and making it unstable. Take that risk. Now in the page search security.fileuri.strict_origin_policy and double click on it to turn its boolean value to false. This should solve your problem.

Font not loading correctly in IE9, some times

I am having an issue where a font that I am hosting from the same server the page is being served from, is not loading in IE9. However, this is not always the case. Most of the time it loads correctly. I noticed that when it does not get displayed correctly, IE9 loads the .eot? (see css about question mark), .woff and the .ttf font files. When the font is displayed correctly, it only loads the .eot? file. Here is my CSS
#font-face
{
font-family: Rokkitt;
src: url('/Hormel/Fonts/rokkitt-bold-webfont.eot');
src: url('/Hormel/Fonts/rokkitt-bold-webfont.eot?#iefix') format('embedded-opentype'),
url('/Hormel/Fonts/rokkitt-bold-webfont.woff') format('woff'),
url('/Hormel/Fonts/rokkitt-bold-webfont.ttf') format('truetype'),
url('/Hormel/Fonts/rokkitt-bold-webfont.svg#rokkittbold') format('svg');
}
I also added the .woff to the IIS MIME type, before that I was getting an 404 error when trying to load the .woff file and it would not load the .ttf file and the font would still not display correctly.
From what I have read, IE9 is supposed to use the first .eot file, not the .eot?iefix file, is this correct?
I tried loading this font from the google font library but i was experiencing the same issues in terms of the font not always showing up correctly.
I don't know if this makes a difference, but I load the CSS on the landing page. When the user clicks on a certain element, I make an ajax call for the page to be displayed in a lightbox that appears. This is where the font is not displaying correctly. Also, when I try to inspect the content in the lightbox, the inspector says that the container, that is always on the page without any content loaded, is empty.
EDIT**
So when the fonts do not load, I get 3 CSS3117: #font-face failed cross-origin request. Resource access is restricted., one for the .eot?#iefix file, one on the .woff file and one of the .ttf file.
This is from Google and when I serve the font files from the same server as the web page.
I also load two other fonts from Google, but do not have any issues with them. Just the Rokkitt font.
I added
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
To the web.config file and added Access-Control-Allow-Origin to the response header on the server.

Include Local Files in Browser Extension?

I'm currently developing a browser extension for Chrome and Safari. To be more specific, a tooltip appears when the user hovers over certain keywords. Currently, i'm using #font-face with a link to certain fonts hosted on my website. The problem is - they take a long time to load. Is it possible to include fonts locally in the browser extensions instead of linking to them externally? Can you just package the fonts (or even images?) with the other extension files?
Yes you can, it is quite common. Just make sure you have permissions to distribute all fonts/images that you did not author. Owning a personal license doesn't count.
Every extension is saved locally, whether the code that makes it function requires internet access and server-side scripting or not. A Chrome extension is just a directory saved as a crx archive, which is literally a renamed zip file. The directory, along with the necessary Developer info and metadata, is just like that of any other site, with html, and css, javascript, fonts, images, etc. Obviously if your extension does use server-side scripts, those need to be hosted. I'm sure you know most of this if you are building extensions, but I am elaborating so beginners can follow along.
Generally speaking, fonts are tiny files (rarely over 200k unless they are grunge type, or you are embedding way more characters than necessary) and so if they are self-hosted and taking too long to load, the problem is likely with your host. If I were you, I would use Google Fonts which don't need to be self-hosted and are free.
There are several ways
to embed them, no matter which you option you choose. But here is an example of how to link to fonts stored locally within the .crx file, <style> tags go between the <head> tags:
<style>
#font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: local('Open Sans'), local('OpenSans'), url("fonts/OpenSans-Regular-webfont.woff") format("woff");
}
#font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 400;
src: local('Open Sans Italic'), local('OpenSans-Italic'), url("fonts/OpenSans-Italic-webfont.woff") format("woff");
}
</style>
Obviously, all paths are relative to the html file in your crx directory.

How to load self hosted font files if the GoogleFont service is unavailable?

I want to use a font collection from Google Fonts directory. I selected the styles and include the CSS link tag in my project's template.
Alternatively, with Google Fonts you can also download the collection, and what you get is a zip file with all the font's styles.
I can create a CSS equivalent of the one Google gives me to include in the HTML, so I want to provide the self hosted font files as fallback, if the visitor can't access the Google Font API.
How do I setup this, and preventing both the Google font file and the self hosted font file from being downloaded? If the user does has access to Google Fonts, it's browser shouldn't download the self hosted version of the font.
I would recommend just self hosting them. This is fontsprings' "bullet-proof" font-face syntax.
#font-face {
font-family: 'MyFontFamily';
src: url('myfont-webfont.eot?#iefix') format('embedded-opentype'),
url('myfont-webfont.woff') format('woff'),
url('myfont-webfont.ttf') format('truetype'),
url('myfont-webfont.svg#svgFontName') format('svg');
}
Having all four of these set will ensure that it works across browsers. Just make sure to have your font in all four types. Font Squirrel has great kits for fontface as well.
You have 3 options:
Use 2 sets of #font-face at-rules, using different font-family names (e.g. "Droid Sans" and "Droid Sans Local"), and then using a font stack like "Droid Sans", "Droid Sans Local", Helvetica, Arial, sans-serif. However, this will cause both fonts to be downloaded, increasing load time.
Use a single set of #font-face at-rules, but use 2 sets of src attributes. This too may increase loading time if the browser decides to download all the font files specified.
Do mirroring at the network layer via DNS, like most CDNs do. This requires network setup, but your CSS would be unchanged, and it's most transparent to the browser, requiring no extra downloads.
Google Web Fonts is already employing the 3rd option, so I personally wouldn't bother if you're already using a CDN-hosted source. But it may be worthwhile if you're using fonts from a less reliable source. But if you're going to go through the effort to set this up for your fonts, why not set it up for all of your static resources (images, stylesheets, JS, etc.)? The most logical course of action is to just get a free or paid CDN to host all of your static assets on.