tab-size

Experimental: 这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

CSS 属性 tab-size 用于自定义制表符 (U+0009) 的宽度。

/* <integer> values */
tab-size: 4;
tab-size: 0;

/* <length> values */
tab-size: 10px;
tab-size: 2em;

/* Global values */
tab-size: inherit;
tab-size: initial;
tab-size: unset;
初始值8
适用元素block containers
是否是继承属性yes
计算值the specified integer or an absolute length
Animation typea length

语法

<integer>

制表符对应的空格数。必须为非负值。

<length>

制表符的宽度。必须为非负值。

形式语法

tab-size = 
<number> |
<length>

示例

pre {
  tab-size: 4; /* 将制表符宽度设为 4 个空格 */
}
pre {
  tab-size: 0; /* 移除缩进效果 */
}
pre {
  tab-size: 2; /* 将制表符宽度设为 2 个空格 */
}

在线演示

本示例比较了默认制表符宽度和自定义制表符宽度。注意 white-space 被设置为 pre 以防止制表符收起。

HTML

<p>no tab</p>
<p>&#0009;default tab size of 8 spaces</p>
<p class="custom">&#0009;custom tab  size of 3 spaces</p>
<p>&nbsp;&nbsp;&nbsp;3 spaces, equivalent to the custom tab size</p>

CSS

p {
  white-space: pre;
}

.custom {
  -moz-tab-size: 3;
  tab-size: 3;
}

结果

规范

Specification
CSS Text Module Level 3
# tab-size-property

浏览器兼容性

BCD tables only load in the browser

参见