简体   繁体   English

CSS外部字体在Div中不起作用

[英]CSS External Font Not Functioning In Div

Recently I downloaded a font for my site and renammed it "a.ttf". 最近,我为我的网站下载了一种字体,并将其重命名为“ a.ttf”。 When I try embeding it into a CSS id for a div however, it doesn't seem functional. 但是,当我尝试将其嵌入到div的CSS ID中时,它似乎没有功能。 Does anyone know the problem here? 有人知道这里的问题吗? Thank you very much and have a great day! 非常感谢,祝您有美好的一天! I forgot to mention also my external CSS page is functioning as I have working classes in other divs. 我忘了提及我的外部CSS页面,因为我在其他div中有工作类。 I just inspected element and it now says Failed to decode downloaded font. 我刚刚检查了元素,它现在显示无法解码下载的字体。 WHAT DOES THIE MEAN. 这意味着什么。 ARGGHHH! 啊! :-) :-)

CSS Code CSS代码

@font-face 
{
font-family: a;
src:url("a.ttf");
}

#text
{
color: white;
font-family: a;
font-size: 25px;
font-weight: bold;
text-align: left;
margin-left: 15px;
margin-top: 20px;
}

HTML Div HTML Div

<div id="text">Test Div</div>

I think you try this one... 我想你试试这个...

Css 的CSS

@font-face {
  font-family: 'a';
  src: url('a.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
}
#text{
 color: white;
 font-family: a;
 font-size: 25px;
 font-weight: bold;
 text-align: left;
 margin-left: 15px;
 margin-top: 20px;
}

Hope this helps. 希望这可以帮助。

Hey I think the code you are using is wrong. 嘿,我认为您使用的代码是错误的。 If you go to FontSquirrel and download a font kit this is the recommended way to add custom fonts 如果转到FontSquirrel并下载字体工具包,则这是添加自定义字体的推荐方法

@font-face{ 
font-family: 'MyWebFont';
src: url('WebFont.eot');
src: url('WebFont.eot?#iefix') format('embedded-opentype'),
     url('WebFont.woff') format('woff'),
     url('WebFont.ttf') format('truetype'),
     url('WebFont.svg#webfont') format('svg');

} }

You need more that one extension for the font so it works in all browsers. 您需要为字体提供一个以上的扩展名,以便它在所有浏览器中均可使用。 Also where is your font located??... the path might not be the same as you css file 另外您的字体在哪里?? ...路径可能与您的css文件不同

  1. use single quote in src:url('a.ttf'); src:url('a.ttf');使用单引号src:url('a.ttf');
  2. make sure that the (a.ttf) in the same path of css file. 确保(a.ttf)位于css文件的相同路径中。

Internet Explorer has some ttf support starting with version 9, but "only working when [fonts are] set to be installable". Internet Explorer从版本9开始具有一些ttf支持,但是“仅在[字体]被设置为可安装时才起作用”。

When you are using the @font-face rule, please ensure you use the following syntax: 使用@font-face规则时,请确保使用以下语法:

@font-face {
    font-family: 'My Font';
    font-style: normal;
    font-weight: 400;
    src: url('../path/to/My-Font.ttf') format('ttf');
}

The error you seem to be getting means the file (font file) you download may have been damaged while it was getting downloaded or it may have been damaged on the server itself. 您似乎收到的错误意味着您下载的文件(字体文件)在下载时可能已损坏,或者在服务器本身上已损坏。 I recommend you download the font again from the same website OR download it again from a different site. 我建议您从同一网站再次下载该字体,或从其他网站再次下载该字体。

When you are going to use a custom font, please ensure you use the following syntax: 当您要使用自定义字体时,请确保使用以下语法:

#text {
    color: white;
    **font-family: 'My Font'**;
    font-size: 25px;
    font-weight: bold;
    text-align: left;
    margin-left: 15px;
    margin-top: 20px;
}

Of course, if you copy my code, make sure you take out the asterisk (*). 当然,如果您复制我的代码,请确保取出星号(*)。


Here is some more information about the @font-face rule, here: 这里是有关@font-face规则的更多信息:

http://www.w3schools.com/cssref/css3_pr_font-face_rule.asp http://www.w3schools.com/cssref/css3_pr_font-face_rule.asp

Here: 这里:

https://css-tricks.com/snippets/css/using-font-face/ https://css-tricks.com/snippets/css/using-font-face/

And, here: 和这里:

https://developer.mozilla.org/en/docs/Web/CSS/@font-face https://developer.mozilla.org/en/docs/Web/CSS/@font-face

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM