askvity

How do I change the position of a background image in CSS?

Published in CSS Background Images 2 mins read

You can change the position of a background image in CSS by using the background-position property. This property allows you to specify the initial placement of the image within its container.

Understanding the background-position Property

The background-position property in CSS uses two values to determine the horizontal and vertical positioning of your background image. The first value sets the left/right position, while the second sets the up/down position.

Syntax Example:

background-position: horizontal-value vertical-value;

Position Values

Keywords:

You can use keywords to set the position:

  • top: Positions the background image at the top edge.
  • bottom: Positions the background image at the bottom edge.
  • left: Positions the background image at the left edge.
  • right: Positions the background image at the right edge.
  • center: Centers the background image along either the horizontal or vertical axis.

Numerical Values:

  • You can use specific lengths (e.g. px, em, rem) to move the image from the edges.
  • Percentage values (%) can also be used. These values are relative to the size of the element.

Examples:

Here are some practical examples of how to use the background-position property:

  • Top Left Corner:

    background-position: top left;
  • Center the Background Image:

     background-position: center center;
  • Position the Background 20px from the Top and 10px from the Right:

    background-position: 10px 20px;
  • Position 50% horizontally and 25% vertically:

    background-position: 50% 25%;

Summary

The background-position property is a flexible tool for controlling the initial position of your background images. Whether you need to align your image to a corner, center it, or position it based on specific pixel or percentage values, this property provides the control you need to achieve your design goals.

Related Articles