CSS(Cascading Style Sheets)是一种用于描述HTML或XML(包括SVG、MathML等各种XML方言)文档样式的样式表语言。在CSS中,可以通过font-family
属性来设置字体。
font-family
属性允许你指定一系列的字体,浏览器会按照顺序查找并使用第一个可用的字体。font-size
)、粗细(font-weight
)、样式(font-style
,如斜体)等。@font-face
规则引入的字体文件,可以是TTF、OTF、WOFF等格式。@font-face
规则在CSS文件中声明自定义字体。例如:@font-face {
font-family: 'MyCustomFont';
src: url('path/to/font.woff2') format('woff2'),
url('path/to/font.woff') format('woff');
}
然后在需要使用该字体的地方设置font-family
属性:
body {
font-family: 'MyCustomFont', sans-serif;
}
以下是一个简单的示例,展示如何使用CSS设置字体:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Fonts Example</title>
<style>
body {
font-family: 'Arial', sans-serif;
font-size: 16px;
font-weight: normal;
font-style: normal;
}
.custom-font {
font-family: 'MyCustomFont', sans-serif;
}
</style>
<style>
@font-face {
font-family: 'MyCustomFont';
src: url('path/to/font.woff2') format('woff2'),
url('path/to/font.woff') format('woff');
}
</style>
</head>
<body>
<p>This is a paragraph with default font settings.</p>
<p class="custom-font">This paragraph uses a custom font.</p>
</body>
</html>
注意:在实际应用中,请确保字体文件的路径正确,并且已经上传到服务器上。