HTML Background Color

Background Color in HTML

In HTML, the background color is the color applied to the background of a web page or a specific element on that page. It enhances the visual appearance and readability of a website. Web developers can use background colors to set the mood, improve usability, and highlight specific sections, such as headers, footers, or navigation bars. Background colors can be defined using named colors, hexadecimal codes, or RGB values.

Importance of Choosing the Right Background Color

Choosing the right background color is essential for:

  • Visual Appeal: A vibrant or complementary background color makes content stand out, while a poorly chosen color can make the page unappealing.
  • Readability: A good contrast between text and background is crucial for readability. A light background with dark text (or vice versa) ensures content is easy to read.
  • Mood and Theme: Colors evoke different emotions. Warm colors like red and orange create excitement, while cool colors like blue and green convey calmness.

Setting Background Color in HTML

Using the bgcolor Attribute

The bgcolor attribute is used to set the background color of elements like tables and cells. However, it is deprecated in HTML5. Using CSS for styling is now recommended for better flexibility and control.

Example:

<table bgcolor="yellow">
  <tr>
    <td>Cell Content</td>
  </tr>
</table>

Inline Styling with the style Attribute

The style attribute allows adding CSS directly to an HTML element for quick and specific changes.

How to Use:

  1. Identify the element to style.
  2. Add the style attribute to the opening tag.
  3. Specify the property background-color.
  4. Assign a value (e.g., background-color: red;).
  5. Multiple properties can be added by separating them with a semicolon.

Example:

<h1 style="background-color: red;">This heading has a red background.</h1>

CSS background-color Property

Understanding CSS Properties

The background-color property in CSS specifies the background color of an element. This property is widely used to style HTML elements, such as the body, divs, or headings.

Example:

p {
  background-color: yellow;
}

Internal CSS Example

HTML Code:

<style>
  p {
    background-color: yellow;
  }
</style>
<p>This paragraph will have a yellow background.</p>

External CSS Example

CSS File (styles.css):

.container {
  background-color: blue;
}

HTML Code:

<link rel="stylesheet" href="styles.css">
<div class="container">This div will have a blue background.</div>

Ways to Specify Background Color

Using Color Names in HTML

HTML supports around 140 standardized color names, like red, green, blue, and more specific ones like skyblue and coral.

Example:

<style>
  body {
    background-color: red;
  }
  p {
    color: darkred;
  }
</style>
<p>This paragraph has dark red text on a red background.</p>

Using Hex Codes

Hexadecimal color codes offer a wide range of color options using combinations of six digits (0-9, A-F).

Example:

div {
  background-color: #FF5733;
}

Using RGB Values

RGB values are specified as rgb(red, green, blue), where each color channel has a range from 0 to 255.

Example:

div {
  background-color: rgb(0, 128, 0);
}

For opacity, use rgba(red, green, blue, alpha), where alpha is between 0 (transparent) and 1 (opaque).

Example:

div {
  background-color: rgba(0, 128, 0, 0.5);
}

Using HSL Values

HSL stands for Hue, Saturation, and Lightness. The hsl() function is used in CSS for more precise color control.

Example:

div {
  background-color: hsl(240, 100%, 50%);
}

To add opacity, use hsla(hue, saturation, lightness, alpha).

Example:

div {
  background-color: hsla(240, 100%, 50%, 0.5);
}

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate