--- title: background-color slug: Web/CSS/background-color translation_of: Web/CSS/background-color ---
{{CSSRef}}

background-color CSS property 是用來設定HTML元素的背景顏色,值可以是顏色亦可以是特定關鍵字-- transparent.

語法

background-color: red;
background-color: rgb(255, 255, 128);
background-color: hsla(50, 33%, 25%, 0.75);
background-color: currentColor;
background-color: transparent;
background-color: #bbff00;

background-color: inherit;

參數

<color>
一個 CSS {{cssxref("<color>")}} 用來表示一致的背景顏色。即便設定了一個或多個 {{cssxref("background-image")}} ,背景渲染上依舊會渲染這顏色,若是圖片並非不透明圖,在透明區域就能看見。在 CSS 中, transparent 是一種顏色

正式語法

{{csssyntax}}

範例

HTML

<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>

CSS

.exampleone {
  background-color: teal;
  color: white;
}

.exampletwo {
  background-color: rgb(153,102,153);
  color: rgb(255,255,204);
}

.examplethree {
  background-color: #777799;
  color: #FFFFFF;
}

Result

{{EmbedLiveSample("範例","200","150")}}

規格定義

Specification Status Comment
{{SpecName('CSS3 Backgrounds', '#background-color', 'background-color')}} {{Spec2('CSS3 Backgrounds')}} 技術上來說,移除了 transparent 關鍵字,但實際上並未發生任何改變,因為它被列入至正式的 {{cssxref("<color>")}}
{{SpecName('CSS2.1', 'colors.html#propdef-background-color', 'background-color')}} {{Spec2('CSS2.1')}} 無變動
{{SpecName('CSS1', '#background-color', 'background-color')}} {{Spec2('CSS1')}} 原始定義。

{{cssinfo}}

Browser compatibility

{{Compat("css.properties.background-color")}}

延伸閱讀