Thuộc tính màu nền của phong cách
Thêm ví dụ "Hãy tự mình thử" bên dưới.
Sự miêu tả
Thuộc tính BackgroundColor đặt hoặc trả về màu nền của một phần tử.
Cú pháp
Trả về thuộc tính BackgroundColor:
object .style.backgroundColor
Đặt thuộc tính màu nền:
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ả về: | Một chuỗi, đại diện cho màu nền |
Phiên bản CSS | CSS1 |
Hỗ trợ trình duyệt
backgroundColor
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ụ
Đặt màu nền của phần tử <div> cụ thể:
document.getElementById("myDiv").style.backgroundColor = "lightblue";
Hãy tự mình thử »Ví dụ
Trả về màu nền của phần tử <div> cụ thể:
let color = document.getElementById("myDiv").style.backgroundColor;
Hãy tự mình thử »Ví dụ
Trả về màu nền của tài liệu:
let color = document.body.style.backgroundColor;
Hãy tự mình thử »