tab-size

Baseline 已广泛支持

此特性已发展成熟,可在多种设备和浏览器版本上使用。自 ⁨2021 年 8 月⁩ 起,它已在所有浏览器中可用。

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

试一试

tab-size: 10px;
tab-size: 16px;
tab-size: 24px;
tab-size: 4;
<section id="default-example">
  <pre id="example-element">&#9;123</pre>
</section>
#example-element {
  border: 1px solid;
}

语法

css
/* <number> 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: revert;
tab-size: revert-layer;
tab-size: unset;

<number>

作为制表符宽度的,一个空格字符(U+0020)前进宽度的倍数。必须是非负数。前进宽度是指光标或打印头在打印下一个字符之前移动的距离。

<length>

制表符的宽度。必须是非负数。

正式定义

初始值8
应用于块容器
继承性
计算值指定的整数或绝对长度
动画类型一个长度

正式语法

tab-size = 
<number [0,∞]> |
<length [0,∞]>

示例

按字符数展开

css
pre {
  tab-size: 4; /* Set tab size to 4 characters wide */
}

折叠制表符

css
pre {
  tab-size: 0; /* Remove indentation */
}

默认制表符大小与自定义大小

此示例比较了默认制表符大小和自定义制表符大小。请注意,white-space 设置为 pre 以防止制表符折叠。

HTML

html
<p>no tab</p>
<p>&#0009;default tab size of 8 characters wide</p>
<p class="custom-number">&#0009;custom tab size of 3 characters wide</p>
<p>&nbsp;&nbsp;&nbsp;3 spaces, equivalent to the custom tab size</p>
<p class="custom-length">&#0009;custom tab size of 50px wide</p>

CSS

css
p {
  white-space: pre;
}

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

.custom-length {
  tab-size: 50px;
}

结果

规范

规范
CSS Text Module Level 3
# tab-size-property

浏览器兼容性

另见