css
  1. css-background-position

CSS Background Position

  • The CSS background-position property sets the starting position for a background image to be placed within an element.
  • It allows you to customize the position of the background image relative to the element.

Syntax

The syntax for the background-position property is:

background-position: x-axis y-axis;

The x-axis and y-axis values can be specified in different units such as pixels, ems, percentages, or keywords like top, center, or bottom.

Example

Here is an example that demonstrates how to use the background-position property:

div {
    background-image: url("https://static.additionalsheet.com/images//others/hills.jpg");
    background-repeat: no-repeat;
    background-position: center top;
}
Try Playground

In this example, we set the background-position to center top, which will cause the background image to be placed vertically at the top of the element and horizontally in the center.

Explanation

The background-position property specifies the starting position of the background image and it's not mandatory to provide both the values (x-axis and y-axis). If the second value is not defined, it is assumed to be center. You can provide different values of the position using different units at the same time. For example, you can use both pixels and percentages for the x-axis and y-axis, respectively.

Use

The background-position property is useful when you want to change the starting position of a background image within an element. It's particularly useful when you want to position multiple images in a single element.

Important Points

  • The background-position property's default value is 0% 0%, which means the background image starts at the top left corner of the element.
  • You can chain multiple position values together or use just one value.
  • The background-position property is inherited by child elements.

Summary

The background-position property in CSS allows you to customize the starting position of a background image within an element. It is a widely used property that can be combined with other background properties to create unique and interesting designs. By understanding the syntax and usage of this property, you can easily manipulate the positioning of background images in your website or web application.

Published on: