49

I trying to use local font to apply styles in html, below is the code.Font is not getting applied for harlow class used element

<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
    font-family: myFirstFont;
    src:local("C:\Users\Website\fonts\Harlow_Solid_Italic.ttf");
}

.harlow{
    font-family: myFirstFont;
}
</style>
</head>
<body>
<div>With CSS3, websites can finally use fonts other than the pre selected "web-safe" fonts.</div>
<p><b class="harlow">Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule with the WOFF format (only support for EOT format).</p>
</body>
</html>
9
  • 1
    Try to open the developer console (F12), what does it say? Commented Jun 28, 2016 at 20:53
  • 2
    local can't be used to access files by path. That would be a security flaw. You can only give the font name. First make sure the font is installed on your computer and not just downloaded. Commented Jun 28, 2016 at 20:54
  • 2
    use url instead of local, and wrap myFirstFont in quotation. Commented Jun 28, 2016 at 20:57
  • 1
    @M.Tanzil Neither of those are necessary. Quotes are only needed when the font name contains a space. Look at some examples Commented Jun 28, 2016 at 21:02
  • 2
    Duplicate of several answered questions. This one's probably useful stackoverflow.com/questions/11812111/… Commented Jun 28, 2016 at 21:11

4 Answers 4

53

I made the following changes and I got the result

  • Quotation marks for font-family
  • Using of URL instead of local
  • Changing of "\" to "/"

Note: Use of the local css function throws an error in the developer console saying resource is not loaded. See the modified code below.

<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
    font-family: "myFirstFont";
    src: url("C:/Users/Desktop/Website/fonts/Harlow_Solid_Italic.ttf");
}

.harlow {
    font-family: "myFirstFont";
}
</style>
</head>
<body>
<div>With CSS3, websites can finally use fonts other than the pre selected "web-safe" fonts.</div>
<p><b class="harlow">Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule with the WOFF format (only support for EOT format).</p>
</body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

You should change src:url("C:/path/to/ttf"); to src:url("file:///path/to/file").
17

Use font face in all the format types according to the browser compatibility

Just add bellow code before all the styling of your css file and then you can use this font family for any selector inside within your css file.

@font-face {
    font-family: 'CustomHeading';
    src: url('./fonts/SFAtarianSystem.ttf') format('embedded-opentype'), /* Internet Explorer */
         url('./fonts/SFAtarianSystem.ttf') format('woff2'),             /* Super Modern Browsers */
         url('./fonts/SFAtarianSystem.ttf') format('woff'),              /* Pretty Modern Browsers */
         url('./fonts/SFAtarianSystem.ttf') format('truetype'),          /* Safari, Android, iOS */
         url('./fonts/SFAtarianSystem.ttf') format('svg');               /* Legacy iOS */
}

Comments

14

Use the correct path for file. your path does not work on the host. because your host has no drive 'c:/...' or anythings like this. so you can use

<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
    font-family: myFirstFont;
    src:url("/fonts/Harlow_Solid_Italic.ttf");
}

.harlow{
    font-family: myFirstFont;
}
</style>
</head>
<body>
<div>With CSS3, websites can finally use fonts other than the pre selected "web-safe" fonts.</div>
<p><b class="harlow">Note:</b> Internet Explorer 8 and earlier, do not support the @font-face rule with the WOFF format (only support for EOT format).</p>
</body>
</html>

Comments

1

You can do that with this code. I tried it on multiple sites and it worked pretty well.

@font-face {
  font-family: myFirstFont;
  src:url("D:\Files\Design\Fonts\SF-Pro-Text-Font-Family");
}

* {
  font-family: myFirstFont !important;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.