← CSS specificity 特异性 CSS text-stroke 文字描边 →

CSS text-indent 文本缩进

原创 2025-09-07 CSS 已有人查阅

CSS text-indent 属性指南:文本缩进

什么是文本缩进?

文本缩进是网页排版中用于控制文本内容相对于其容器边界位置的重要技术。通过合理的文本缩进设计,可以显著提升网页内容的可读性和视觉美感。在 CSS 中,文本缩进主要通过 text-indent 属性实现,同时也涉及 marginpadding 等相关属性的配合使用。

text-indent 属性详解

基本语法

selector {
  text-indent: length | percentage | inherit | initial;
}

属性值说明

  • length:使用固定单位(px、pt、em、rem 等)设置缩进值

  • percentage:基于父元素宽度的百分比设置缩进

  • inherit:继承父元素的 text-indent 值

  • initial:重置为默认值(0)

  • each-line(实验性):影响所有行,包括强制换行后的首行

  • hanging(实验性):创建悬挂缩进效果

基础文本缩进示例

使用固定单位缩进

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>基础文本缩进示例 - 代码号编程</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 20px;
            background: #f8f9fa;
        }
        
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 40px;
            border-bottom: 2px solid #3498db;
            padding-bottom: 15px;
        }
        
        .article-section {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
            margin-bottom: 30px;
        }
        
        /* 像素单位缩进 */
        .indent-px {
            text-indent: 40px;
            text-align: justify;
            line-height: 1.8;
        }
        
        /* em 单位缩进 - 相对于当前字体大小 */
        .indent-em {
            text-indent: 2em;
            text-align: justify;
            line-height: 1.8;
        }
        
        /* rem 单位缩进 - 相对于根元素字体大小 */
        .indent-rem {
            text-indent: 2rem;
            text-align: justify;
            line-height: 1.8;
        }
        
        .code-example {
            background: #2c3e50;
            color: #ecf0f1;
            padding: 20px;
            border-radius: 6px;
            margin: 20px 0;
            font-family: 'Consolas', monospace;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <h1>CSS text-indent 属性示例</h1>
    
    <div class="article-section">
        <h2>像素单位缩进 (40px)</h2>
        <p class="indent-px">
            在网页排版设计中,文本缩进是提升内容可读性的重要技术。通过合理的首行缩进,可以让段落结构更加清晰,增强用户的阅读体验。CSS 的 text-indent 属性为此提供了强大的支持,开发者可以根据设计需求选择不同的单位和数值。
        </p>
        
        <div class="code-example">
            .indent-px {<br>
            &nbsp;&nbsp;text-indent: 40px;<br>
            &nbsp;&nbsp;text-align: justify;<br>
            &nbsp;&nbsp;line-height: 1.8;<br>
            }
        </div>
    </div>
    
    <div class="article-section">
        <h2>相对单位缩进 (2em)</h2>
        <p class="indent-em">
            使用相对单位(如 em、rem)进行文本缩进是现代响应式设计的推荐做法。em 单位基于当前元素的字体大小,而 rem 单位基于根元素的字体大小。这种相对单位的使用确保了在不同屏幕尺寸和字体设置下,缩进比例保持一致。
        </p>
        
        <div class="code-example">
            .indent-em {<br>
            &nbsp;&nbsp;text-indent: 2em;<br>
            &nbsp;&nbsp;text-align: justify;<br>
            &nbsp;&nbsp;line-height: 1.8;<br>
            }
        </div>
    </div>
    
    <div class="article-section">
        <h2>根相对单位缩进 (2rem)</h2>
        <p class="indent-rem">
            rem 单位提供了另一种相对缩进方式,它基于文档根元素的字体大小。这使得在整个网站中保持一致的缩进比例变得更加容易,特别是在需要响应不同设备屏幕尺寸的场景下。
        </p>
        
        <div class="code-example">
            .indent-rem {<br>
            &nbsp;&nbsp;text-indent: 2rem;<br>
            &nbsp;&nbsp;text-align: justify;<br>
            &nbsp;&nbsp;line-height: 1.8;<br>
            }
        </div>
    </div>
</body>
</html>

高级缩进技术

悬挂缩进效果

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>悬挂缩进示例 - 代码号编程</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #2c3e50;
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 20px;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }
        
        .reference-list {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
        }
        
        .reference-item {
            margin-bottom: 20px;
            padding-left: 30px;
            text-indent: -30px;
            line-height: 1.6;
        }
        
        .reference-author {
            font-weight: 600;
            color: #3498db;
        }
        
        .reference-title {
            font-style: italic;
            color: #2c3e50;
        }
        
        .reference-source {
            color: #7f8c8d;
        }
        
        .explanation-box {
            background: #e3f2fd;
            border-left: 4px solid #2196f3;
            padding: 20px;
            margin: 30px 0;
            border-radius: 6px;
        }
    </style>
