2015-10-06 66 views
-2

不工作我讀了很多帖子,讓我的字體面的代碼相同,在其他的答案,它仍然沒有工作...字體面都

@font-face { 
    font-family: 'KlavikaLT'; 
    src: url('Klavika-Light.otf') format('opentype'); 
} 

@font-face { 
    font-family: 'KlavikaLTit'; 
    src: url('Klavika-LightItalic.otf') format('opentype'); 
} 

@font-face { 
    font-family: 'KlavikaBD'; 
    src: url('Klavika-Bold.otf') format('opentype'); 
} 

@font-face { 
    font-family: 'KlavikaBDit'; 
    src: url('Klavika-BoldItalic.otf') format('opentype'); 
} 

@font-face { 
    font-family: 'KlavikaRG'; 
    src: url('Klavika-Regular.otf') format('opentype'); 
} 

.footerleft 
{ 
    font-family: KlavikaLT; 
} 

.footerright 
{ 
    font-family: KlavikaBDit; 
} 

這是我的示例代碼我使用。字體與樣式表文件位於同一文件夾中。我在哪裏犯了一個錯誤?

編輯1 ,因爲它的工作在我的本地這很有趣,和所有的路徑是正確的。沒有在我的服務器上工作,但我做了與我的其他網站上的代碼相同,它在那裏工作...

+2

檢查瀏覽器開發工具中的網絡選項卡,查看加載文件時是否有錯誤。 – CodingWithSpike

+0

嘗試添加ttf,svg,eot,woff和woff2文件。不要只保留一種類型。你的布勞威爾不支持它,你有問題。 –

+2

有加載字體的網絡標籤錯誤(404),但我檢查了路徑,它是正確的...不知道爲什麼它不工作... – Ashiv3r

回答

0

只需添加字體的更多的擴展,像.TTF和.woff。現在它工作得很好,但不知道爲什麼.otf根本不起作用。乾杯!

@font-face 
{ 
    font-family: KlavikaLT; 
    src: url('fonts/KlavikaLT.otf') format('opentype'), 
     url('fonts/KlavikaLT.ttf') format('truetype'), 
     url('fonts/KlavikaLT.woff') format('woff'); 
} 
1

'KlavikaLT'不是像Courier這樣的標準字體。除非您告訴它,否則您的瀏覽器不知道該字體是什麼。

你可以告訴你的字體瀏覽器在幾個方面:

後進口在你的CSS的頂部:

@import url(https://fonts.googleapis.com/css?family=Open+Sans); 

導入與HTML,你的CSS上面:

<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'> 

或者使用JavaScript:

<script type="text/javascript"> 
    WebFontConfig = { 
    google: { families: [ 'Open+Sans::latin' ] } 
    }; 
    (function() { 
    var wf = document.createElement('script'); 
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') + 
     '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; 
    wf.type = 'text/javascript'; 
    wf.async = 'true'; 
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(wf, s); 
    })(); </script> 

Google Fonts複製的示例。


如果您使用託管自己的服務器上的自定義字體,你可以使用上面的語法,而是換成你自己的網址。每個人應該包含@font-face,它看起來像這樣:

@font-face { 
    font-family: 'Open Sans'; 
    font-style: normal; 
    font-weight: 400; 
    src: local('Open Sans'), local('OpenSans'), url(https://fonts.gstatic.com/s/opensans/v13/cJZKeOuBrn4kERxqtaUH3ZBw1xU1rKptJj_0jans920.woff2) format('woff2'); 
} 
+0

如果字體是自定義字體(非谷歌),那麼你的答案不適用。 – Ciprian

+0

當然可以,只需要編寫自己的導入文件即可。我已將這部分包含在我的答案中。 –

+0

您只添加了一種格式 - 「woff」 - 但OP可能使用較舊的瀏覽器,並且他/她需要指定所有兼容格式。看到我的答案。 – Ciprian

2

使用此語法:

<style> 
    @font-face { 
     font-family: 'KlavikaLT'; 
     src: url('KlavikaLT.eot'); 
     src: url('KlavikaLT.eot?#iefix') format('embedded-opentype'), 
      url('KlavikaLT.woff') format('woff'), 
      url('KlavikaLT.ttf') format('truetype'), 
      url('KlavikaLT.svg#KlavikaLT') format('svg'); 
     font-weight: normal; 
     font-style: normal; 
    } 
    body { font-family: "KlavikaLT", serif; } 
    </style> 

確保文件的路徑是正確的。

這是@font-face規格:

@font-face { 
    [font-family: <family-name>;]? 
    [src: [ <uri> [format(<string>#)]? | <font-face-name> ]#;]? 
    [unicode-range: <urange>#;]? 
    [font-variant: <font-variant>;]? 
    [font-feature-settings: normal|<feature-tag-value>#;]? 
    [font-stretch: <font-stretch>;]? 
    [font-weight: <weight>]; 
    [font-style: <style>]; 
} 
+0

你真的應該在'src'的開頭加入'local('KlavikaLT')',這樣如果樣式表在已經存在字體的頁面上使用,那麼你不會再次加載文件。 –

+0

你有'local()'聲明的任何引用鏈接嗎?我從來沒有聽說過它,我可能從中受益。 – Ciprian

+1

它只是在新下載之前檢查本地系統的字體。我找不到任何實際的文檔,但這裏有一些可能有用的鏈接:http://readableweb.com/mo-bulletproofer-font-face-css-syntax/,http://stackoverflow.com/questions/3698319/css-font-face-what-does-src-local-mean –