CSS background-color Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Backgrounds & Color

What You’ll Learn

The background-color property is one of the most common CSS properties. It sets the solid color behind an element’s content using named colors, hex, rgb, hsl, and more.

01

Solid Backgrounds

Fill element areas.

02

Syntax

One simple property.

03

Color Formats

Named, hex, rgb, hsl.

04

transparent

Default see-through value.

05

Alpha Colors

Semi-transparent panels.

06

CSS Variables

Reusable theme colors.

Definition and Usage

The background-color CSS property sets the background color of an element. It paints a solid color behind the content area, which helps you separate sections, highlight cards, and improve readability.

Unlike background-image, which can use photos or gradients, background-color applies one flat color. It is often combined with padding, borders, and other background properties.

💡
Beginner Tip

Start with a named color like lightblue or a hex value like #2563eb. Once that works, try rgba() when you need transparency.

📝 Syntax

Apply background-color to any element that should have a colored background:

syntax.css
selector {

  background-color: color;

}

Basic Example

background-color.css
div {

  background-color: lightblue;

  padding: 20px;

  border: 1px solid #000;

}

Syntax Rules

  • The value can be any valid CSS color.
  • The initial value is transparent.
  • The property is not inherited, but transparent backgrounds can reveal a parent’s background.
  • It can be animated when the color value changes over time.
  • Use background-color for solid fills and background-image for photos or gradients.

⚡ Quick Reference

QuestionAnswer
Initial valuetransparent
Applies toAll elements
InheritedNo
AnimatableYes
Common useCards, sections, alerts, buttons, panels

Default Value

The initial value of background-color is transparent. That means the element does not paint its own background color, so the parent element’s background can show through.

💎 Property Values

background-color accepts standard CSS color values. These are the most common ones for beginners.

ValueExampleMeaning
Named colorbackground-color: lightblue;Uses a CSS color keyword
Hex colorbackground-color: #2563eb;Uses a hexadecimal color
RGB or RGBAbackground-color: rgba(37, 99, 235, 0.8);Uses red, green, blue, and optional alpha
HSL or HSLAbackground-color: hsl(142 72% 29%);Uses hue, saturation, and lightness
CSS variablebackground-color: var(--surface);Uses a reusable theme color
transparentbackground-color: transparent;No background color is painted
lightblue #2563eb rgb() hsl() rgba()
named

Easy to read and great for quick demos and learning.

Example: lightblue

hex

Very common in design systems and brand color palettes.

Example: #7c3aed

rgb()

Useful when you want numeric control over red, green, and blue.

Example: rgb(5, 150, 105)

hsl()

Helpful when adjusting lightness or saturation is easier than hex.

Example: hsl(24 95% 53%)

rgba()

Lets the background show through with partial transparency.

Example: rgba(219, 39, 119, 0.65)

transparent

The default value. No background color is painted.

Parent backgrounds can show through.

background-color vs color

PropertyStylesBest for
background-colorThe area behind the element’s contentPanels, sections, cards, page blocks
colorText and other foreground contentHeadings, paragraphs, links, button labels

👀 Live Preview

These swatches use different color formats with the same property:

lightblue
#2563eb
rgb()
rgba()

All four boxes use background-color with different valid color values.

Examples Gallery

Try background-color with named colors, hex values, rgba transparency, and CSS theme variables.

📚 Basic Background Colors

Start with simple solid colors before moving to transparency and theme variables.

Example 1 — Named Color on a div

Set a quick background color using a CSS color keyword.

named-color.html
<style>

  div {

    background-color: lightblue;

    padding: 20px;

    border: 1px solid #000;

  }

</style>



<div>This is a div with a light blue background.</div>
Try It Yourself

How It Works

The browser paints a solid light blue fill behind the div’s content area.

Example 2 — Hex Color Card

Use a hex value when you need an exact brand or design-system color.

hex-color.css
.card {

  background-color: #2563eb;

  color: #eff6ff;

  padding: 1.25rem;

  border-radius: 0.75rem;

}
Try It Yourself

