Background Image on a HTML element


To add a background image on an HTML element, use the HTML style attribute and the CSS background-image property:

Example

Add a background image on a HTML element:

<p style="background-image: url('cat.jpg');">

You can also specify the background image in the <style> element, in the <head> section:


Live Demo & Try it yourself! Read More » »

Background Image on a Page


If you want the entire page to have a background image, you must specify the background image on the <body> element:

Example

Add a background image for the entire page:

<style>
body {
  background-image: url('img_girl.jpg');
}
</style>


Live Demo & Try it yourself! Read More » »

Background Repeat


If the background image is smaller than the element, the image will repeat itself, horizontally and vertically, until it reaches the end of the element:


Live Demo & Try it yourself! Read More » »

Background No Repeat


To avoid the background image from repeating itself, set the background-repeat property to no-repeat.

<style>
body {
  background-image: url('cat.jpg');
  background-repeat: no-repeat;
}
</style>


Live Demo & Try it yourself! Read More » »

Background Cover


If you want the background image to cover the entire element, you can set the background-size property to cover.

Also, to make sure the entire element is always covered, set the background-attachment property to fixed:

This way, the background image will cover the entire element, with no stretching (the image will keep its original proportions):

<style>
body {
  background-image: url('cat.jpg');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
}
</style>


Live Demo & Try it yourself! Read More » »

Background Stretch


If you want the background image to stretch to fit the entire element, you can set the background-size property to 100% 100%:

Try resizing the browser window, and you will see that the image will stretch, but always cover the entire element.

<style>
body {
  background-image: url('cat.jpg');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: 100% 100%;
}
</style>


Live Demo & Try it yourself! Read More » »