CSS background-color 属性
CSS 属性中的 background-color 会设置元素的背景色, 属性的值为颜色值或关键字 transparent 二者选其一.
实例
<html><head><style type="text/css">body {background-color: yellow}h1 {background-color: #00ff00}h2 {background-color: transparent}p {background-color: rgb(250,0,255)}p.no2 {background-color: gray; padding: 20px;}</style></head><body><h1>这是标题 1</h1><h2>这是标题 2</h2><p>这是段落</p><p class="no2">这个段落设置了内边距。</p></body></html>
浏览器支持
表格中的数字注明了完全支持该属性的首个浏览器版本。
| 属性 | |||||
|---|---|---|---|---|---|
| background-color | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
定义和用法
background-color 属性设置元素的背景颜色。
元素背景的范围
background-color 属性为元素设置一种纯色。这种颜色会填充元素的内容、内边距和边框区域,扩展到元素边框的外边界(但不包括外边距)。如果边框有透明部分(如虚线边框),会透过这些透明部分显示出背景色。
transparent 值
尽管在大多数情况下,没有必要使用 transparent。不过如果您不希望某元素拥有背景色,同时又不希望用户对浏览器的颜色设置影响到您的设计,那么设置 transparent 值还是有必要的。
| 默认值: | transparent |
|---|---|
| 继承性: | no |
| 版本: | CSS1 |
| JavaScript 语法: | object.style.backgroundColor="#00FF00" |
可能的值
| 值 | 描述 |
|---|---|
| color_name | 规定颜色值为颜色名称的背景颜色(比如 red)。 |
| hex_number | 规定颜色值为十六进制值的背景颜色(比如 #ff0000)。 |
| rgb_number | 规定颜色值为 rgb 代码的背景颜色(比如 rgb(255,0,0))。 |
| transparent | 默认。背景颜色为透明。 |
| inherit | 规定应该从父元素继承 background-color 属性的设置。 |
相关页面
CSS 教程:CSS 背景HTML DOM 参考手册:backgroundColor 属性