overflow
CSS
shorthand property
sets the desired behavior for an element's overflow — i.e. when an element's content is too big to fit in its
block formatting context
— in both directions.
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.
此特性是下列 CSS 特性的简写:
/* Keyword values */ overflow: visible; overflow: hidden; overflow: clip; overflow: scroll; overflow: auto; overflow: hidden visible; /* Global values */ overflow: inherit; overflow: initial; overflow: unset;
overflow
property is specified as one or two keywords chosen from the list of values below. If two keywords are specified, the first applies to
overflow-x
and the second to
overflow-y
. Otherwise, both
overflow-x
and
overflow-y
are set to the same value.
visible
Content is not clipped and may be rendered outside the padding box.
hidden
offsetLeft
), so the element is still a scroll container.
clip
hidden
, the content is clipped to the element's padding box. The difference between
clip
and
hidden
is that the
clip
keyword also forbids all scrolling, including programmatic scrolling. The box is not a scroll container, and does not start a new formatting context. If you wish to start a new formatting context, you can use
display: flow-root
to do so.
scroll
Content is clipped if necessary to fit the padding box. Browsers always display scrollbars whether or not any content is actually clipped, preventing scrollbars from appearing or disappearing as content changes. Printers may still print overflowing content.
auto
visible
, but still establishes a new block formatting context. Desktop browsers provide scrollbars if content overflows.
overlay
auto
, but with the scrollbars drawn on top of content instead of taking up space. Only supported in WebKit-based (e.g., Safari) and Blink-based (e.g., Chrome or Opera) browsers.
-moz-scrollbars-none
[1]
overflow: hidden
代替。
-moz-scrollbars-horizontal
[1]
overflow-x
: scroll
and
overflow-y
: hidden
,或
overflow: hidden scroll
代替。
-moz-scrollbars-vertical
[1]
overflow-x
: hidden
and
overflow-y
: scroll
,或
overflow: scroll hidden
代替。
-moz-hidden-unscrollable
overflow: clip
代替。
[1] As of Firefox 63, this feature is behind a feature preference setting. In about:config, set
layout.css.overflow.moz-scrollbars.enabled
to
true
Overflow options include clipping, showing scrollbars, or displaying the content flowing out of its container into the surrounding area.
Specifying a value other than
visible
(the default) creates a new
block formatting context
. This is necessary for technical reasons — if a float intersected with the scrolling element it would forcibly rewrap the content after each scroll step, leading to a slow scrolling experience.
In order for
overflow
to have an effect, the block-level container must have either a set height (
height
or
max-height
) or
white-space
设为
nowrap
.
Setting one axis to
visible
(the default) while setting the other to a
different
value results in
visible
behaving as
auto
.
JavaScript
Element.scrollTop
property may be used to scroll an HTML element even when
overflow
is set to
hidden
.
| 初始值 |
visible
|
|---|---|
| 适用于 | Block-containers, flex containers, and grid containers |
| 继承 | no |
| 计算值 |
as each of the properties of the shorthand:
|
| 动画类型 | discrete |
[ visible | hidden | clip | scroll | auto ]{1,2}
p {
width: 12em;
height: 6em;
border: dotted;
overflow: visible; /* content is not clipped */
}
visible
(default)
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.
p { overflow: hidden; /* no scrollbars are provided */ }
p { overflow: scroll; /* always show scrollbars */ }
overflow: scroll
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.
p { overflow: auto; /* append scrollbars if necessary */ }
overflow: auto
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Overflow Module Level 3
The definition of 'overflow' in that specification. |
工作草案 | Changed syntax to allow one or two keywords instead of only one |
|
CSS Level 2 (Revision 1)
The definition of 'overflow' in that specification. |
推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
overflow
|
Chrome 完整支持 1 | Edge 完整支持 12 |
Firefox
完整支持
1
注意事项
|
IE
完整支持
4
注意事项
|
Opera 完整支持 7 | Safari 完整支持 1 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
clip
value
Experimental
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
完整支持
81
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No |
Firefox Android
完整支持
81
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
Multiple keyword syntax for
overflow-x
and
overflow-y
|
Chrome 完整支持 68 | Edge 完整支持 79 | Firefox 完整支持 61 | IE 不支持 No | Opera 完整支持 55 | Safari 不支持 No | WebView Android 完整支持 68 | Chrome Android 完整支持 68 | Firefox Android 完整支持 61 | Opera Android 完整支持 48 | Safari iOS 不支持 No | Samsung Internet Android 完整支持 10.0 |
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
使用非标名称。
text-overflow
,
white-space
,
overflow-x
,
overflow-y
,
overflow-inline
,
overflow-block
,
clip
,
display