HTML Background Color

HTML Background Color

Background colors can enhance the overall interactivity of a webpage by making it visually appealing. The Background Color can be referred to as implementing the color to the background of any webpage or application. In this blog, we will look at how we can add the correct HTML and CSS to add a background color.

Table of Contents:

Background Color in HTML

The Background color defines the color applied to the background of the website or application with the help of the background-color property in CSS. This CSS property sets the color of the element.

Syntax:

<element style="background-color: colorname;"> Content </element>

Methods to Add Background Color in HTML

Adding background color is easy by using HTML and CSS. Here are some ways through which you can add background color to a website:

Using the bgcolor attribute

For the HTML elements, like the <body>, <table>, <td>, and <tr>, the bgcolor attribute can be used to set the background color. This attribute can take color names, hex, or RGB as its value. This attribute is deprecated and not recommended for use in HTML 5.

Example:

Html

Output:

bgcolor attribute
Design and Build Responsive Websites
Start Today
quiz-icon

Using Inline CSS:

Inline styling is used to apply the background color. Inline CSS uses a global style attribute for adding CSS inside the HTML tags. By using the “style” attribute, you can set the background color only for one element. Here is the syntax to use:

<element style="background-color: lightblue;">Content</element>

Example:

Html

Output:

background color using inline css

Here, the background-color property only applies to the div element, and not to the entire background.

Using Internal CSS

You can use internal CSS to add a background color to the website. Internal CSS uses the <style> tag inside the <head> section. It can offer you clearer and more readable syntax for writing code.

Example:

The following example illustrates the internal styling of the Background-color Property.

Html

Output:

bgcolor internal css

Using External CSS

This type of styling uses the external CSS file, which is linked to the HTML file with the help of the <link> tag inside the <head> tag. The syntax below is used to include the external CSS file.

Syntax:

<link rel="stylesheet" href="styles.css">

Example:

index.html

Html

Styles.css:

body {
font-family: Arial, Helvetica, sans-serif;
background-color: rgb(82, 131, 90);
color: white
}

Output:

bgcolor external css

Adding Color Gradient Using CSS

Gradients in CSS allow you to create a smooth transition between two or more colors. Gradients in CSS are important for creating eye-catching and beautiful backgrounds, buttons, headers, and more without using any background image. There are two main types of gradients in CSS:

Linear Gradient

A linear gradient creates a smooth transition between two or more colors along a straight line. This can be helpful in producing the gradient in various directions, such as from top to bottom, left to right, or at specific angles.

Syntax:

background: linear-gradient(direction, color1, color2, ...);

Example:

Html

Output:

linear gradient

Radial Gradient

A radial gradient is another type of gradient that radiates outward in a circular or elliptical shape from a central point. The gradient starts from the center with one color, and then gradually makes transitions to other colors and moves towards the edges.

Syntax:

background: radial-gradient(shape size at position, start-color, ..., last-color);

Example:

Html

Output:

radial gradient

Browser Compatibility

The background-color CSS property is fully supported by all modern browsers. Here is the list of all the browsers that support the background-color property:

  • Google Chrome
  • Mozilla Firefox
  • Safari
  • Microsoft Edge
  • Opera Mini
  • Internet Explorer (From IE4)

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

The background-color property in CSS is an important and useful tool that helps in making a webpage more interactive. It allows developers to add color to the background of any HTML elements. You can use solid colors, transparent shades, or create linear and radial gradients. The background-color attribute is mostly used by developers to set the background colors of the website. You can use different color formats like color names, hex codes, RGB, HSL, or even CSS variables to set the background color of your website.

CSS Background Color – FAQ’s

Q1. What is the shorthand property used for the CSS background-color property?

The background shorthand property can be used to set single or multiple background properties, including the background-color property

Q2. Is it possible to inherit the background-color property?

No, by default, the background-color property is not inherited, however, it can be set to inherit by applying it to child elements using the CSS Selectors.

Q3. How to implement the CSS variables with the background-color property?

CSS variables can be utilized to define the reusable colors for the CSS background-color property.

Q4. Is it possible to combine the CSS background-color property with other background properties?

Yes, you can use the background shorthand property to combine the background-color property with other background properties, such as the background-image property.

Q5. What is a color code?

Color code is defined as the system that uses specific colors to represent information and convey meaning.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner