background-repeat
CSS
property sets how background images are repeated. A background image can be repeated along the horizontal and vertical axes, or not repeated at all.
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.
By default, the repeated images are clipped to the size of the element, but they can be scaled to fit (using
round
) or evenly distributed from end to end (using
space
).
/* Keyword values */ background-repeat: repeat-x; background-repeat: repeat-y; background-repeat: repeat; background-repeat: space; background-repeat: round; background-repeat: no-repeat; /* Two-value syntax: horizontal | vertical */ background-repeat: repeat space; background-repeat: repeat repeat; background-repeat: round space; background-repeat: no-repeat round; /* Global values */ background-repeat: inherit; background-repeat: initial; background-repeat: unset;
<repeat-style>
The one-value syntax is a shorthand for the full two-value syntax:
| Single value | Two-value equivalent |
repeat-x
|
repeat no-repeat
|
repeat-y
|
no-repeat repeat
|
repeat
|
repeat repeat
|
space
|
space space
|
round
|
round round
|
no-repeat
|
no-repeat no-repeat
|
repeat
|
The image is repeated as much as needed to cover the whole background image painting area. The last image will be clipped if it doesn't fit. |
space
|
The image is repeated as much as possible without clipping. The first and last images are pinned to either side of the element, and whitespace is distributed evenly between the images. The
background-position
property is ignored unless only one image can be displayed without clipping. The only case where clipping happens using
space
is when there isn't enough room to display one image.
|
round
|
As the allowed space increases in size, the repeated images will stretch (leaving no gaps) until there is room (space left >= half of the image width) for another one to be added. When the next image is added, all of the current ones compress to allow room. Example: An image with an original width of 260px, repeated three times, might stretch until each repetition is 300px wide, and then another image will be added. They will then compress to 225px. |
no-repeat
|
The image is not repeated (and hence the background image painting area will not necessarily be entirely covered). The position of the non-repeated background image is defined by the
background-position
CSS property.
|
| 初始值 |
repeat
|
|---|---|
| 适用于 |
所有元素。它还适用于
::first-letter
and
::first-line
.
|
| 继承 | no |
| 计算值 | a list, each item consisting of two keywords, one per dimension |
| 动画类型 | discrete |
<repeat-style>#where
<repeat-style> = repeat-x | repeat-y | [ repeat | space | round | no-repeat ]{1,2}
<ol>
<li>no-repeat
<div class="one"></div>
</li>
<li>repeat
<div class="two"></div>
</li>
<li>repeat-x
<div class="three"></div>
</li>
<li>repeat-y
<div class="four"></div>
</li>
<li>space
<div class="five"></div>
</li>
<li>round
<div class="six"></div>
</li>
<li>repeat-x, repeat-y (multiple images)
<div class="seven"></div>
</li>
</ol>
/* Shared for all DIVS in example */
ol,
li {
margin: 0;
padding: 0;
}
li {
margin-bottom: 12px;
}
div {
background-image: url(https://mdn.mozillademos.org/files/12005/starsolid.gif);
width: 160px;
height: 70px;
}
/* Background repeats */
.one {
background-repeat: no-repeat;
}
.two {
background-repeat: repeat;
}
.three {
background-repeat: repeat-x;
}
.four {
background-repeat: repeat-y;
}
.five {
background-repeat: space;
}
.six {
background-repeat: round;
}
/* Multiple images */
.seven {
background-image: url(https://mdn.mozillademos.org/files/12005/starsolid.gif),
url(https://developer.cdn.mozilla.net/media/redesign/img/favicon32.png);
background-repeat: repeat-x,
repeat-y;
height: 144px;
}
In this example, each list item is matched with a different value of
background-repeat
.
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Backgrounds and Borders Module Level 3
The definition of 'background-repeat' in that specification. |
候选推荐 |
Adds support for multiple background images, the two-value syntax allowing distinct repetition behavior for the horizontal and vertical directions, the
space
and
round
keywords, and for backgrounds on inline-level elements by precisely defining the background painting area.
|
|
CSS Level 2 (Revision 1)
The definition of 'background-repeat' in that specification. |
推荐 | No significant changes. |
|
CSS Level 1
The definition of 'background-repeat' in that specification. |
推荐 | 初始定义。 |
The compatibility table in 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 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
background-repeat
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 3.5 | Safari 完整支持 1 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
| Multiple backgrounds | Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 3.6 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 1.3 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 11 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
round
and
space
关键词
|
Chrome 完整支持 30 | Edge 完整支持 12 | Firefox 完整支持 49 | IE 完整支持 9 |
Opera
完整支持
17
|
Safari 完整支持 7 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 30 | Firefox Android 完整支持 49 | Opera Android 完整支持 18 | Safari iOS 完整支持 8 | Samsung Internet Android 完整支持 2.0 |
| Two-value syntax (different values for x & y directions) | Chrome 完整支持 3 | Edge 完整支持 12 | Firefox 完整支持 13 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 5 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 14 | Opera Android 完整支持 11 | Safari iOS 完整支持 4 | Samsung Internet Android 完整支持 1.0 |
完整支持
background
background-attachment
background-clip
background-color
background-image
background-origin
background-position
background-position-x
background-position-y
background-repeat
background-size
border
border-bottom
border-bottom-color
border-bottom-left-radius
border-bottom-right-radius
border-bottom-style
border-bottom-width
border-color
border-image
border-image-outset
border-image-repeat
border-image-slice
border-image-source
border-image-width
border-left
border-left-color
border-left-style
border-left-width
border-radius
border-right
border-right-color
border-right-style
border-right-width
border-style
border-top
border-top-color
border-top-left-radius
border-top-right-radius
border-top-style
border-top-width
border-width
box-shadow