p {
text-indent: 2em; /* 首行縮進 2 個字符寬度 */
}
font-size 計算值2em 精確對應.article-content {
font-size: 16px;
line-height: 1.8;
text-indent: 2em; /* 32px 縮進 */
margin-block: 1.5em; /* 段落間距 1.5em = 24px */
}
/* 響應式字號下自動縮放 */
@media (max-width: 768px) {
.article-content { font-size: 15px; } /* 縮進自動變為 30px */
}
| 錯誤寫法 | 問題 | 正確寫法 |
|---|---|---|
text-indent: 24px | 固定像素,字號變化時縮進不變,破壞視覺比例 | text-indent: 2em |
text-indent: 2rem | 相對根元素字號,非當前段落字號 | text-indent: 2em |
| 用空格/ 強行縮進 | 語義錯誤、無法響應、屏幕閱讀器讀出空格 | text-indent: 2em |
p { text-indent: 2em; } 作用於 <div> | 只對塊級容器首行生效,內部<p>不生效 | 直接作用於 p、li、dd 等塊級文本容器 |
.article-content p:first-child { text-indent: 0; }
/* 或 */
.article-content p + p { text-indent: 2em; } /* 相鄰兄弟選擇器:從第二段開始縮進 */
.article-content h2 + p,
.article-content h3 + p,
.article-content h4 + p { text-indent: 0; }
.article-content li,
.article-content blockquote,
.article-content pre,
.article-content table { text-indent: 0; }
.vertical-text {
writing-mode: vertical-rl; /* 垂直排版:右到左,上到下 */
text-indent: 2em; /* 在垂直方向上產生「首行縮進」效果 */
}
.drop-cap::first-letter {
float: left;
font-size: 3.5em;
line-height: 1;
margin: 0.05em 0.1em 0 0;
font-weight: 700;
color: var(--color-primary);
}
.drop-cap { text-indent: 0; } /* 首字下沉段落不再需要縮進 */
--text-indent: 2em,統一管理,主題切換時同步調整p, li, dd, blockquote { text-indent: 0; } /* 重置默認 */
.article p { text-indent: var(--text-indent); } /* 文章區域啟用 */
<p>標籤,而非<br>換行,才能正確應用text-indenttext-indent 不影響屏幕閱讀器朗讀,優於空格方案總結:text-indent: 2em 是中文段落首行縮進的標準、語義化、響應式、無障礙的最佳方案。拒絕空格硬編碼,擁抱相對單位與現代排版規範。