tab-size

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

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
适用媒体visual
计算值the specified integer or an absolute length
Animation typea length
正规顺序the unique non-ambiguous order defined by the formal grammar

语法

<integer>
制表符对应的空格数。必须为非负值。
<length>
制表符的宽度。必须为非负值。

正式语法

<integer> | <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 Status Comment
CSS Text Module Level 3
tab-size
Working Draft Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
tab-size
Experimental
Chrome Full support 21
Notes
Full support 21
Notes
Notes This property is not yet animatable.
Edge No support NoFirefox Full support 4
Prefixed Notes
Full support 4
Prefixed Notes
Prefixed Implemented with the vendor prefix: -moz-
Notes See bug 737785 for the status of unprefixing this property.
Notes Before Firefox 53, this property was not animatable.
IE No support NoOpera Full support 15
Full support 15
Full support 10.5
Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari Full support 6.1WebView Android Full support 4.4Chrome Android Full support 25
Notes
Full support 25
Notes
Notes This property is not yet animatable.
Firefox Android Full support 4
Prefixed Notes
Full support 4
Prefixed Notes
Prefixed Implemented with the vendor prefix: -moz-
Notes See bug 737785 for the status of unprefixing this property.
Notes Before Firefox 53, this property was not animatable.
Opera Android Full support 14
Full support 14
Full support 11
Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari iOS Full support 7Samsung Internet Android ?
<length>
Experimental
Chrome Full support 42Edge No support NoFirefox Full support 53IE No support NoOpera Full support 29Safari No support NoWebView Android Full support 56Chrome Android Full support 42Firefox Android Full support 53Opera Android Full support 29Safari iOS No support NoSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

参见