Nền phong cách
Thí dụ
Đặt màu nền cho tài liệu:
document.body.style.backgroundColor = "red";
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 backgroundColor đặt hoặc trả về màu nền của 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 | |||||
---|---|---|---|---|---|
backgroundColor | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Cú pháp
Trả lại thuộc tính backgroundColor:
object.style.backgroundColor
Đặt thuộc tính backgroundColor:
object.style.backgroundColor = "color|transparent|initial|inherit"
Giá trị tài sản
Value | Description |
---|---|
color | Specifies the background color. Look at CSS Color Values for a complete list of possible color values |
transparent | Default. The background color is transparent (underlying content will shine through) |
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: | trong suốt |
---|---|
Giá trị trả lại: | Một chuỗi, đại diện cho màu nền |
Phiên bản CSS | CSS1 |
Các ví dụ khác
Thí dụ
Đặt màu nền của một phần tử <div> cụ thể:
document.getElementById("myDiv").style.backgroundColor = "lightblue";
Thí dụ
Trả lại màu nền của một phần tử <div> cụ thể:
alert(document.getElementById("myDiv").style.backgroundColor);
Thí dụ
Trả lại màu nền của tài liệu:
alert(document.body.style.backgroundColor);
Các trang liên quan
Hướng dẫn CSS: Nền CSS
Tham chiếu CSS: thuộc tính background-color
Tham chiếu DOM HTML: thuộc tính nền