如何在 CSS 中添加一些非标准字体到网站?
非标准字体是指任何不在大多数浏览器 CSS 中提供的默认字体集中的字体。默认字体(包括 Arial、Times New Roman 和 Verdana)是标准字体,因为它们预装在大多数计算机和设备上。
非标准字体是没有预安装的字体,必须专门加载到网站才能使用。这些字体可以从 Google、Adobe 或 MyFonts 等网站获取。它们也可以是自定义设计或购买的。
使用非标准字体有助于为网站设计增添独特和个性化的风格。它们经常用于创建特定外观或建立品牌的视觉识别。
在 CSS 中添加一些非标准字体到网站
排版在创建网站的美感方面起着至关重要的作用。CSS 中提供的默认字体(如 Arial、Times New Roman 和 Verdana),虽然实用,但通常显得平淡无奇。
@font-face 规则允许指定字体文件和属性,从而允许将字体应用于页面上的特定元素。
语法
@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;
}
示例
<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.ac.cn/css?family=Permanent+Marker&display=swap');
示例
<!DOCTYPE html>
<html>
<title>Google fonts example</title>
<head>
<link href="https://fonts.googleapis.ac.cn/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>
结论
通过以上步骤,我们成功地将非标准字体添加到网站。请记住,为了使字体能够正确渲染,必须确保字体文件托管在服务器上并且浏览器可以访问。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP