--- title: background-color slug: Web/CSS/background-color tags: - CSS Background - CSS Property - CSS_参考 - Reference translation_of: Web/CSS/background-color ---
CSS属性中的 background-color 会设置元素的背景色, 属性的值为颜色值或关键字"transparent"二者选其一.
/* Keyword values */ background-color: red; /* Hexadecimal value */ background-color: #bbff00; /* RGB value */ background-color: rgb(255, 255, 128); /* HSLA value */ background-color: hsla(50, 33%, 25%, 0.75); /* Special keyword values */ background-color: currentColor; background-color: transparent; /* Global values */ background-color: inherit; background-color: initial; background-color: unset;
background-color
属性只能使用 <color>
值。<color>
transparent
是一种颜色。<div class="exampleone"> Lorem ipsum dolor sit amet, consectetuer </div> <div class="exampletwo"> Lorem ipsum dolor sit amet, consectetuer </div> <div class="examplethree"> Lorem ipsum dolor sit amet, consectetuer </div>
.exampleone { background-color: teal; color: white; } .exampletwo { background-color: rgb(153,102,153); color: rgb(255,255,204); } .examplethree { background-color: #777799; color: #FFFFFF; }
{{EmbedLiveSample("Examples","200","150")}}
规范 | 状态 | 注释 |
---|---|---|
{{SpecName('CSS3 Backgrounds', '#background-color', 'background-color')}} | {{Spec2('CSS3 Backgrounds')}} | Though technically removing thetransparent keyword, this doesn't change anything as it has been incorporated as a true {{cssxref("<color>")}} |
{{SpecName('CSS2.1', 'colors.html#propdef-background-color', 'background-color')}} | {{Spec2('CSS2.1')}} | 没有变化 |
{{SpecName('CSS1', '#background-color', 'background-color')}} | {{Spec2('CSS1')}} | 初始定义 |