bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/HTML/HTML Foundations
HTML•HTML Foundations

HTML Background Images

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind HTML Background Images?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

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

Put the learning moves in the order that makes the concept easiest to apply.

Background Image on a Page
Background Image on an HTML element
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