bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/HTML/HTML Foundations
HTML•HTML Foundations

HTML Background Images

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

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

Live preview

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

Example

Formatted code
<style>

Live preview

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

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

  }
</style>

Live preview

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

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

Live preview

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

Example

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

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

Live preview

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

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

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

  background-size: cover;

  }
</style>

Live preview

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

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

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

  background-size: 100% 100%;

  }
</style>

Live preview

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 .

Previous

HTML Image Maps

Next

HTML <picture> Element

This chapter

Overview
56

Lessons

191m

Read time

1. HTML Tutorial2. HTML Introduction3. HTML Editors4. HTML Elements5. HTML Attributes6. HTML Headings7. HTML Paragraphs8. HTML Styles9. HTML Text Formatting10. HTML Comments11. HTML Colors12. HTML RGB and RGBA Colors13. HTML HEX Colors14. HTML HSL and HSLA Colors15. HTML Styles - CSS16. HTML Links17. HTML Links - Different Colors18. HTML Links - Create Bookmarks19. HTML Images20. HTML Image Maps21. HTML Background Images22. HTML <picture> Element23. HTML Favicon24. HTML Page Title25. HTML Tables26. HTML Table Borders27. HTML Table Sizes28. HTML Table Headers29. HTML Table Padding & Spacing30. HTML Table Colspan & Rowspan31. HTML Table Styling32. HTML Table Colgroup33. HTML Lists34. HTML Unordered Lists35. HTML Ordered Lists36. HTML Other Lists37. HTML Block and Inline Elements38. HTML Div Element39. HTML class Attribute40. HTML id Attribute41. HTML Buttons42. HTML Iframes43. HTML JavaScript44. HTML File Paths45. HTML - The Head Element46. HTML Layout Elements and Techniques47. HTML Responsive Web Design48. HTML Computer Code Elements49. HTML Semantic Elements50. HTML Style Guide51. HTML Entities52. HTML Symbols53. Using Emojis in HTML54. HTML Encoding (Character Sets)55. HTML Uniform Resource Locators56. HTML Versus XHTML

On this page

Background Image on an HTML elementBackground Image on a PageBackground RepeatBackground CoverBackground StretchLearn More CSS