下面有更多 "亲自试一试" 示例。
这个background-repeat
属性设置背景图片是否/如何重复。
默认情况下,一个背景图垂直和水平重复。
提示:背景图片根据背景位置属性。如果未指定背景位置,则图片始终放置在元素的左上角。
表中的数字指定完全支持该属性的第一个浏览器版本。
Property | |||||
---|---|---|---|---|---|
background-repeat | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
background-repeat: repeat|repeat-x|repeat-y|no-repeat|initial|inherit;
Value | Description | Demo |
---|---|---|
repeat | The background image is repeated both vertically and horizontally. The last image will be clipped if it does not fit. This is default | Demo ❯ |
repeat-x | The background image is repeated only horizontally | Demo ❯ |
repeat-y | The background image is repeated only vertically | Demo ❯ |
no-repeat | The background-image is not repeated. The image will only be shown once | Demo ❯ |
space | The background-image is repeated as much as possible without clipping. The first and last image is pinned to either side of the element, and whitespace is distributed evenly between the images | Demo ❯ |
round | The background-image is repeated and squished or stretched to fill the space (no gaps) | Demo ❯ |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
垂直和水平重复背景图片(这是默认设置):
body {
background-image: url("paper.gif");
background-repeat: repeat;
}
亲自试一试 »
不要重复背景图片。该图片只会显示一次:
body {
background-image: url("paper.gif");
background-repeat: no-repeat;
}
亲自试一试 »
使用背景重复:空间和背景重复:圆形:
#example2 {
border: 2px solid black;
padding: 25px;
background: url("w3css.gif");
background-repeat: space;
}
#example3 {
border: 1px solid black;
padding: 25px;
background: url("w3css.gif");
background-repeat: round;
}
亲自试一试 »
使用不同的背景属性创建"hero" 图片:
.hero-image {
background-image: url("photographer.jpg"); /* The image used */
background-color: #cccccc; /* Used if the image is unavailable */
height: 500px; /* You must set a specified height */
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
}
亲自试一试 »
CSS教程:CSS 背景
CSS 参考:背景位置属性
HTML DOM 参考:背景重复属性
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!