Thuộc tính lặp lại nền CSS
Thí 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;
}
Thêm các ví dụ "Hãy tự mình thử" bên dưới.
Định nghĩa và Cách sử dụng
Thuộc background-repeat
tính đặt nếu / cách một hình nền sẽ được lặp lại.
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 background-position . 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: | nói 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ỗ 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 |
Lưu ý: IE8 trở về trước không hỗ trợ nhiều hình nền trên một phần tử.
Cú pháp CSS
background-repeat: repeat|repeat-x|repeat-y|no-repeat|initial|inherit;
Giá trị tài sản
Value | Description | Play it |
---|---|---|
repeat | The background image is repeated both vertically and horizontally. The last image will be clipped if it does not fit. This is default | |
repeat-x | The background image is repeated only horizontally | |
repeat-y | The background image is repeated only vertically | |
no-repeat | The background-image is not repeated. The image will only be shown once | |
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 | |
round | The background-image is repeated and squished or stretched to fill the space (no gaps) | |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
Các ví dụ khác
Thí 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;
}
Thí dụ
Chỉ lặp lại hình nền theo chiều ngang:
body
{
background-image: url("paper.gif");
background-repeat: repeat-x;
}
Thí dụ
Không lặp lại một 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;
}
Thí dụ
Sử dụng background-repeat: space và background-repeat: round:
#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;
}
Thí 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 */
}
Các trang liên quan
Hướng dẫn CSS: Nền CSS
Tham chiếu CSS: thuộc tính background-position
Tham chiếu DOM HTML: thuộc tính backgroundRepeat