Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a background color in HTML

Understanding the Basics of CSS and HTML

Before we delve into the process of adding a background color in HTML, it's essential that we understand what CSS and HTML entail. HTML, or Hyper Text Markup Language, is the foundational structure of any webpage. Think of it as the skeleton of a body, providing the essential structure.

On the other hand, CSS, or Cascading Style Sheets, is the aesthetic enhancer. This is like the skin and clothing that makes the skeleton look presentable and appealing. It allows you to specify the style of text, the layout of content, and even to add appealing visuals such as colors and images.

Role of CSS in Coloring HTML

One of CSS's primary functions is to add color to your HTML elements. To put it simply, CSS is like the paintbrush in your HTML masterpiece. You can use it to color your texts, your borders, and, of course, your background.

How to Apply Background Color in HTML

To apply a background color in HTML, you will need to use a CSS property known as background-color. The background-color property is used to specify the background color of an element. An element in HTML is any part of a webpage. For example, the entire page itself is an element, a paragraph is an element, even a single character can be an element.

Here's what the code might look like:

<body style="background-color:blue;">
    <p>This is a paragraph.</p>
</body>

In this example, the body element, which represents the entire content of the document, has a background color of blue.

Breaking Down the Code

Let's break down the parts of this code.

<body>: This is the opening tag for the body element. Everything that you want to appear on your webpage will be enclosed between the opening <body> and closing </body> tags.

style: This is an HTML attribute we use to apply CSS styling directly to an HTML element. It's like a direct channel that we can use to pass in our CSS code.

"background-color:blue;": This is our CSS code. It's saying, "make the background color blue". Here "blue" is a color keyword. CSS recognizes a wide array of color keywords.

</body>: This is the closing tag for the body element. It signifies the end of the element.

Different Ways to Specify Color

In the above example, we used a simple color keyword ("blue") to specify the background color. But CSS offers more sophisticated ways to specify color.

Hex Codes

Hex codes are a popular way to specify color in web design. A hex code is a six-digit, three-byte hexadecimal number. The bytes represent the red, green, and blue components of the color.

Here's how you might use a hex code to specify a background color:

<body style="background-color:#ff0000;">
    <p>This is a paragraph.</p>
</body>

In this example, #ff0000 is the hex code for the color red.

RGB

Another way to specify color is using RGB values. RGB stands for Red, Green, and Blue. These are the three primary colors that can be combined in various ways to produce a broad array of colors.

Here's an example:

<body style="background-color:rgb(255,0,0);">
    <p>This is a paragraph.</p>
</body>

In this example, rgb(255,0,0) is the RGB value for the color red.

Conclusion

In conclusion, adding a background color in HTML is a simple process that involves the use of CSS. Remember, HTML provides the structure of your webpage, and CSS is there to make it look appealing. Whether you're using color keywords, hex codes, or RGB values, adding color to your webpage can make it more appealing and user-friendly. Now, it's time for you to experiment with different colors and find what works best for your website. Don't be afraid to get creative and try different things. Happy coding!