@Font-Face 语法的防弹部分

huangapple go评论82阅读模式
英文:

Bulletproof @Font-Face Syntax

问题

我正在尝试通过FTP安装字体,但出现了错误的Bulletproof字体样式语法。请问,我做错了什么?

@font-face {
    font-family: 'Brasilero2018Free';
    src: 
        url('/themes/fonts/Brasilero2018Free.woff2') format('woff2'),
        url('/themes/fonts/Brasilero2018Free.woff') format('woff'),
        url('/themes/fonts/Brasilero2018Free.ttf') format('truetype'); 
    font-weight: 400;
    font-style: normal;
}
英文:

I'm trying to install a font via FTP and there is this error of wrong Bulletproof font-face syntax. Please, what am I doing wrong?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

@font-face {
font-family: &#39;Brasilero2018Free&#39;;
src: 
    url(&#39;/themes/fonts/Brasilero2018Free.woff2&#39;) format(&#39;woff2&#39;),
    url(&#39;/themes/fonts/Brasilero2018Free.woff&#39;) format(&#39;woff&#39;),
    url(&#39;/themes/fonts/Brasilero2018Free.ttf&#39;) format(&#39;truetype&#39;); 
    font-weight: 400;
    font-style: normal;
}

<!-- end snippet -->

答案1

得分: 1

I've tested your @font-face rule in css-live editor (extension).

There is a CORS issue (see the dev tools console):

> Access to font at
> 'https://www.flordeibez.org/wp-content/themes/fonts/Brasilero2018Free.woff2'
> from origin 'https://flordeibez.org' has been blocked by CORS policy:
> No 'Access-Control-Allow-Origin' header is present on the requested
> resource.

Your website's primary URL is https://flordeibez.org but your css references the www subdomain.

Changing your font src like solved the problem in my test.

@font-face {
font-family: 'Brasilero2018Free';
src:
url('https://flordeibez.org/wp-content/themes/fonts/Brasilero2018Free.woff2') format('woff2'),
url('https://flordeibez.org/wp-content/themes/fonts/Brasilero2018Free.woff') format('woff'),
url('https://flordeibez.org/wp-content/themes/fonts/Brasilero2018Free.ttf') format('truetype');
}

Provided your css file is in your theme folder you could use relative paths like:

@font-face {
font-family: 'Brasilero2018Free';
src:
url('../fonts/Brasilero2018Free.woff2') format('woff2'),
url('../fonts/Brasilero2018Free.woff') format('woff'),
url('../fonts/Brasilero2018Free.ttf') format('truetype');
}

If this font-family doesn't include italics or other width you should specify fonst style and weight too:

@font-face {
font-family: 'Brasilero2018Free';
src:
url('../fonts/Brasilero2018Free.woff2') format('woff2'),
url('../fonts/Brasilero2018Free.woff') format('woff'),
url('../fonts/Brasilero2018Free.ttf') format('truetype');
font-weight: 400;
font-style: normal;
}

Keep in mind, you also need to apply the font-family to an HTML element - otherwise, the browser won't load it.

E.g. apply it to the <h1> heading:

h1{
font-family: 'Brasilero2018Free';
font-weight: 400;
font-style: normal;
}

BTW. you can safely remove .eot (Internet Explorer only) and .svg fonts (not supported by Firefox or Chrome since years). Instead, add the woff2 version since it's the most compact format.

I also highly recommend removing the local() rule as it can introduce issues in some browsers blocking locally installed fonts.

Issues with elementor specificity

Elementor and other page builder plugins tend to add a lot of CSS specificity to font styles.

Workaround:
Install a plugin like Custom Fonts to upload custom fonts in your admin area/media library.

Go to your theme customizer and open the Additional CSS drawer and add a custom rule using !important to override existing styles.

@Font-Face 语法的防弹部分

h1{
font-family: 'Brasilero2018Free'!important;
font-weight: 400;
font-style: normal;
}

英文:

I've tested your @font-face rule in css-live editor (extension).

There is a CORS issue (see the dev tools console):

> Access to font at
> 'https://www.flordeibez.org/wp-content/themes/fonts/Brasilero2018Free.woff2'
> from origin 'https://flordeibez.org' has been blocked by CORS policy:
> No 'Access-Control-Allow-Origin' header is present on the requested
> resource.

Your website's primary URL is https://flordeibez.org but your css references the www subdomain.

Changing your font src like solved the problem in my test.

@font-face {
font-family: &#39;Brasilero2018Free&#39;;
src: 
    url(&#39;https://flordeibez.org/wp-content/themes/fonts/Brasilero2018Free.woff2&#39;) format(&#39;woff2&#39;),
    url(&#39;https://flordeibez.org/wp-content/themes/fonts/Brasilero2018Free.woff&#39;) format(&#39;woff&#39;),
    url(&#39;https://flordeibez.org/wp-content/themes/fonts/Brasilero2018Free.ttf&#39;) format(&#39;truetype&#39;); 
}

Provided your css file is in your theme folder you could use relative paths like:

@font-face {
font-family: &#39;Brasilero2018Free&#39;;
src: 
    url(&#39;../fonts/Brasilero2018Free.woff2&#39;) format(&#39;woff2&#39;),
    url(&#39;../fonts/Brasilero2018Free.woff&#39;) format(&#39;woff&#39;),
    url(&#39;../fonts/Brasilero2018Free.ttf&#39;) format(&#39;truetype&#39;); 
}

If this font-family doesn't include italics or other width you should specify fonst style and weight too:

 @font-face {
    font-family: &#39;Brasilero2018Free&#39;;
    src: 
        url(&#39;../fonts/Brasilero2018Free.woff2&#39;) format(&#39;woff2&#39;),
        url(&#39;../fonts/Brasilero2018Free.woff&#39;) format(&#39;woff&#39;),
        url(&#39;../fonts/Brasilero2018Free.ttf&#39;) format(&#39;truetype&#39;); 
    font-weight: 400;
    font-style: normal;
}

Keep in mind, you also need to apply the font-family to a HTML element - otherwise the browser won't load it.

E.g. apply it to the &lt;h1&gt; heading:

h1{
    font-family: &#39;Brasilero2018Free&#39;;
    font-weight: 400;
    font-style: normal;
}

BTW. you can safely remove .eot (Internet Explorer only) and .svg fonts (not supported by Firefox or Chrome since years). Instead add the woff2 version since it's the most compact format.

I also highly recommend to remove the local() rule as it can introduce issues in some browsers blocking locally installed fonts.

Issues with elementor specificity

Elementor and other page builder plugins tend to ad a lot of CSS specificity to font styles.

Workaround:
Install a plugin like Custom Fonts to upload custom fonts in your admin area/media library.

Go to your theme customizer and open the Additional CSS drawer and add a custom rule using !important to override existing styles.

@Font-Face 语法的防弹部分

h1{
 font-family: &#39;Brasilero2018Free&#39;!important;
 font-weight: 400;
 font-style: normal;
}

答案2

得分: 0

使用以下语法:

src: url('某个网址');
src: url('另一个网址');

你只是在每一行中重新定义了字体的网址。因此,浏览器将仅使用最后一个,其他的将被丢弃。正确的基本语法是:

@font-face {
font-family: 'SomeFont';
src:
local("SomeFont"),
url('//host.tld/SomeFont.woff2') format('woff2'),
url('//host.tld/SomeFont.woff') format('woff'),
url('//host.tld/SomeFont.ttf') format('truetype');
}

如果需要满足旧版浏览器的要求,可以包括额外的字体格式。

注意:感谢 @herrstrietzel,已更正错误的 `format('ttf')` 为 `format('truetype')`。
英文:

With your syntax as:

src: url(&#39;some_url&#39;);
src: url(&#39;other_url&#39;);

you just redefine the URL for the font with every line. So browser will use only the last one, others are just discarded. The correct basic syntax is:

@font-face {
  font-family: &#39;SomeFont&#39;;
  src: 
    local(&quot;SomeFont&quot;),
    url(&#39;//host.tld/SomeFont.woff2&#39;) format(&#39;woff2&#39;), 
    url(&#39;//host.tld/SomeFont.woff&#39;) format(&#39;woff&#39;), 
    url(&#39;//host.tld/SomeFont.ttf&#39;) format(&#39;truetype&#39;);
}

You can include extra font formats if you need to satisfy old browsers.

NB: changed incorrect format(&#39;ttf&#39;) to format(&#39;truetype&#39;), thanks to @herrstrietzel

huangapple
  • 本文由 发表于 2023年6月26日 23:19:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76558054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定