HTMLs Tutorial

HTML Tutorial HTML Tags HTML Basic Tags HTML Attributes HTML Elements HTML Formatting HTML Text Format HTML body tag HTML samp tag HTML script Tag HTML section tag HTML select tag HTML source tag HTML span tag HTML strike tag HTML strong tag HTML style tag HTML sub tag HTML summary tag HTML sup Tag HTML svg tag HTML table tag HTML u tag HTML Headings HTML Paragraphs HTML wbr tag HTML Anchor HTML Image HTML Lists HTML Ordered List HTML Unordered List HTML Form HTML Form input HTML with CSS HTML Layouts HTML References HTML Frames HTML Links Fieldset Tag in HTML Basic HTML Tags Br Tag in HTML Free HTML Templates How to Create a Table in HTML HTML Calendar HTML Card HTML Cellspacing HTML Center Image HTML Checkbox Read-only HTML Cleaner HTML Code for a Tab HTML Comment HTML Compiler HTML Nested Forms HTML Overlay Text on the Image HTML Select Option Default HTML Snake Game HTML Subheader HTML Tab Character dd Tag in HTML How Many HTML Tags are There HTML Align Tag HTML Responsive HTML Tab Code HTML Table Alternate Row Color HTML Table Fix Column Width Contact HTML DL Tag in HTML How to Insert Image in HTML HTML Background Color HTML Dark Mode How to Convert HTML to PNG HTML Data Toggle HTML Email Template HTML Font Color HTML Font Family ID and Class in HTML HTML Tab Space HTML Tab Tag HTML Itemprop HTML Itemscope HTML Form Design HTML Input Only Numbers HTML Textarea HTML to JPG HTML to Markdown Python li Tag in HTML MDN HTML What is the Correct HTML for Making a Hyperlink? What is the Root Element of an HTML Document How to Make a Box in HTML How to Save HTML Files in Notepad How to Align Text in HTML How to Change Font Color in HTML? How to Change Font Size in HTML How to Change Image Size in HTML How to Create a HTML Page How to Create a Link in HTML File? How to Create an HTML File? HR Tag in HTML HTML Base Tag HTML Default Attribute HTML Hyperlink HTML Indent HTML Injection Payloads HTML Input Numbers Only HTML Roadmap HTML Row Height HTML Schedule HTML Space HTML Tab HTML vs HTTP HTML5 API HTML5 Video HTML Collection to Array Text Area in HTML

HTML5 Advance

HTML5 Tutorial HTML5 Tags HTML Button Tag HTML canvas Tag HTML caption Tag HTML City tag HTML Tag HTML5 SVG HTML Event Attribute HTML5 Audio HTML5 Youtube HTML5 Button Tag HTML5 Tags

Misc

How to add JavaScript to HTML How to change font in HTML How to change text color in HTML HTML Date HTML Hide Element HTML Nested Table HTML Reset Button What does HTML stand for? HTML Background Image HTML Tag Div Tag in HTML How to insert Image in HTML How to create a link with no underline in HTML How to insert spacestabs in text using HTMLCSS HTML tag HTML Code HTML Tag HTML Canvas Design a tribute page using HTML and CSS What is a Container tag Font tag in HTML Difference between HTML and DHTML Empty Tag in HTML HTML Button Link Html Line Break Img src HTML Nested List in HTML Placeholder in HTML TD in HTML HTML Space Code HTML Target Attribute HTML Tag Markup Meaning in HTML Border-Collapse in HTML HTML Onclick Online HTML Compiler Convert HTML to PDF HTML Formatter HTML5 - Web Storage HTTP – Responses Container Tag in HTML DL Tag in HTML Horizontal Rule HTML HTML Tab Text Html Table Cell Background Color HTML Table Cell Color HTML Col Width How Many HTML Tags are There Convert String to Unicode Characters in Python HTML Runner HTML Style Attribute HTML Superscript Attribute HTML tabindex Marquee Tag in HTML HTML Dynamic Form HTML side Tag HTML Pattern Attribute HTML q Tag HTML Readonly Base 64 Encoding in HTML Documents Enhancing Data Portability and Security Evo Cam Web Cam HTML Free code camp HTML CSS How to Add a JS File in HTML? How to Add Picture in HTML How to Add the Logo in HTML? How to Add Video in HTML HTML Class Attribute HTML Entities HTML Form Elements HTML Form Templates HTML Marquee Tag HTML Radio Buttons HTML Text box HTML to JSX HTML Tooltip Basic HTML Codes How to Align Image Center in HTML HTML Header Tag HTML Image Tag HTML Next Line

