Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to set background color in HTML

Understanding the Basics of Coloring in HTML

HTML, or HyperText Markup Language, is a cornerstone of web development. It's the language that allows us to structure our web content and make it meaningful. One of the elements of a webpage we can manipulate using HTML is the background color.

The background color of your webpage sets the tone, mood, and overall look and feel of your website. Imagine your web page as a canvas; the background color serves as the base upon which all other elements are painted.

The 'background-color' Property

To set the background color in HTML, we use a property called 'background-color'. This property is part of CSS (Cascading Style Sheets), a language used for describing the look and formatting of a document written in HTML.

Imagine you are painting a room. The 'background-color' property is like the paint you choose for your walls. It will cover the entire area and set the tone for the rest of your decorations.

Here's how we write it in code:

body {
    background-color: lightblue;

This code tells the browser to paint the background of the webpage with a light blue color.

Using Color Names

There are 140 standard color names that are recognized by most browsers. These color names are intuitive and easy to remember.

For example:

body {
    background-color: PeachPuff;

In this case, the webpage background would take on a light orange or peach color.

Using Hexadecimal Color Values

Apart from using color names, we can also use hexadecimal color values to set the background color. Hexadecimal colors are a six-digit combination of numbers and letters defined by its mix of red, green and blue (RGB). Each color goes from 00 to FF (in hexadecimal).

Think of it as a color mixer where you have three primary colors (red, green, blue) and by combining them in different proportions you get different colors.

Here's an example:

body {
    background-color: #FF5733;

This color is a vibrant shade of orange.

Using RGB

RGB stands for Red Green Blue, which are known as primary colors. With these three, you can create any color on the spectrum.

The RGB value is a combination of three numbers between 0 and 255, one for each primary color. Think of it as three different light sources in a room: one red, one green, and one blue. The final color is a combination of these three lights.

Here's how we can set background color using RGB in our HTML:

body {
    background-color: rgb(255, 99, 71);

In this case, the color would again be a kind of orange, as it's the same RGB values translated from our previous hexadecimal example.

Using RGBA

RGBA is an extension of RGB with an 'A', which stands for Alpha. The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

This can be thought of as a thin curtain over our window: the higher the value, the less you can see through it, and vice-versa.

Here's its usage:

body {
    background-color: rgba(255, 99, 71, 0.5);

This code will create a semi-transparent background color.

Final Thoughts

Setting up a background color in HTML is like setting up your canvas before you start painting. It's a straightforward task, but it offers a world of possibilities. By understanding and using different color values like color names, hexadecimal, RGB, and RGBA, you can create a unique and appealing aesthetic for your webpages.

Remember, colors have a profound impact on the overall user experience, so choose them wisely. Happy coding!