How to include a font .ttf using CSS? - html

I want to include a global font for my page. I downloaded a .ttf file, and included it in my CSS, but my font won't change.
Here's my code:
#font-face {
font-family: 'oswald';
src: url('/font/oswald.regular.ttf');
}
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
margin:0;
padding:0;
border:0;
font-size:100%;
font:inherit;
font-family: oswald;
vertical-align:baseline
}
Where did I go wrong?

Only providing .ttf file for webfont won't be good enough for cross-browser support. The best possible combination at present is using the combination as :
#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 */
}
This code assumes you have .eot , .woff , .ttf and svg format for you webfont. To automate all this process , you can use : Transfonter.org.
Also , modern browsers are shifting towards .woff font , so you can probably do this too :
#font-face {
font-family: 'MyWebFont';
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 */
}
Read more here : http://css-tricks.com/snippets/css/using-font-face/
Look for browser support : Can I Use fontface

Did you try format?
#font-face {
font-family: 'The name of the Font Family Here';
src: URL('font.ttf') format('truetype');
}
Read this article: http://css-tricks.com/snippets/css/using-font-face/
Also, might depend on browser as well.

You can use font face like this:
#font-face {
font-family:"Name-Of-Font";
src: url("yourfont.ttf") format("truetype");
}

I know this is an old post but this solved my problem.
#font-face{
font-family: "Font Name";
src: url("../fonts/font-name.ttf") format("truetype");
}
notice src:url("../fonts/font-name.ttf"); we use two periods to go back to the root directory and then into the fonts folder or wherever your file is located.
hope this helps someone down the line:) happy coding

Check the console to make sure the resource is loaded. Depending on file and folder structure, your url may be wrong. Notice the difference between:
src: url('/VT323/VT323-Regular.ttf') format('truetype');
src: url('VT323/VT323-Regular.ttf') format('truetype');
src: url('../VT323/VT323-Regular.ttf') format('truetype');

Related

Adding custom font using #fontface to style.css

I have a custom font I would like to use on a webpage. Specifically GT-Walsheim-Pro-Medium-Oblique.woff from:
https://andrewsonline.co.uk/content/fonts/
The style.css I am working with is this:
https://github.com/syunghong/veil/blob/master/css/style.css
How do I incorperate this font file using #fontface into my style.css file?
I have GT-Walsheim-Pro-Medium-Oblique.woff in a folder called fonts.
here
#font-face {
font-family: 'RobotoBold'; /*this is what you put on your font family*/
src: url('../fonts/Roboto-Bold.ttf'); /*Link to the font*/
}
to use it
p {
font-family: RobotoBold;
font-size: 14;
}
If you own the rights to use the webfont, take the original ttf or otf font and render a full set here at FontSquirrel. Different browsers prefer different font types, the older browsers are pickiest.
Then add the font to the top of your stylesheet so it's loaded first or even before the stylesheet inline if you see FOUT, "Flash of Unstyled Text". More info here but basically you want the font loaded and ready before your html starts loading.
Then load your font like this:
#font-face {
font-family: 'GT Walsheim Pro Medium Oblique';
src: url('GT-Walsheim-Pro-Medium-Oblique.eot');
src: url('GT-Walsheim-Pro-Medium-Oblique.eot?#iefix&v=4.6.3') format('embedded-opentype'),
url('GT-Walsheim-Pro-Medium-Oblique.woff2') format('woff2'),
url('GT-Walsheim-Pro-Medium-Oblique.woff') format('woff'),
url('GT-Walsheim-Pro-Medium-Oblique.ttf') format('truetype'),
url('GT-Walsheim-Pro-Medium-Oblique.svg') format('svg');
font-weight: normal;
font-style: normal;
}
Font Squirrel will output the above code for you when you render the font package there.
Call the font in your css like this, I'm using your H titles as an example, make sure to always have a fallback for the font in case it errors or doesn't load for some reason:
h1, h2, h3, h4, h5, h6 {
font-family: 'GT Walsheim Pro Medium Oblique', arial, sans-serif;
}

How do I change the font-family of a <button> element?

