Quackit Logo

FREE Hosting!

With every domain name you register with ZappyHost, you get FREE hosting.

PLUS you get:

  • FREE Website Builder
  • FREE Blog
  • FREE Starter Web Page
  • FREE Email Account
  • & much more! (Total value $123 for FREE!)
Get your FREE hosting today! >>

HTML Background Code

Print Version

This article provides HTML background code - code for setting the background properties of an HTML element.

The following examples use Cascading Style Sheets (CSS). CSS is the best way of setting the background properties in your HTML. It's more flexible than the (outdated) HTML method (more on that below). Using CSS, you can set the background properties of any HTML element. Plus you can do things like, specify an image's position, whether it should repeat, how it should repeat etc.

Background Color

You can set the background color of any HTML element using the background-color property:

<div style="background-color:red;width:200px;">
HTML background code is limited, CSS background code is much better!
</div>

This results in:

HTML background code is limited, CSS background code is much better!

Choose a color with the HTML color chart.

Background Image

You can set a background image like this:

<div style="background-image:url(/pix/smile.gif);
		background-repeat:repeat;
		width:200px;
		height:200px;">
</div>

This results in:

HTML background code is limited, CSS background code is much better!

Fixed Background Image

You can fix the position of a background image so that its position is fixed even if its containing block scrolls. You do this with the background-attachment property:

<div style="background-image:url(/pix/smile.gif);
		background-repeat:repeat;
		background-attachment:fixed;
		overflow:scroll;
		width:200px;
		height:100px;">
</div>

This results in:

HTML background code is limited, CSS background code is much better! example shows how a background image can be set to a fixed position - even though its containing block scrolls.

Shorthand Background Code

CSS provides the following properties for setting the background on HTML elements: background-attachment, background-color, background-image, background-position, background-repeat.

You can use the CSS background property to set all the background properties at once. Therefore, using the previous code example, we could rewrite it to this:

<div style="background:url(/pix/smile.gif) repeat fixed;
		overflow:scroll;
		width:200px;
		height:100px;">
HTML background code is limited, CSS background code is much better!
example shows how a background image can be set to a fixed position
- even though its containing block scrolls.</div>
HTML background code is limited, CSS background code is much better! example shows how a background image can be set to a fixed position - even though its containing block scrolls.

Backgrounds for the Whole Page

To set the background properties for the whole page, simply apply the property/properties to the body element.

<html>
<head>
</head>
<body style="background-color:#eeeeee;">
...content goes here...
</body>

Backgrounds for your Whole Website

To apply background properties to your whole website, you should place the background code into an external style sheet.

Doing this will save you from having to copy/paste the code on to multiple pages. External style sheets enable you to write the code once, then apply it to your whole site (regardless of how many pages you have).

Oh, and external style sheets are extremely easy to create!

Full List of Background Properties

Here's a list of CSS background properties you can use on your HTML elements:

HTML Background Code (deprecated)

The above examples demonstrate the best way of setting the background of your web pages - using CSS.

You may occasionally encounter an old web page where the background has been set using HTML (instead of CSS). Something like this:

<body background="/images/image_name.gif" bgcolor="orange">

This is the old way of specifying background. Until CSS came along, this was the only way to set background properties in HTML. HTML is very limited for setting backgrounds. You can only specify a background image on one tag (the body tag), and setting a background color is limted to the document's body and tables.

This method of specifying the background is deprecated (i.e. not recommended) - you should use the CSS methods above to set background properties.

Enjoy this website?

  1. Link to this page (copy/paste into your own website or blog):
  2. Add this page to your favorite social bookmarks sites:
                     
  3. Add this page to your Favorites

Oh, and thank you for supporting Quackit!