Dofactory.com
Dofactory.com
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML Background Images

Background images can be defined for almost any HTML element.

To add a background image, use the CSS background-image property.

Example

#

A <div> element with a background image.

<div style="background-image: url('/img/html/sunflowers.jpg');
            width:450px;height:400px;">
</div>

For more details on background-image, see our CSS background-image property reference.


Sizing Considerations

To add a background image, there are some sizing considerations:

When the image is larger than the element, the image is cropped.

When the image is smaller than the element, the image is repeated -- in the x and y direction.

Next, let's review how to create background images with CSS.


The background-image property

A <div> element with a background image defined by the CSS background-image property. The text displays on top of the background image.

This text displays on top of the background image
<div style="background-image: url('/img/html/sunflowers.jpg');">
  <div style="padding:30px;color:white;">
    This text displays on top of the background image
  </div>
</div>

Tip:  Background images can also be defined with external CSS.

The background-repeat property

By default, smaller background images are repeated horizontally and vertically.

The CSS background-repeat property adjusts how the image will repeat.

Possible values are:

  • repeat-x -- repeat horizontally,
  • repeat-y -- repeat vertically,
  • repeat -- repeat both horizontally and vertically, or
  • no-repeat -- don't repeat
This text is on top of a repeating background.
<style>
  .sunflowers {background-image:url('/img/html/sunflowers-sm.jpg');
               background-repeat:repeat; padding:30px; color:white;}
</style>

<div class="sunflowers">
  This text is on top of a repeating background.
</div>

A small background image repeated multiple times.

For more details on background-repeat, see our CSS background-repeat property reference.


The background-size property

By default, background image size is the actual size of the image.

The CSS background-size can change the background image size.

It accepts one or two values. The first value is width, the second is height.

If only width is provided, it will adjust the height according to the image ratio.

A-200 pixel background image that is not repeated.

This text is on top of the resized background image.
<style>
  .bg-size {background-image: url('/img/html/sunflowers.jpg');
            background-size:200px; background-repeat:no-repeat; 
            padding:30px; }
</style>

<div class="bg-size">
  This text is on top of the resized background image.
</div>

For more details on background-size, see our CSS background-size property reference.


Set background image to cover the element

To ensure that the background image fully covers the element use background-size:cover. This will stretch or crop the background image to cover the entire element.

This text is on top of a covered background image.
<style>
  .bg-cover {
     background-image: url('/img/html/sunflowers.jpg');
     background-size:cover; background-repeat:no-repeat;
     padding: 30px; color:white;
}
</style>

<div class="bg-cover">
  This text is on top of a covered background image.
</div>

The background-position property

Use background-position to set the position of the background image inside the hosting element.

The image can be positioned at the top, bottom, left, right, or center.

It accepts single value (if value is center), or two values: vertical and horizontal position.

A resized background image that is positioned top right.

This div has a positioned background image.
<style>
  .bg-pos {
     background-image: url('/img/html/sunflowers.jpg');
     background-repeat:no-repeat; background-size:300px;
     background-position:top right; padding:30px; }
</style>

<div class="bg-pos">
  This div has a positioned background image.
</div>

For more details on background-position, see our CSS background-position property reference.


Shorthand background property

The CSS background property is a shorthand for several background image properties.

It sets the url, repeat, position, and other properties in one operation.

A single background property sets the url, repeat, and position for the background image.

This div is styled with the background property.
<style>
  .bg-prop {
     background:url('/img/html/sunflowers.jpg') no-repeat top right;
     padding:30px; color:white; }
</style>

<div class="bg-prop">
  This div is styled with the background property.
</div>

For more details on background, see our CSS background property reference.


Did you know?

Did you know?

Creating gradient backgrounds

Interestingly, you can also create color gradients with the background-image property.
Here is an example:

This text is on top of a gradient background.
<style>
  .bg-gradient {
     background-image: linear-gradient(#339977, white);
     padding: 30px;
}
</style>

<div class="bg-gradient">
  This text is on top of a gradient background.
</div>

You may also like


Last updated on Sep 30, 2023

Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides