# CSS3的font怎么設置
在網頁設計中,字體(font)的樣式直接影響頁面的視覺體驗和可讀性。CSS3提供了豐富的字體控制屬性,本文將詳細介紹如何通過CSS3設置字體樣式。
## 一、基礎字體屬性
### 1. font-family 設置字體族
```css
p {
font-family: "Microsoft YaHei", Arial, sans-serif;
}
h1 {
font-size: 24px; /* 像素單位 */
}
h2 {
font-size: 1.5em; /* 相對單位 */
}
h3 {
font-size: 150%; /* 百分比 */
}
常用單位: - px:絕對像素 - em:相對于父元素 - rem:相對于根元素 - %:百分比 - vw/vh:視窗單位
.bold-text {
font-weight: bold; /* 粗體 */
}
.light-text {
font-weight: 300; /* 數值100-900 */
}
取值: - normal(400) - bold(700) - lighter/bolder(相對值) - 100-900(數字值)
.italic {
font-style: italic; /* 斜體 */
}
.oblique {
font-style: oblique; /* 傾斜體 */
}
.small-caps {
font-variant: small-caps;
}
p {
line-height: 1.6; /* 無單位數字表示倍數 */
}
@font-face {
font-family: 'MyCustomFont';
src: url('myfont.woff2') format('woff2'),
url('myfont.woff') format('woff');
font-weight: normal;
font-style: normal;
font-display: swap; /* 控制字體加載行為 */
}
注意事項: - 需要提供多種字體格式(woff2/woff/ttf) - 注意字體授權問題 - 使用font-display優化加載體驗
.wide-text {
font-stretch: expanded; /* 擴展 */
}
.narrow-text {
font-stretch: condensed; /* 壓縮 */
}
.heading {
font-kerning: normal; /* 啟用字距調整 */
}
/* 格式:font: style variant weight size/line-height family */
.title {
font: italic small-caps bold 24px/1.2 "Helvetica Neue", sans-serif;
}
注意: - size和family是必須的 - 省略的屬性會使用默認值 - 順序不能隨意更改
html {
font-size: 16px;
}
@media (min-width: 768px) {
html {
font-size: 18px;
}
}
h1 {
font-size: 2rem; /* 根據根元素自動調整 */
}
body {
font-family: -apple-system, BlinkMacSystemFont,
"Segoe UI", "PingFang SC", "Hiragino Sans GB",
"Microsoft YaHei", "Helvetica Neue", sans-serif;
text-rendering: optimizeLegibility; /* 優化可讀性 */
-webkit-font-smoothing: antialiased; /* Mac下抗鋸齒 */
}
字體不生效:
自定義字體加載慢:
跨平臺顯示不一致:
:root {
--main-font: "Helvetica Neue", sans-serif;
--heading-size: 2rem;
}
h1 {
font-family: var(--main-font);
font-size: var(--heading-size);
}
通過合理運用CSS3的字體屬性,開發者可以創建出既美觀又具有良好可讀性的網頁排版效果。 “`
(注:實際字數為約1100字,可根據需要擴展具體示例或添加更多細節)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。