Thuộc tính lặp lại nền CSS
Ví dụ
Chỉ lặp lại hình nền theo chiều dọc:
body
{
background-image: url("paper.gif");
background-repeat: repeat-y;
}
Hãy tự mình thử »Thêm ví dụ "Hãy tự mình thử" bên dưới.
Định nghĩa và cách sử dụng
Thuộc tính background-repeat
đặt nếu/cách lặp lại hình nền.
Theo mặc định, hình nền được lặp lại theo cả chiều dọc và chiều ngang.
Mẹo: Hình nền được đặt theo thuộc tính vị trí nền . Nếu không chỉ định vị trí nền, hình ảnh luôn được đặt ở góc trên cùng bên trái của phần tử.
Giá trị mặc định: | lặp lại |
---|---|
Thừa hưởng: | KHÔNG |
Hoạt hình: | KHÔNG. Đọc về hoạt hình |
Phiên bản: | CSS1 |
Cú pháp JavaScript: | đối tượng .style.backgroundRepeat="repeat-x" Hãy thử nó |
Hỗ trợ trình duyệt
Các số trong bảng chỉ định phiên bản trình duyệt đầu tiên hỗ trợ đầy đủ thuộc tính.
Property | |||||
---|---|---|---|---|---|
background-repeat | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Cú pháp CSS
background-repeat: repeat|repeat-x|repeat-y|no-repeat|initial|inherit;
Giá trị tài sản
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 |
Thêm ví dụ
Ví dụ
Lặp lại hình nền theo cả chiều dọc và chiều ngang (đây là mặc định):
body
{
background-image: url("paper.gif");
background-repeat: repeat;
}
Hãy tự mình thử »Ví dụ
Chỉ lặp lại hình nền theo chiều ngang:
body
{
background-image: url("paper.gif");
background-repeat: repeat-x;
}
Hãy tự mình thử »Ví dụ
Không lặp lại hình nền. Hình ảnh sẽ chỉ được hiển thị một lần:
body
{
background-image: url("paper.gif");
background-repeat: no-repeat;
}
Hãy tự mình thử »Ví dụ
Sử dụng lặp lại nền: dấu cách và lặp lại nền: tròn:
#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;
}
Hãy tự mình thử »Ví dụ
Sử dụng các thuộc tính nền khác nhau để tạo hình ảnh "anh hùng":
.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 */
}
Hãy tự mình thử »Trang liên quan
Hướng dẫn CSS: Nền CSS
Tham chiếu CSS: thuộc tính vị trí nền
Tham chiếu DOM HTML: thuộc tính BackgroundRepeat