How to Position a Background Image in CSS

CSS, or Cascading Style Sheets, is a powerful tool for web developers to control the layout and presentation of web pages.

One of the many features of CSS is the ability to add background images to elements on a web page.

In this CSS tutorial, we will go over the different ways to position a background image in CSS and provide code examples for each method.


Introduction

When adding a background image to an element, it is important to consider how the image should be positioned within the element.

The position of the background image can greatly affect the overall design and layout of a web page.

In this tutorial, we will go over the different ways to position a background image in CSS and provide code examples for each method.

Background-Position Property

The most common way to position a background image in CSS is by using the background-position property.

This property takes two values, one for the horizontal position and one for the vertical position.

The values can be specified in pixels, percentages, or keywords.

For example, to position a background image in the top left corner of an element, the following code can be used:

.example {
background-image: url(example.jpg);
background-position: 0 0;
}

In this example, the background image is positioned at 0 pixels from the left and 0 pixels from the top.

Another example:

.example {
background-image: url(example.jpg);
background-position: 50% 50%;
}

In this example, the background image is positioned at 50% from the left and 50% from the top.

Background-Repeat Property

The background-repeat property is used to specify whether a background image should be repeated or not.

The default value for this property is “repeat” which means the background image will repeat both horizontally and vertically.

The other possible values for this property are “repeat-x”, “repeat-y”, and “no-repeat”.

For example, to repeat a background image only horizontally:

.example {
background-image: url(example.jpg);
background-repeat: repeat-x;
}

In this example, the background image will repeat horizontally but will not repeat vertically.

Background-Size Property

The background-size property is used to specify the size of a background image.

The values for this property can be specified in pixels, percentages, or keywords.

For example, to make a background image cover the entire element:

.example {
background-image: url(example.jpg);
background-size: cover;
}

In this example, the background image will be resized to cover the entire element.

Shorthand Property

Instead of using multiple properties to position a background image, you can use the shorthand property background to set all the values at once.

The background property takes the values for background-color, background-image, background-repeat, background-attachment, and background-position.

For example, to set all the background properties at once:

.example {
background: url(example.jpg) no-repeat center center;
}

In this example, the background image will not repeat and will be positioned in the center of the element.


Conclusion

In this article, we have gone over the different ways to position a background image in CSS.