</head>
<body>
    <h1>悬挂缩进技术实现</h1>
    
    <div class="reference-list">
        <h2>参考文献列表</h2>
        
        <div class="reference-item">
            <span class="reference-author">张小明</span>. 
            <span class="reference-title">现代Web开发实践指南</span>. 
            <span class="reference-source">北京:电子工业出版社,2022</span>.
        </div>
        
        <div class="reference-item">
            <span class="reference-author">王大力, 李思思</span>. 
            <span class="reference-title">CSS高级技术与响应式设计</span>. 
            <span class="reference-source">上海:科技出版社,2023</span>.
        </div>
        
        <div class="reference-item">
            <span class="reference-author">Johnson, M.</span>. 
            <span class="reference-title">Advanced CSS Layout Techniques</span>. 
            <span class="reference-source">New York: Tech Press, 2021</span>.
        </div>
    </div>
    
    <div class="explanation-box">
        <h3>技术实现原理</h3>
        <p>悬挂缩进通过负值的 text-indent 配合正值的 padding-left 实现:</p>
        <pre>
.reference-item {
    padding-left: 30px;      /* 为整个段落创建左边距 */
    text-indent: -30px;      /* 将首行向左移动相同距离 */
    line-height: 1.6;
}
        </pre>
        <p>这种技术常用于参考文献、书目列表等需要特殊排版格式的场景。</p>
    </div>
</body>
</html>

