Nền phong cách
Thí dụ
Thay đổi vị trí của hình nền:
document.body.style.backgroundPosition = "top right";
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 tính backgroundPosition đặt hoặc trả về vị trí của hình ảnh nền trong một phần tử.
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 | |||||
---|---|---|---|---|---|
backgroundPosition | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Cú pháp
Trả lại thuộc tính backgroundPosition:
object.style.backgroundPosition
Đặt thuộc tính backgroundPosition:
object.style.backgroundPosition = value
Giá trị tài sản
Value | Description |
---|---|
top left top center top right center left center center center right bottom left bottom center bottom right |
If you only specify one keyword, the other value will be "center". |
x% y% | The x value indicates the horizontal position and the y value indicates the vertical position. 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%. |
xpos ypos | The x value indicates the horizontal position and the y value indicates the vertical position. 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 units |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Chi tiết kỹ thuật
Giá trị mặc định: | 0% 0% |
---|---|
Giá trị trả lại: | Một chuỗi, đại diện cho vị trí của hình nền |
Phiên bản CSS | CSS1 |
Các ví dụ khác
Thí dụ
Thay đổi vị trí của hình nền trong phần tử <div> thành phần dưới giữa:
document.getElementById("myDiv").style.backgroundPosition = "center bottom";
Thí dụ
Thay đổi vị trí của hình nền trong phần tử <div> thành 200px ngang và 40px dọc:
document.getElementById("myDiv").style.backgroundPosition = "200px 40px";
Thí dụ
Trả lại vị trí của hình nền trong phần tử <div>:
document.getElementById("myDiv").style.backgroundPosition;
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 CSS: thuộc tính background-position
Tham chiếu DOM HTML: thuộc tính nền