Background Color In Html

4 min read Jul 01, 2024
Background Color In Html

Background Color in HTML

In HTML, you can set the background color of an element using the style attribute or by using CSS. This allows you to customize the visual appearance of your web page.

Using the style Attribute

The style attribute can be added to any HTML element and allows you to directly apply CSS styles within the HTML tag. To set the background color, you can use the following syntax:

This paragraph has a light grey background.

In this example, we set the background color of a paragraph element (<p>) to a light grey color using the hex code #f0f0f0.

Using CSS

While using the style attribute is convenient for quick styling, it's generally recommended to use external CSS files for better organization and maintainability.

You can create a separate CSS file (e.g., style.css) and link it to your HTML file using the <link> tag:




  Background Color Example
  



  

This paragraph has a light blue background.

In your style.css file, you can define a CSS rule that applies the background color to the paragraph element:

p {
  background-color: lightblue;
}

This CSS rule will set the background color of all paragraph elements (<p>) to light blue.

Specifying Colors

You can use different methods to specify colors in HTML and CSS:

  • Hex Codes: A six-digit hexadecimal color code (e.g., #f0f0f0)
  • RGB Values: A combination of red, green, and blue values (e.g., rgb(240, 240, 240))
  • Color Names: Common color names (e.g., lightblue, red, green)

Setting Background Color for the Entire Page

To set the background color for the entire page, you can use the <body> tag and apply the background-color style:


  

Or, you can use CSS to style the body element:

body {
  background-color: #e0ffff;
}

Example:




  Background Color Example
  



  

Welcome to my website!

This paragraph has a light blue background.

This example demonstrates setting the background color for the entire page (body), as well as using CSS to style a heading (<h1>) with a different background color and additional styling.

Related Post


Latest Posts