Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Background Image | Decorative Effects

Background ImageBackground Image

background-image property allows us to add an image as the background of an HTML element. The basic syntax is so simple:

As a value for the background-image property, we use url(), and inside these brackets, we specify the image url.

html

index.html

css

index.css

js

index.js

background-repeat

It determines if the image will be repeated horizontally, vertically, both or not at all.

html

index.html

css

index.css

js

index.js

background-position

background-position is used to set the starting position of a background image within an element's background area. This property allows us to specify where the background image should be placed and how it should be positioned relative to the element. We have to set the position on the x and y axis

html

index.html

css

index.css

js

index.js

background-size

background-size specifies the size of an element's background image. It can be used to scale the image to fit the element, or to set a specific size for the background image.

  • auto - default value, which sets the background image to its original size;
  • cover - scales the background image to completely cover the element while maintaining its aspect ratio. This may cause some parts of the image to be cropped if the aspect ratio of the element and the image are different;
  • contain - scales the background image to fit the entire element while maintaining its aspect ratio. This may cause empty spaces to be visible around the image if the aspect ratio of the element and the image are different;
  • value - sets the width and height of the background image.

For this example, we will use the bigger picture than we need as we wish to consider the behavior difference of each value for the background-size property.

html

index.html

css

index.css

js

index.js

1. What does the `background-image` property do?
2. What does the `background-repeat` property do?
3. What property determines the position of an element's background image?

What does the background-image property do?

Select the correct answer

What does the background-repeat property do?

Select the correct answer

What property determines the position of an element's background image?

Select the correct answer

Everything was clear?

Section 5. Chapter 2

Background ImageBackground Image

background-image property allows us to add an image as the background of an HTML element. The basic syntax is so simple:

As a value for the background-image property, we use url(), and inside these brackets, we specify the image url.

html

index.html

css

index.css

js

index.js

background-repeat

It determines if the image will be repeated horizontally, vertically, both or not at all.

html

index.html

css

index.css

js

index.js

background-position

background-position is used to set the starting position of a background image within an element's background area. This property allows us to specify where the background image should be placed and how it should be positioned relative to the element. We have to set the position on the x and y axis

html

index.html

css

index.css

js

index.js

background-size

background-size specifies the size of an element's background image. It can be used to scale the image to fit the element, or to set a specific size for the background image.

  • auto - default value, which sets the background image to its original size;
  • cover - scales the background image to completely cover the element while maintaining its aspect ratio. This may cause some parts of the image to be cropped if the aspect ratio of the element and the image are different;
  • contain - scales the background image to fit the entire element while maintaining its aspect ratio. This may cause empty spaces to be visible around the image if the aspect ratio of the element and the image are different;
  • value - sets the width and height of the background image.

For this example, we will use the bigger picture than we need as we wish to consider the behavior difference of each value for the background-size property.

html

index.html

css

index.css

js

index.js

1. What does the `background-image` property do?
2. What does the `background-repeat` property do?
3. What property determines the position of an element's background image?

What does the background-image property do?

Select the correct answer

What does the background-repeat property do?

Select the correct answer

What property determines the position of an element's background image?

Select the correct answer

Everything was clear?

Section 5. Chapter 2
some-alt