text-indent CSS property sets the length of empty space (indentation) that is put before lines of text in a block.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Horizontal spacing is with respect to the left (or right, for right-to-left layout) edge of the containing block-level element's content box.

句法

/* <length> values */
text-indent: 3mm;
text-indent: 40px;
/* <percentage> value
   relative to the containing block width */
text-indent: 15%;
/* Keyword values */
text-indent: 5em each-line;
text-indent: 5em hanging;
text-indent: 5em hanging each-line;
/* Global values */
text-indent: inherit;
text-indent: initial;
text-indent: unset;
					

<length>
Indentation is specified as an absolute <length> . Negative values are allowed. See <length> values for possible units.
<percentage>
Indentation is a <percentage> of the containing block's width.
each-line
Indentation affects the first line of the block container as well as each line after a forced line break , but does not affect lines after a soft wrap break .
hanging
Inverts which lines are indented. All lines except the first line will be indented.

形式定义

初始值 0
适用于 block containers
继承 yes
百分比 refer to the width of the containing block
计算值 the percentage as specified or the absolute length, plus any keywords as specified
动画类型 a length , percentage or calc();

形式句法

<length-percentage> && hanging? && each-line?

where
<length-percentage> = <length> | <percentage>

范例

Simple indent

HTML

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
    nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
    nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
					

CSS

p {
  text-indent: 5em;
  background: powderblue;
}
					

结果

Percentage indent

HTML

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
    nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
    nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
					

CSS

p {
  text-indent: 30%;
  background: plum;
}
					

结果

规范

规范 状态 注释
CSS Text Module Level 3
The definition of 'text-indent' in that specification.
工作草案 添加 hanging and each-line keywords.
CSS Level 2 (Revision 1)
The definition of 'text-indent' in that specification.
推荐 The behavior with display: inline-block and anonymous block boxes is explicitly defined.
CSS Level 1
The definition of 'text-indent' in that specification.
推荐 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
text-indent Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 1 IE 完整支持 3 Opera 完整支持 3.5 Safari 完整支持 1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 14 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0
each-line Chrome 不支持 No Edge 不支持 No Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
hanging Chrome 不支持 No Edge 不支持 No Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: