Thuộc tính vị trí nền CSS
Thí dụ
Cách định vị hình nền:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
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-position
tính đặt vị trí bắt đầu của hình nền.
Mẹo: Theo mặc định, hình nền được đặt ở góc trên cùng bên trái của một phần tử và được lặp lại theo cả chiều dọc và chiều ngang.
Giá trị mặc định: | 0% 0% |
---|---|
Thừa hưởng: | không |
Hoạt hình: | Đúng. Đọc về hoạt hình |
Phiên bản: | CSS1 |
Cú pháp JavaScript: | đối tượng .style.backgroundPosition = "center" |
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-position | 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-position: value;
Giá trị tài sản
Value | Description | Play it |
---|---|---|
left top left center left bottom right top right center right bottom center top center center center bottom |
If you only specify one keyword, the other value will be "center" | |
x% y% | The first value is the horizontal position and the second value is the vertical. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%. Default value is: 0% 0% | |
xpos ypos | The first value is the horizontal position and the second value is the vertical. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and positions | |
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ụ
Cách đặt ảnh nền được căn giữa ở trên cùng:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
Thí dụ
Cách đặt hình nền ở phía dưới bên phải:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
Thí dụ
Cách định vị hình nền bằng phần trăm:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
Thí dụ
Cách định vị hình nền bằng pixel:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
}
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-image
Tham chiếu DOM HTML: thuộc tính backgroundPosition