Back to HTML Notes
Topic #23

Background Images

Background images sit behind page content and can be repeated, positioned and fixed. They are a classic design tool, though CSS now handles most of it.


A background image can be specified for almost any HTML element.


Background Image on an HTML element

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

Example

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

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

Example

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

  }
</style>

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

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

  }
</style>

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:

Example

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

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

Example

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

  background-repeat: no-repeat;
}
</style>

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):

Example

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

  background-repeat: no-repeat;
  background-attachment: fixed;

  background-size: cover;

  }
</style>

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.

Example

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

  background-repeat: no-repeat;
  background-attachment: fixed;

  background-size: 100% 100%;

  }
</style>

Learn More CSS

From the examples above you have learned that background images can be styled by using the CSS background properties.

To learn more about CSS background properties, study our CSS Background Tutorial.

Want to go beyond the notes?

Join Coding Now Tech Institute's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Background Images – FAQs

Quick answers about learning Background Images in HTML.

This free note from Coding Now Tech Institute explains Background Images in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on Coding Now Tech Institute, including Background Images, is 100% free with no signup required.
With focused practice, most students grasp Background Images in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now