HTML Background Color

Introduction

In HTML, the color visible behind the webpage's content is referred to as the background color. CSS can be applied to alter it, and there are several techniques for changing the background color. Here, the string of color values is given, which can be a name, RGB, HSL, or hexadecimal code, to define the background color property of a CSS rule. To achieve the desired HTML element, apply this rule.

Background Color using Hex Color Codes

It is very easy to color the background of a webpage. The first and most common method is to use the background-color property with a Hex color code. In this instance, we apply a Hex color straight to the HTML element using the style tag.

Nearly any HTML element can be styled using this same technique; however, note that the behavior of the components may vary depending on whether they are block-level or inline. To find a color's Hex code, use our color picker or color chart.

HTML

<body style="background-color:#FF0000;">

</body>

Background Color using HTML Color Names

HTML element coloring is possible with various methods. The most common of them are HEX color codes. Furthermore, you can also utilize an HTML color name by substituting the HEX code with one of the 140 color names available, and you are good to go.

HTML

<body style="background-color:red;">

</body>

Background Color using HSL Color Values

Many contemporary browsers now support HSL values, which are less common but just as effective. See Wikipedia for an overview of the reasons for HSL's awesomeness, which stands for Hue, Saturation, and Lightness. Use the same syntax as for the RGB values above, using the hsl() prefix if you only want to utilize them as an HTML background color.

Alpha channel control is also available for HSL, just like it is for their RGB counterparts. Create a fourth value between 0 and 1 and insert it using the hsla() prefix.

HTML

<body style="background-color:hsl(0,100%,50%);">

</body>

HTML

<body style="background-color:hsla(0,100%,50%,0.5);">

</body>

Background Color using RGB Color Values

HTML elements can also have a background color added by using RGB values. The HEX code or color name should be replaced with an appropriately structured RGB value using the same style element as previously (be sure to contain it in parentheses and prefix it with a lowercase 'rgb'). The RGB value implies the brightness of red, green, and blue colors.

HTML

<body style="background-color:rgb(255,0,0);">

</body>

You also have the option to specify the opacity level when utilizing RGB values in HTML. You can add a fourth value between 0 and 1, with 0 representing entire transparency and 1 representing complete opaqueness, using the prefix rgba(). Decimal values should be used for levels in between. The 'a' in this prefix denotes alpha, the channel that regulates transparency.

HTML

<body style="background-color:rgba(255,0,0,0.5);">

</body>

How to Set the Background Color in HTML?

We can build unique layouts for websites by changing the background color of a page or an element on the page.

Use the style attribute in conjunction with the CSS property background color inside the HTML document's body tag to set the background color in HTML.

Since HTML5 does not support the tag's bgcolor attribute, the background color is added using CSS style. The bgcolor property was deprecated in HTML5.

Using the other property to override the property, we achieve that change of background color.

Syntax

<body style="background-color:aquamarine;">

Example

Below is a program that gets the background color for an HTML page.

<html>

<body style="background-color:aquamarine;">

   <h1>HTML Articles</h1>

</body>

</html>

By replacing the property with a different color, we can alter the background's color.

Example

Below is a program that gets the background color for an HTML page.

<html>

<body style="background-color:khaki;">

   <h1>HTML Articles</h1>

</body>

</html>

We can fix the background color based on the RGB value. The RGB value shows the power of red, green, and blue colors. Every intensity value ranges from 00 to FF in the hexadecimal number system or from 0 to 255 on the scale.

Example      

The program that follows is an example of how to set the background color using the RGB values.

<html>

<head>

   <style>

      body {

         background-color: rgb(20,30, 1);

      }

   </style>

   <h1>HTML Articles</h1>

</head>

<body>

</body>

</html>

How to Change the Background Color in HTML?

There are some methods to change the background color of a document:

  • Changing Background Color using the bgcolor attribute
  • Changing Background Color using an Inline CSS
  • Changing Background Color using Internal CSS
  • Changing Background Color using External CSS