This is my only CSS
#font-face {
font-family: 'thefont';
src: url('fonts/the-font.otf');
font-style: normal;
}
body {
font-family: 'thefont';
}
When I do a <button>Hi</button> the font ends up being -apple-system.
If I actually assign the font to button, it will make the font appear.
Does anyone know why it's not affecting the body and everything inside it?
In addition to the info below, to ensure your custom font is being taken into account for the button, you need to apply
button {
font-family : inherit;
font-size: 1em;
}
to all button elements.
You can inspect how they do it there:
http://purecss.io/buttons/
or there:
http://getbootstrap.com/css/#buttons
Also make sure that your font is exported in several different formats so that it is supported by all platforms.
You can use FontSquirrel Generator to export your font to all formats.
Your CSS should look a bit like that:
#font-face {
font-family: 'thefont';
src: url('the-font.eot'); /* IE9 Compat Modes */
src: url('the-font.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('the-font.woff2') format('woff2'), /* Super Modern Browsers */
url('the-font.woff') format('woff'), /* Pretty Modern Browsers */
url('the-font.ttf') format('truetype'), /* Safari, Android, iOS */
url('the-font.svg#svgFontName') format('svg'); /* Legacy iOS */
}

Webfont font-face does not change font of html elements, although style is updated

I installed a web font from myfonts.com on my WordPress site. I updated the CSS as follows:
#import url("../bridge/style.css");
/* LICENSE HERE */
#import url("//hello.myfonts.net/count/xxxxxxx");
#font-face {
font-family: "VeneerTwo";
src: url('http://www.example.com/wordpress/webfonts/xxxxxx_0_0.eot');
src: url('http://www.example.com/wordpress/webfonts/xxxxxx_0_0.eot?#iefix') format('embedded-opentype');
src: url('http://www.example.com/wordpress/webfonts/xxxxxx_0_0.woff2') format('woff2');
src: url('http://www.example.com/wordpress/webfonts/xxxxxx_0_0.woff') format('woff');
src: url('http://www.example.com/wordpress/webfonts/xxxxxx_0_0.ttf') format('truetype');
src: url('http://www.example.com/wordpress/webfonts/xxxxxx_0_0.svg#wf') format('svg');
}
h1, h2, h3, h4, h5 { font-family: "VeneerTwo" !important; }
All the fonts exist on the webserver.
When I check the site in Developer Tools I see that the H2 element has the style VeneerTwo:
h1, h2, h3, h4, h5 {
font-family: "VeneerTwo" !important;
}
Unfortunately, the font of the h2 element is just the default (Times New Roman if I am not mistaken).
Does anyone know why the VeneerTwo font is not displayed and how it can be fixed?
Try putting the #font-face first, before you import other media or css.
But you told that the font is displayed on the developers tool , just try to clear the cache and try again .. Like by pressing ctrl+f5 twice
I want to add the following:
Motivated by the suggestion of #arkascha to check the access logs I just checked it with an old iphone I had layin around (iPhone 4). And yes, on the iPhone 4 the font is being displayed.

What is the best way to include custom font using CSS to make it compatible with maximum browsers?