How It Works

Hex colors are precise and widely used in real projects. Pair them with a readable text color using the color property.

🎨 Transparency & Theme Colors

Use alpha colors for overlays and CSS variables to keep theme colors consistent.

Example 3 — Semi-transparent RGBA Overlay

Create a panel that lets the background behind it show through.

rgba-overlay.css
.overlay {

  background-color: rgba(15, 23, 42, 0.55);

  color: #fff;

  padding: 1.25rem;

  border-radius: 0.85rem;

}
Try It Yourself

How It Works

The alpha value 0.55 makes the dark background 55% opaque, so the gradient behind it remains visible.

Example 4 — Theme Colors with CSS Variables

Store reusable colors once and apply them with background-color: var(...).

theme-background.css
:root {

  --surface: #ecfdf5;

  --accent: #059669;

}



.panel {

  background-color: var(--surface);

}



.badge {

  background-color: var(--accent);

}
Try It Yourself

How It Works

Changing the custom properties updates every element that references them, which makes theme updates much easier.

🧠 How background-color Works

1

You choose a color value

Write a named color, hex value, rgb(), hsl(), or CSS variable in your stylesheet.

Color value
2

The browser paints the background

The color fills the element’s background painting area behind its content.

Rendering
3

Other properties can shape it

Properties like background-clip, padding, and borders affect how much of that color you see.

Layout
=

Clear visual sections

Solid background colors help organize content and improve readability.

Universal Browser Support

background-color is one of the most widely supported CSS properties and works reliably across modern and older browsers.

Baseline · All browsers

Set background colors everywhere

Chrome, Firefox, Safari, Edge, Opera, and legacy browsers all support standard color values.

99% Universal support
Google Chrome1+ · Desktop & Mobile
Full support
Mozilla Firefox1+ · Desktop & Mobile
Full support
Apple Safari1+ · macOS & iOS
Full support
Microsoft Edge12+ · All versions
Full support
Opera3.5+ · Modern versions
Full support

Fallback behavior

Invalid color values are ignored by the browser, leaving the previous or default background in place.

💻
Very old browsers May not support newer color formats like space-separated hsl()
Rare
background-color property 99% supported

Bottom line: Use background-color confidently in any web project.

Conclusion

The background-color property is a foundational CSS tool for styling the web. It lets you add solid colors to sections, cards, buttons, and panels using simple, readable syntax.

Start with named colors and hex values, then explore rgba() for overlays and CSS variables for reusable theme colors.

💡 Best Practices

✅ Do

  • Use hex or CSS variables for brand and theme colors
  • Pair background colors with readable text colors
  • Use rgba() for overlays and subtle panels
  • Test contrast in light and dark sections
  • Keep color choices consistent across the page

❌ Don’t

  • Confuse background-color with color
  • Use low-contrast background and text combinations
  • Hard-code the same color in many unrelated selectors
  • Assume transparent means inherited
  • Forget that gradients belong in background-image

Key Takeaways

Knowledge Unlocked

Five things to remember about background-color

Use these points when styling backgrounds in your next layout.

5
Core concepts
📝02

Many Formats

Named, hex, rgb, hsl.

Values
🖼️03

transparent Default

No color unless you set one.

Default
👁04

Not color

Background vs text color.

Compare
🛠05

Theme Variables

Reuse with var().

Projects

❓ Frequently Asked Questions

The background-color property sets the solid background color behind an element's content. It fills the element's background painting area unless clipped by other background properties.
The initial value is transparent, which means no background color is painted and the parent element's background can show through.
You can use named colors, hex values, rgb(), rgba(), hsl(), hsla(), currentColor, and CSS custom properties.
background-color styles the area behind the element's content. color styles the text and other foreground content inside the element.
Yes. Use rgba() or hsla() with an alpha value, or a modern hex value with alpha such as #2563eb80.

Practice in the Live Editor

Open the HTML editor, apply background-color, and preview solid and transparent backgrounds instantly.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful