HTML Introduction | HTML Basic Elements | HTML background

The default background of an HTML document is white. Each HTML element has its own background.

In HTML, you can change the background in the following ways.

  1. Change the background color
  2. Change the background to an image

Changing the background color

Before HTML5, you could use the bgcolor attribute to change the background color of an HTML element to another color. However, HTML5 no longer supports the bgcolor attribute and recommends changing background colors with CSS instead.

The following example changes the background color by using CSS styles.

<style>
    body { background-color: lightgreen; }
    div { background-color: rgb(255,0,128); }
    p { background-color: #FFCCFF; }
</style>

Run code

By using the CSS background-color property, you can set the background color in more varied ways.

Changing the background to an image

By using the background attribute of an HTML element, you can set the background of that element to an image.

Syntax

<tagName background="imageUrl"></tagName>

The following example creates a background by using an image.

<body background="/resources/images/logo/96x96.png">
</body>

Run code

Using an image as the background increases the loading time of the web page. Therefore, a small image is usually made into a pattern and configured to repeat as the background image.