Thuộc tính nền vị trí
Ví dụ
Thay đổi vị trí của hình nền:
document.body.style.backgroundPosition = "top right";
Hãy tự mình thử »Thêm ví dụ "Hãy tự mình thử" bên dưới.
Sự miêu tả
Thuộc tính BackgroundPosition đặt hoặc trả về vị trí của hình nền trong một phần tử.
Xem thêm:
Kiểu HTML: Thuộc tính nền
Hướng dẫn CSS: Nền CSS
Hướng dẫn CSS3: Nền CSS3
Tham khảo CSS: Thuộc tính hình nền
Tham khảo CSS: Thuộc tính vị trí nền của Thr
Cú pháp
Trả về 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ả về: | Một Chuỗi, biểu thị vị trí của hình nền |
Phiên bản CSS | CSS1 |
Hỗ trợ trình duyệt
backgroundPosition
là một tính năng CSS1 (1996).
Nó được hỗ trợ đầy đủ trong tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
Thêm ví dụ
Ví dụ
Thay đổi vị trí của hình nền trong phần tử <div> ở giữa phía dưới:
document.getElementById("myDiv").style.backgroundPosition = "center bottom";
Hãy tự mình thử »Ví dụ
Thay đổi vị trí của hình nền trong phần tử <div> thành 200px theo chiều ngang và 40px theo chiều dọc:
document.getElementById("myDiv").style.backgroundPosition = "200px 40px";
Hãy tự mình thử »Ví dụ
Trả về vị trí của hình nền trong phần tử <div>:
document.getElementById("myDiv").style.backgroundPosition;
Hãy tự mình thử »