search
HomeWeb Front-endFront-end Q&AWhat CSS rules are required to define custom fonts?

Defining a custom font requires the "@font-face" rule of css. The "@font-face" rule is mainly to embed user-defined web fonts into web pages, the syntax is "@font-face{font-family: 'font name';src:url('file address');}".

What CSS rules are required to define custom fonts?

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

How to make your page support custom fonts, in one sentence, use @font-face.

@font-face is a module in CSS3. It mainly embeds self-defined Web fonts into your web pages. With @font-face## With the emergence of the #module, we are not afraid that we can only use Web-safe fonts when using fonts in Web development. Let’s take a look at how to use @font-face.

First of all, let’s take a look at the grammatical rules of

@font-face:

   @font-face {
      font-family: <YourWebFontName>;
      src: <source> [<format>][,<source> [<format>]]*;
      [font-weight: <weight>];
      [font-style: <style>];
    }

Value description

1. YourWebFontName: This value refers to your customized font name. It is best to use the default font you downloaded. It will be referenced to the font-family in your Web element. Such as "font-family:"YourWebFontName";"

2. source: This value refers to the storage path of your customized font, which can be a relative path or an absolute path;

3. Format: This value refers to the format of your customized font, which is mainly used to help the browser identify it. Its values ​​mainly include the following types: truetype, opentype, truetype-aat, embedded-opentype, avg, etc.;

4. Weight and style: You must be familiar with these two values. Weight defines whether the font is bold, and style mainly defines the font style, such as italics.

Compatible browsers

What CSS rules are required to define custom fonts?

Speaking of browser compatibility issues with @font-face, this involves a font format Problem, because different browsers have inconsistent support for font formats, it is necessary for everyone to understand what kind of fonts are supported by various versions of browsers. I briefly mentioned several formats related to fonts earlier. I will explain them separately below. Let’s talk about this problem so that everyone has an idea:

1. TrueTpe (.ttf) format:

.ttf font is the most common for Windows and Mac The font is a RAW format, so it is not optimized for the website. The browsers that support this font are [IE9, Firefox3.5, Chrome4, Safari3, Opera10, iOS Mobile Safari4.2];

2. OpenType (.otf) format:

.otf font is considered an original font format, which is built on the basis of TrueType, so it also provides more functions. Browsers that support this font include [Firefox3.5, Chrome4.0, Safari3.1, Opera10.0, iOS Mobile Safari4.2];

3. Web Open Font Format(.woff ) Format:

.woff font is the best format among Web fonts. It is an open TrueType/OpenType compressed version. It also supports the separation of metadata packages and supports the browsing of this font. The devices include [IE9, Firefox3.5, Chrome6, Safari3.6, Opera11.1];

4. Embedded Open Type (.eot) format:

. eot font is a special font for IE. This format font can be created from TrueType. The browsers that support this font are [IE4];

5. SVG (.svg) format:

.svg font is a format based on SVG font rendering. Browsers that support this font include [Chrome4, Safari3.1, Opera10.0, iOS Mobile Safari3.2].

This means that in @font-face we need at least two format fonts, .woff and .eot, and even .svg and other fonts to support more browsing versions.

In order to enable @font-face to achieve more browser support,

Paul Irish wrote a unique @font-face syntax called Bulletproof @font-face :

   @font-face {
	font-family: &#39;YourWebFontName&#39;;
	src: url(&#39;YourWebFontName.eot?&#39;) format(&#39;eot&#39;);/*IE*/
	src:url(&#39;YourWebFontName.woff&#39;) format(&#39;woff&#39;), url(&#39;YourWebFontName.ttf&#39;) format(&#39;truetype&#39;);/*non-IE*/
   }

But in order to be supported by many browsers, you can also write:

   @font-face {
	font-family: &#39;YourWebFontName&#39;;
	src: url(&#39;YourWebFontName.eot&#39;); /* IE9 Compat Modes */
	src: url(&#39;YourWebFontName.eot?#iefix&#39;) format(&#39;embedded-opentype&#39;), /* IE6-IE8 */
             url(&#39;YourWebFontName.woff&#39;) format(&#39;woff&#39;), /* Modern Browsers */
             url(&#39;YourWebFontName.ttf&#39;)  format(&#39;truetype&#39;), /* Safari, Android, iOS */
             url(&#39;YourWebFontName.svg#YourWebFontName&#39;) format(&#39;svg&#39;); /* Legacy iOS */
   }

After talking about so much empty theoretical knowledge, everyone must be a little itchy, so let’s do it first Take a look at how the blue font in the navigation section of the W3CPLUS homepage is implemented. If we have a DOM tag like this, we need to apply a custom font:

HTML Code:

   <h2 id="Neues-nbsp-Bauen-nbsp-Demo">Neues Bauen Demo</h2>

Define your own Web Font through @font-face:

  @font-face {
    font-family: &#39;NeuesBauenDemo&#39;;
    src: url(&#39;../fonts/neues_bauen_demo-webfont.eot&#39;);
    src: url(&#39;../fonts/neues_bauen_demo-webfont.eot?#iefix&#39;) format(&#39;embedded-opentype&#39;),
	url(&#39;../fonts/neues_bauen_demo-webfont.woff&#39;) format(&#39;woff&#39;),
	url(&#39;../fonts/neues_bauen_demo-webfont.ttf&#39;) format(&#39;truetype&#39;),
	url(&#39;../fonts/neues_bauen_demo-webfont.svg#NeuesBauenDemo&#39;) format(&#39;svg&#39;);
    font-weight: normal;
    font-style: normal;
  }

I use a relative path here, but of course you can also use an absolute path. At this point we need to apply the defined font to our actual page:

   h2.neuesDemo {
      font-family: &#39;NeuesBauenDemo&#39;
   }

Effect:

What CSS rules are required to define custom fonts?

(Learn Video sharing:

css video tutorial)

The above is the detailed content of What CSS rules are required to define custom fonts?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在 Google Docs 中安装自定义字体如何在 Google Docs 中安装自定义字体Apr 26, 2023 pm 01:40 PM

GoogleDocs在学校和工作环境中变得很流行,因为它提供了文字处理器所期望的所有功能。使用Google文档,您可以创建文档、简历和项目提案,还可以与世界各地的其他用户同时工作。您可能会注意到GoogleDocs不包括MicrosoftWord附带的所有功能,但它提供了自定义文档的能力。使用正确的字体可以改变文档的外观并使其具有吸引力。GoogleDocs提供了大量字体,您可以根据自己的喜好从中选择任何人。如果您希望将自定义字体添加到Google文档,请继续阅读本文。在本文中

利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft