Tillitsdone
down Scroll to discover

CSS Background-Color Enhance Your Web Design

Learn how to use CSS background-color to enhance your web design.

Discover available options like named colors, hexadecimal values, RGB, RGBA, HSL, HSLA, and more.
thumbnail

Introduction

The background-color CSS property is a powerful tool for web development and design. It sets the background color of an element, enhancing the visual appeal and readability of your webpage. You can use named colors, hexadecimal values, RGB, RGBA, HSL, and HSLA to define this property.

This property is great for highlighting content or improving the overall look of your website. Whether you’re working on a blog post or a complex web app, background-color gives you the flexibility to style both specific elements and the entire page.

In this guide, we’ll explore the syntax, values, and accessibility considerations of the background-color property. We’ll also provide examples and browser compatibility information. By the end, you’ll know how to effectively use the background-color property in your web development projects.

Syntax

The background-color property is easy to use. Here’s the basic syntax:

element {
background-color: color | transparent | initial | inherit;
}

Explanation

  • color: Can be a named color (e.g., red), a hexadecimal value (e.g., #bbff00), an RGB value (e.g., rgb(255 255 128)), an RGBA value (e.g., rgba(255 255 128 / 0.5)), an HSL value (e.g., hsl(50 33% 25%)), or an HSLA value (e.g., hsla(50 33% 25% / 0.75)).
  • transparent: Makes the background fully transparent.
  • initial: Resets the property to its default value, which is transparent.
  • inherit: Inherits the background color from the parent element.

Examples

Here are some examples of how to use the background-color property:

/* Using a named color */
background-color: red;
/* Using a hexadecimal value */
background-color: #bbff00;
/* Using an RGB value */
background-color: rgb(255 255 128);
/* Using an RGBA value with transparency */
background-color: rgba(255 255 128 / 0.5);
/* Using an HSL value */
background-color: hsl(50 33% 25%);
/* Using an HSLA value with transparency */
background-color: hsla(50 33% 25% / 0.75);
/* Setting the background to be transparent */
background-color: transparent;
/* Resetting to the default value */
background-color: initial;
/* Inheriting the background color from the parent element */
background-color: inherit;

Values

The background-color property accepts various values:

  1. Named Colors: Predefined color names like red or blue.
  2. Hexadecimal Values: Six-digit (e.g., #bbff00) or shorthand three-digit codes (e.g., #bf0).
  3. RGB Values: Red, green, blue components (e.g., rgb(255, 255, 128)).
  4. RGBA Values: RGB with alpha transparency (e.g., rgba(255, 255, 128, 0.5)).
  5. HSL Values: Hue, saturation, lightness components (e.g., hsl(50, 33%, 25%)).
  6. HSLA Values: HSL with alpha transparency (e.g., hsla(50, 33%, 25%, 0.75)).
  7. Special Keywords: currentcolor, transparent.
  8. Global Values: inherit, initial, revert, revert-layer, unset.

Accessibility

When using background-color, it’s important to consider accessibility. Ensure there’s enough contrast between the background color and the text for readability. A contrast ratio of 4.5:1 is needed for text, and 3:1 for larger text.

Tools for Checking Color Contrast

  • WebAIM Color Contrast Checker: Input background and text colors to check the contrast ratio.
  • MDN Understanding WCAG, Guideline 1.4 Explanations: Detailed explanations of WCAG guidelines.
  • Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0: Explains the importance of color contrast.

Tips for Ensuring Accessibility

  • Use high contrast colors (e.g., dark text on light background or light text on dark background).
  • Avoid low contrast combinations.
  • Test with real content to ensure readability.

Example

/* High contrast background and text colors */
.accessible-background {
background-color: #ffffff; /* White background */
color: #000000; /* Black text */
}
/* Larger text with sufficient contrast */
.accessible-heading {
background-color: #000000; /* Black background */
color: #ffffff; /* White text */
font-size: 24px; /* Larger text size */
}

Formal Definition

Initial Value: transparent Applies To: All HTML elements and ::first-letter and ::first-line pseudo-elements. Inherited: No Computed Value: A computed color Animation Type: Color

Examples

Example 1: Colorize Boxes

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: transparent;
}
.exampletwo {
background-color: rgb(153 102 153);
color: rgb(255 255 204);
}
.examplethree {
background-color: #777799;
color: #ffffff;
}

Example 2: Colorize Tables

HTML
<table>
<tr id="r1">
<td id="c11">11</td>
<td id="c12">12</td>
<td id="c13">13</td>
</tr>
<tr id="r2">
<td id="c21">21</td>
<td id="c22">22</td>
<td id="c23">23</td>
</tr>
<tr id="r3">
<td id="c31">31</td>
<td id="c32">32</td>
<td id="c33">33</td>
</tr>
</table>
CSS
#r1 {
background-color: #ffcccc;
}
#r2 {
background-color: #ccccff;
}
#r3 {
background-color: #ccffcc;
}
#c11 {
background-color: #ffe6e6;
}
#c22 {
background-color: #e6e6ff;
}
#c33 {
background-color: #e6ffe6;
}

Example 3: Background Color with Transparency

HTML
<div class="transparent-background">Lorem ipsum dolor sit amet, consectetuer</div>
CSS
.transparent-background {
background-color: rgba(0, 0, 0, 0.5); /* 50% transparent black background */
color: white;
padding: 20px;
}

Example 4: Inheriting Background Color

HTML
<div class="parent">
<div class="child">Lorem ipsum dolor sit amet, consectetuer</div>
</div>
CSS
.parent {
background-color: lightblue;
padding: 20px;
}
.child {
background-color: inherit; /* Inherits the background color from the parent element */
padding: 10px;
}

Browser Compatibility

The background-color property is widely supported across all major browsers:

  • Google Chrome: Supported since version 1.0 (Dec 2008).
  • Mozilla Firefox: Supported since version 1.0 (Nov 2004).
  • Microsoft Edge/Internet Explorer: Supported since version 4.0 (Sep 1997).
  • Opera: Supported since version 3.5 (Nov 1998).
  • Safari: Supported since version 1.0 (Jun 2003).

Ensuring Compatibility

To ensure consistent appearance across different browsers:

  1. Use Standard Values: Stick to standard color values like named colors, hexadecimal codes, RGB, and HSL values.
  2. Test Across Browsers: Regularly test your web pages in different browsers.
  3. Fallback Options: Provide fallback options for older browsers.
  4. Use Validation Tools: Utilize CSS validation tools to check your code.

Additional Resources

For further reading and to deepen your understanding of CSS properties related to backgrounds and colors:

  • Multiple backgrounds: Learn how to use multiple background images and colors on a single element.
  • The <color> data type: Explore the various ways to define colors in CSS.
  • Other color-related properties:

By exploring these resources, you can gain a deeper understanding of how to use the background-color property and other related CSS properties to create visually appealing and accessible web designs.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.