HTML Background Image Code
This article provides HTML background image code - code for setting a background image on an HTML element.
More importantly, it also provides the CSS code for a background image. The CSS method is recommended as it is more powerful, besides, the HTML method is deprecated.
CSS allows you to set a background image for any HTML element. Plus you can specify its position, whether it should repeat across the page, how it should repeat etc.
Background Image Example
Here, we apply a background image to a
Here, we set the background image to
background-repeat:no-repeat. This prevents the image repeating across the element (in the event that the image is smaller than the element). You can also use
repeat-y to make the background repeat only horizontally or vertically. For more info, see the CSS
The following code specifies a background image for the whole page (via the 'body' tag). We position the image in the center of the page and prevent it from repeating across the page.
Even better, you can use the CSS
background property to set all the background properties at once. Therefore, we could rewrite the above code to this:
Where Do I Put the Code?
To apply the background image to every page on your website, put the code into an external style sheet.
More Background Properties
Here are more background codes and usage examples.
Also, here's a full list of CSS background properties you can apply to your HTML elements:
background(shorthand for all background properties)
HTML Method (not recommended)
You may occasionally see an old web page with code similar to this:
<body background="/images/image_name.gif" bgcolor="orange">
This is the old way of specifying a background image in HTML, and is now deprecated (outdated and not recommended) by the W3C. Instead of using this method, you should use the CSS method as described above.