如何在CSS中将一些非标准字体添加到网站上?

PHPz
PHPz 转载
2023-08-22 09:37:02 665浏览

如何在CSS中将一些非标准字体添加到网站上?

非标准字体是指在CSS中,不属于大多数浏览器默认可用字体集合的任何字体。默认字体包括Arial、Times New Roman和Verdana等,它们是标准字体,因为它们预装在大多数计算机和设备上。

非标准字体是指未预装的字体,必须特别加载到网站上才能使用。这些字体可以从Google、Adobe或MyFonts等网站获取。它们也可以是定制设计或购买的。

使用非标准字体有助于为网站设计增添独特和个性化的风格。它们常常被用来创造特定的外观或建立品牌的视觉形象。

在CSS中为网站添加一些非标准字体

排版在设计网站时起着至关重要的作用。在CSS中提供的默认字体,如Arial、Times New Roman和Verdana,虽然功能上可用,但常常显得平淡和普通。

The @font-face规则允许指定字体文件和属性,从而可以将字体应用于页面上的特定元素。

Syntax

使用@font-face规则的语法如下 -

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

在这个例子中,我们指定了字体族为'MyFont',这个名字将在整个CSS中用来引用这个字体。'src'属性指定了字体文件的位置,'format'属性指定了字体的文件格式。为了更好的浏览器兼容性,建议包含多种字体格式,如truetype、woff、woff2、eot等。

一旦使用@font-face规则定义字体,就可以使用'font-family'属性将其应用于页面上的特定元素。在下面的示例中,我们将自定义字体'MyFont'应用于'body'元素−

body {
   font-family: 'MyFont', Fallback, sans-serif;
}

Example

的中文翻译为:

示例

<html>
<head>
   <style>
      body {
         background-color:pink;
      }
      @font-face {
         font-family: 'MyFont';
         src: url('/css/font/SansationLight.woff')
         format('truetype');
         font-weight: normal;
         font-style: normal;
      }
      p {
         font-family: 'MyFont', Fallback, sans-serif;
      }
      .div {
         font-family: 'MyFont', Arial, sans-serif;
      }
   </Style>
</head>
<body>
   <div class="div">This is the example of font face with CSS3.</div>
   <p><b>Original Text :</b>This is the example of font face with CSS.</p>
</body>
</html> 

我们还可以使用 @import 从远程源(如Google Fonts或任何其他字体托管服务)导入字体。

@import url('https://fonts.googleapis.com/css?family=Permanent+Marker&display=swap'); 

Example

的中文翻译为:

示例

<!DOCTYPE html>
<html>
   <title>Google fonts example</title>
   <head>
      <link href="https://fonts.googleapis.com/css?family=Permanent+Marker&display=swap" rel="stylesheet"/>
      <style>
         body {
            font-family: "Permanent Marker";
            font-size: 15px;
         }
      </style>
   </head>
<body>
   <h1>Google fonts example</h1>
   <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Debitis non a quos repudiandae doloribus cumque! Ex rem rerum aut maiore. Lorem ipsum dolor sit amet consectetur, adipisicing elit. Debitis non a quos repudiandae doloribus cumque! Ex rem rerum aut maiores</p>
</body>
</html>

结论

通过上述步骤,我们已经成功地将非标准字体添加到网站中。请记住,重要的是确保字体文件托管在服务器上,并且对于浏览器来说可以访问,以便正确地呈现字体。

以上就是如何在CSS中将一些非标准字体添加到网站上?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除