I have tried various codes to embed custom font and finally following seems to work in FF & IE8 above. But it does not support in IE7.
#font-face {
font-family: 'xyzfont';
src: url('fonts/abc-font.eot?') format('eot'),
url('fonts/abc-font.woff') format('woff'),
url('fonts/abc-font.ttf') format('truetype');
}
h1, h2, h3, div span { font-family: 'xyzfont', Georgia, Arial; }
Any suggestion to make it more compatible (such as IE7) most welcome.
Here is the css i use to embed my font in every browser, hope that helps:
#font-face {
font-family: 'xyzfont';
src: url('fonts/abc-font.eot'); /* IE9 Compat Modes */
src: url('fonts/abc-font.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('fonts/abc-font.woff') format('woff'), /* Modern Browsers */
url('fonts/abc-font.ttf') format('truetype'), /* Safari, Android, iOS */
url('fonts/abc-font.svg#svgFontName') format('svg') /* Legacy iOS */
}
Also note, that you can set your font family without quote, like this:
h1, h2, h3, div span { font-family: xyzfont, Georgia, Arial; }
You can learn about font browser support here.
fontsquirrel is probably the best place to generate every font format you need.
This article explain how to find your font ID to add after the hashtag for the SVG font.
If someone is asking why is there ?#iefix for IE6-IE8 browsers eot font, see this answer.
i use this code ;)
#font-face {
font-family: 'BBadr';
src: url('fonts/BBadr/BBadr.eot'); /* IE9 Compat Modes */
src: url('fonts/BBadr/BBadr.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url("fonts/BBadr/BBadr.otf") format("opentype"), /* Open Type Font */
url('fonts/BBadr/BBadr.woff') format('woff'),/* Pretty Modern Browsers */
url('fonts/BBadr/BBadr.woff2') format('woff2'), /* Super Modern Browsers */
url('fonts/BBadr/BBadr.ttf') format('truetype'),/* Safari, Android, iOS */
url('fonts/BBadr/BBadr.svg#BBadr') format('svg'); /* Legacy iOS */
}
you can convert Online youre font ( ttf ( with this website :
Simple Online Web Font Converter: ttf otf woff woff2 eot svg otf with css stylesheet
Convert font and use my css code ( not use the sample css of site )

font-family rule is ignored

I'm in the process of updating a site for someone, and I'm trying to get rid of a global #font-face and apply it only to specific elements.
It was defined like this:
#font-face {
font-family: "neutra";
src: url( /styles/NeutraDisp-Bold.eot ); /* IE */
src: local("Neutra Display"), url( /styles/NeutraDisp-Bold.ttf ) format("truetype"); /* non-IE */
}
#font-face {
font-family: "futura";
src: url( /styles/FuturaStd-Heavy.eot ); /* IE */
src: local("Futura Std"), url( /styles/FuturaStd-Heavy.ttf ) format("truetype"); /* non-IE */
}
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
font-family: neutra, Arial, Helvetica, Tahoma, Geneva, sans-serif;
}
I only want it on a div that has the class .header and legends (and a few other tags, eventually) so I modified the CSS to look like this instead:
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
font-family: Arial, Helvetica, Tahoma, Geneva, sans-serif;
}
#font-face {
font-family: "neutra";
src: url('../../styles/NeutraDisp-Bold.eot'); /* IE */
src: local("Neutra Display"), url('../../styles/NeutraDisp-Bold.ttf') format("truetype"); /* non-IE */
}
#font-face {
font-family: "futura";
src: url('../../styles/FuturaStd-Heavy.eot'); /* IE */
src: local("Futura Std"), url('../../styles/FuturaStd-Heavy.ttf') format("truetype"); /* non-IE */
}
legend{
font-family: neutra, Helvetica, Arial, sans-serif;
letter-spacing: .125em;
-webkit-border-radius: .5em;
-moz-border-radius: .5em;
border-radius: .5em;
}
.header{
width: 75em;
height: 12.375em;
background-color: #FFFFFF;
margin: auto;
font-family: neutra, Helvetica, Arial, Tahoma, Geneva, sans-serif;
}
However, the .header font-family is being ignored. All of the other declarations in that rule are used, and Firebug shows the font-family, which indicates to me that it's valid CSS.
In addition, the legend rule works perfectly, and shows the correct font.
Note: I moved the fonts and various other things around when I started working, but I know the new font paths are correct, because the legend rule works. I've also tried "neutra" and 'neutra'.
A pastebin of the entire CSS is here, if you think the problem is somewhere else. I've also created a jsfiddle with a fontface included to see an example of it being ignored.
Old Update
The jsfiddle is doing what it should. I have no idea what is different in my own code.
Update
I've added the offending rule. I think I'm missing something about rule weights, which would be why a lower rule still isn't overriding a higher one.
It's an issue of precedence. Check it out at w3:
http://www.w3.org/TR/CSS2/cascade.html
Your first rule which sets the default as Arial also directly applies the font-face to most html elements. This is unnecessary and causing your problem. Rather you should just set it once, on a top level element like html.
/* this single rule applies the Arial font to the whole document tree under <html> */
html { font-face: Arial, etc; }
/* this would set the font on .header, and everything inside of it */
.header { font-face: neutra, etc; }
In your case, p { font-face: Arial; } and div { font-face: Arial; } and etc beat your your singly nested .header rule. If you cut that long rule back to just a top level element, it will solve your problem.
Small example of the css cascade here, with the original long rule declaration:
<html>
<body>
My text is Arial because I exist under html and there are
no other rules modifying me.
<div class="header">
My text is neutra because I'm a direct child text node of "header"
<p>
my text is Arial because of the rule on "p", which in turn overrides
the rule on "header"
</p>
</div>
</body>
</html>
For a quick check, have you tried :
.header, .header *{
font-family: neutra, Helvetica, Arial, Tahoma, Geneva, sans-serif;
}
Since you're specifying a font-family for a lot of the tags, maybe that first set-up is too "strong".
Is there any chance you are using the html5 <header> element and defining the style for the element in your css as .header {} (a class) instead of header {}?