百分比缩进与响应式设计

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>响应式缩进设计 - 代码号编程</title>
    <style>
        :root {
            --base-font-size: 16px;
            --primary-color: #3498db;
            --secondary-color: #2c3e50;
            --background-color: #ecf0f1;
            --text-color: #2c3e50;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            font-size: var(--base-font-size);
            line-height: 1.6;
            color: var(--text-color);
            background: var(--background-color);
            padding: 20px;
            margin: 0;
        }
        
        .responsive-article {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        }
        
        /* 百分比缩进 - 基于父元素宽度 */
        .percentage-indent {
            text-indent: 10%;
            text-align: justify;
            line-height: 1.8;
            margin-bottom: 30px;
        }
        
        /* 响应式设计 - 移动端适配 */
        @media (max-width: 768px) {
            .responsive-article {
                padding: 20px;
            }
            
            .percentage-indent {
                text-indent: 5%;  /* 在小屏幕上减少缩进比例 */
                font-size: 0.95rem;
            }
        }
        
        @media (max-width: 480px) {
            .percentage-indent {
                text-indent: 1em;  /* 在超小屏幕上使用固定单位 */
                text-align: left;   /* 改为左对齐提高可读性 */
            }
        }
        
        .code-demo {
            background: #2d3436;
            color: #dfe6e9;
            padding: 25px;
            border-radius: 8px;
            margin: 25px 0;
            overflow-x: auto;
        }
        
        .info-panel {
            background: linear-gradient(135deg, #74b9ff 0%, #0984e3 100%);
            color: white;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <div class="responsive-article">
        <h1>响应式文本缩进设计</h1>
        
        <div class="info-panel">
            <h3>百分比缩进的优势</h3>
            <p>使用百分比单位的 text-indent 可以根据容器宽度自动调整,非常适合响应式网页设计。</p>
        </div>
        
        <div class="percentage-indent">
            在现代Web开发中,响应式设计已经成为基本要求。通过使用百分比单位的文本缩进,我们可确保在不同屏幕尺寸下保持合适的排版比例。这种方法的优势在于其自适应性——当容器宽度变化时,缩进量会自动按比例调整。
        </div>
        
        <div class="percentage-indent">
            对于移动设备用户而言,阅读体验尤为重要。过大的缩进在小屏幕上可能会浪费宝贵的显示空间,而过小的缩进又可能影响段落结构的清晰度。通过媒体查询和百分比缩进的结合使用,我们可以为不同设备提供较优的排版方案。
        </div>
        
        <div class="code-demo">
            <h4>CSS 代码示例</h4>
            <pre>
/* 基础百分比缩进 */
.percentage-indent {
    text-indent: 10%;
    text-align: justify;
    line-height: 1.8;
}

/* 平板设备适配 */
@media (max-width: 768px) {
    .percentage-indent {
        text-indent: 5%;
        font-size: 0.95rem;
    }
}

/* 手机设备适配 */
@media (max-width: 480px) {
    .percentage-indent {
        text-indent: 1em;
        text-align: left;
    }
}
            </pre>
        </div>
    </div>
</body>
</html>

列表项缩进技术

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>列表项缩进技术 - 代码号编程</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: #f8f9fa;
            padding: 40px 20px;
            color: #2c3e50;
            max-width: 1000px;
            margin: 0 auto;
        }
        
        .list-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 30px;
            margin-top: 40px;
        }
        
        .list-example {
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
        }
        
        /* 基础列表样式 */
        .basic-list {
            list-style-type: disc;
            padding-left: 40px;
        }
        
        .basic-list li {
            margin-bottom: 12px;
            line-height: 1.6;
        }
        
        /* 自定义缩进列表 */
        .custom-indent-list {
            list-style-type: none;
            padding-left: 0;
        }
        
        .custom-indent-list li {
            position: relative;
            padding-left: 30px;
            margin-bottom: 15px;
            line-height: 1.7;
        }
        
        .custom-indent-list li::before {
            content: "•";
            color: #3498db;
            font-weight: bold;
            position: absolute;
            left: 0;
            top: 0;
            font-size: 1.2em;
        }
        
        /* 多级嵌套列表 */
        .nested-list {
            list-style-type: none;
            padding-left: 20px;
        }
        
        .nested-list ul {
            list-style-type: none;
            padding-left: 40px;
            margin-top: 10px;
        }
        
        .nested-list li {
            position: relative;
            padding-left: 25px;
            margin-bottom: 10px;
        }
        
        .nested-list li::before {
            content: "→";
            color: #e74c3c;
            position: absolute;
            left: 0;
        }
        
        .nested-list ul li::before {
            content: "↳";
            color: #2ecc71;
        }
        
        .code-block {
            background: #34495e;
            color: #ecf0f1;
            padding: 20px;
            border-radius: 6px;
            margin-top: 20px;
            font-family: 'Consolas', monospace;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <h1>列表项缩进技术详解</h1>
    <p>探索不同类型的列表缩进实现方式及其应用场景</p>
    
    <div class="list-container">
        <div class="list-example">
            <h2>基础列表缩进</h2>
            <ul class="basic-list">
                <li>前端开发技术栈学习</li>
                <li>响应式网页设计原理</li>
                <li>CSS 布局与动画技术</li>
                <li>JavaScript 编程实践</li>
            </ul>
            
            <div class="code-block">
                .basic-list {<br>
                &nbsp;&nbsp;list-style-type: disc;<br>
                &nbsp;&nbsp;padding-left: 40px;<br>
                }
            </div>
        </div>
        
        <div class="list-example">
            <h2>自定义标记列表</h2>
            <ul class="custom-indent-list">
                <li>代码号编程HTML教程</li>
                <li>代码号编程CSS教程</li>
                <li>代码号编程JavaScript教程</li>
                <li>代码号编程前端框架教程</li>
            </ul>
            
            <div class="code-block">
                .custom-indent-list li {<br>
                &nbsp;&nbsp;position: relative;<br>
                &nbsp;&nbsp;padding-left: 30px;<br>
                }<br>
                .custom-indent-list li::before {<br>
                &nbsp;&nbsp;content: "•";<br>
                &nbsp;&nbsp;position: absolute;<br>
                &nbsp;&nbsp;left: 0;<br>
                }
            </div>
        </div>
        
        <div class="list-example">
            <h2>多级嵌套列表</h2>
            <ul class="nested-list">
                <li>Web开发基础
                    <ul>
                        <li>HTML5语义化标签</li>
                        <li>CSS3新特性</li>
                    </ul>
                </li>
                <li>前端框架
                    <ul>
                        <li>React.js原理与实践</li>
                        <li>Vue.js组件化开发</li>
                    </ul>
                </li>
            </ul>
            
            <div class="code-block">
                .nested-list ul {<br>
                &nbsp;&nbsp;padding-left: 40px;<br>
                }<br>
                .nested-list li::before {<br>
                &nbsp;&nbsp;content: "→";<br>
                &nbsp;&nbsp;position: absolute;<br>
                &nbsp;&nbsp;left: 0;<br>
                }
            </div>
        </div>
    </div>
</body>
</html>

本节课程知识要点

  1. text-indent 属性:掌握文本缩进的基本语法和取值方式

  2. 单位选择:理解不同单位(px、em、rem、%)的适用场景

  3. 悬挂缩进:学会使用负值 text-indent 实现特殊排版效果

  4. 响应式设计:掌握基于媒体查询的响应式缩进技术

  5. 列表缩进:了解列表项的特殊缩进处理方法

实践建议

  1. 使用相对单位:优先选择 em、rem 或百分比单位确保可伸缩性

  2. 考虑可访问性:确保缩进不会影响文本的可读性

  3. 响应式设计:为不同屏幕尺寸提供适当的缩进值

  4. 代码注释:为复杂的缩进实现添加必要的注释说明

  5. 浏览器测试:在不同浏览器中测试缩进效果的一致性

  6. 性能考虑:避免过度复杂的缩进实现影响渲染性能

CSS text-indent 属性是网页排版中不可或缺的工具,通过合理运用可以实现从基础段落缩进到复杂悬挂缩进的各种效果。掌握这些技术不仅能够提升网页的视觉美观度,还能显著改善用户的阅读体验。

关键要点总结:

  • 选择适当的单位和值实现精确的缩进控制

  • 使用负值 text-indent 创建悬挂缩进效果

  • 结合媒体查询实现响应式缩进设计

  • 为列表项设计合适的缩进方案

  • 始终考虑可访问性和浏览器兼容性

← CSS specificity 特异性 CSS text-stroke 文字描边 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号