FREE HTML CODE
Search HTML Code

HTML Background Code

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. You can choose a background color with the HTML color chart.
Code:


Result:


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 using the background-image property.

Using the code below, you need to change /pix/smile.gif to the location of your background image. If you need a background image, check out these free background patterns.
Code:


Result:
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. Once again, you need to change /pix/smile.gif to the location of your background image.


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

You can use the CSS background property to set all the background properties at once. Therefore, you can set background-attachment, background-color, background-image, background-position, background-repeat all in one place using the background property.

Therefore, using the previous code example, we could rewrite it to this:


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!
| More