Changing Background Color using the bgcolor Attribute

With this method, background color changes can be made quickly because the bgcolor attribute is used directly in HTML components. Although HTML5 deprecates the bgcolor attribute, it is still maintained for compatibility. It adjusts an element's backdrop color.

Changing Background Color using bgcolor attribute Syntax:

bgcolor="color_name"

The bgcolor attribute is used to set the body's background color to light green in this instance.

Changing Background Color using bgcolor attribute Example:

<html>

    <head>

        <title>Background Change using bgcolor attribute Example</title>

    </head>

    <body bgcolor="pink">

        <h1>

            Hello reader

            Welcome

        </h1>

    </body>

    </html>

Changing Background Color using bgcolor attribute Example Explanation:

  • The structure of the webpage is specified in the HTML file.
  • Using the bgcolor attribute found in the <body> tag, set it to pink.

Changing Background Color using Inline CSS

Here, we are altering an HTML element's background color using inline CSS. Applying the style attribute directly within the HTML tag and giving the background-color property is how this is accomplished.

Changing Background Color using Inline CSS Syntax:

< tag style="background-color: color name;">..</tag>

Changing Background Color using Inline CSS Example:

Inline CSS is being used here. Inline CSS uses the style property contained in the HTML tag to apply styles to specific HTML elements directly.

<html>

            <head>

                        <title>Background Change using Inline CSS Example</title>

            </head>

            <body style="background-color: yellow;">

                        <h1>Welcome to our website</h1>

                        <h2>We are using the Inline CSS</h2>

            </body>

            </html>

Changing Background Color using Inline CSS Example Explanation:

  • With the <html>, <head>, <title>, <body>, <h1>, and <h2> components, we are generating a basic HTML document in this instance.
  • The <body> tag has the background color "yellow" added to it directly using the style attribute.

Changing Background Color using Internal CSS

The <style> tag in the <head> area of an HTML document is where we apply styling rules, such as changing the background color to elements. In this case, we are using internal CSS.

Changing Background Color using Internal CSS Syntax:

<style>

    Body {

        background-color: green-yellow;

    }

</style>

Changing Background Color using Internal CSS Example:

Here, internal CSS is used in the section to set the background color to pink.

<html>

    <head>

        <title>Internal CSS</title>

        <style>

            Body {

                background-color: pink;

            }

        </style>

    </head>

    <body>

        <h1>Welcome to our website</h1>

        <h2>We are using the Internal CSS</h2>

    </body>

    </html>

Changing Background Color using Internal CSS Explanation:

  • With the <html>, <head>, <title>, <style>, and <body> elements, we are generating a basic HTML document here.
  • Style is applied inside the <style> tag with the <body> element as the target for the background color. 

Changing Background Color using External CSS

Here, External CSS is being used, where style guidelines are specified in a different CSS file and connected to HTML content through the element. All stylistic information, including background color options, is contained in this file.

Changing Background Color using External CSS Syntax:

body{

    background-color: yellow;

};

Changing Background Color using External CSS Example:

In this example, an HTML document is linked to a second CSS file containing stylistic rules using the '<link>' tag, which is created using External CSS. The background color to be used with our HTML element is contained in this file.

CSS:

body{

            background-color: yellow;

            }

HTML:

<html lang="en">

            <head>

                        <title>inline style attribute</title>

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

            </head>

            <body>

                        <h1>Welcome to our website</h1>

                        <h2>We are using the External CSS</h2>

            </body>

            </html>

Changing Background Color using External CSS Example Explanation:

  • Here, the <html>, <head>, <title>, <link>, and <body> tags form the structure of the document.
  • To add more styling, use the External CSS Link to connect the HTML file to the "style.css" external stylesheet.
  • We aim to get our desired background color of body elements by using this external style sheet.

Conclusion

In conclusion, HTML offers various mechanisms for manipulating background colors, such as Hex codes, HTML colors, HSL, and RGB values. On the other hand, deprecated attributes like 'bgcolor' are still supported for compatibility reasons, but the modern approach involves CSS styles.

Inline, internal, and external CSS styles vary to style web page elements, and because of this, the process is easier and more maintainable with changing background colors. Through the utilization of these methods, developers can make the website more attractive and easy to use, and it must meet the current web standards and technologies.