Front-End Development Mastering Image Alignment: Centering Images with HTML & CSS Adding Video to Your React Native App with react-native-video HTML Image Slider: Do It Yourself and 1-Step Image Gallery Widget How to Effectively Manage Digital Assets in a PHP Image Gallery Introducing Angular Image Editor: Your New Editing Too Mastering Javascript Image Annotation Mastering JavaScript Image Popup Python Video Player: 3 Free Options and a Quick Tutorial Image Recognition Machine Learning: Use Cases and Common Algorithms HTML/CSS: How to Center Images Vertically and Horizontally How to Create an Image Map Understand CSS Background Position with 4 Simple Examples Java for Image Processing: 4 Libraries You Should Know Python Video Processing: 6 Useful Libraries and a Quick Tutorial Blur Image CSS: Two Ways to Blur Images for Gorgeous Effects Designing a Video Flipping App for Android Build an App for Embedding Video Watermarks on Android Devices Change Image on Hover with HTML and CSS How to Align Images with CSS Full Page Background Image with CSS: Tutorial and 5 Automation Tips Using CSS to Scale Page Elements and a Better Way to Scale Your Images CSS Background Image: Quick Tutorial and 3 Automation Tips Featured Image: Best Practices to Feature Images on Your Website Image Gallery Websites: Tips and Tricks for a Stunning Image Gallery 6 Ways to Stretch a Background Image with CSS Auto Cropping for Images and Video: Features & Best Practices FLAC vs. WAV: 4 Key Differences and How to Choose Converting Audio to Video: A Practical Guide FLAC vs. AIFF: 5 Key Differences and How to Choose FLAC vs. MQA: 5 Key Differences and How to Choose Converting WAV Files To OGG The Ultimate Guide On Converting OGG Files To WAV Sound Choices: FLAC vs. MP3 AAC vs MP3 – The Future of Audio Files All about AIFF and how it compares to WAV and MP3 Integrating Cloudinary with Netlify Integrating Cloudinary with Svelte and SvelteKit Integrating Cloudinary with Nuxt Integrating Cloudinary with Gatsby File Upload as a Service: How It Works and 5 Leading Solutions Native Mobile App Development Creative Uses for CSS Inner Border and 3 Ways to Set a Border Integrating Cloudinary with Next.js Front-End Development: The Complete Guide

Understand CSS Background Position with 4 Simple Examples

css background position

What Is the CSS Background Position Property?

The CSS background position property allows you to position a background image at any position within an element. This property helps create attractive web designs by allowing you to control exactly where a background image is positioned.

The CSS background position property is particularly useful when designing responsive websites. It allows you to adjust the positioning of background images based on the size of the viewport, ensuring that your design remains visually appealing and functional across a variety of screen sizes.

The background-position property in CSS accepts both percentage and length values, as well as keyword values like “top,” “center,” and “bottom.” You can achieve a wide range of positioning effects by combining these values in different ways.

This is part of a series of articles about CSS Image

In this article:

css background position

Importance of Controlling Background Image Position

Controlling the position of a background image is crucial for creating visually appealing and responsive designs. Properly positioned background images can enhance the user experience by ensuring key elements are always in view and maintaining the site’s overall aesthetic. Here are some key reasons why mastering background image positioning is essential:

Enhanced Visual Appeal

Controlling the background image position can significantly enhance the visual appeal of your web design. By strategically positioning your background images, you can draw attention to key elements of your design, create engaging visual patterns, and even guide your users’ eyes around the page. This can make your design more engaging and visually appealing.

Responsive Design Considerations

The background position property plays a crucial role in responsive design. It ensures that your background images always look their best, regardless of your user’s device or screen size. You can specify different background property values for different devices or screen sizes; one way to do this is via media queries.

Learn more in our detailed guide to responsive image techniques

Effective Use of Image Sprites

Image sprites are a technique for combining multiple images into a single image file, which can significantly reduce the number of HTTP requests your site makes and improve page load times. By using background-position, you can control which part of the sprite is visible within an element, allowing you to display different images from the sprite in other parts of your design.

Using the Background Position Property

The syntax for the CSS background position property is relatively straightforward. The property accepts one or two values, which specify the position of the background image along the x-axis (horizontal) and y-axis (vertical), respectively, like this:

 	background-position: 50px 100px;

These values can be specified as percentages, lengths, or keywords. There are nine possible keywords: top, bottom, left, right, center, top left, top right, bottom left, and bottom right. These keywords allow you to position the background image relative to the edges of the element.

Two-value vs. Four-value Syntax

The CSS background position property can be specified using a two or four-value syntax. The two-value syntax, shown above, is simpler and more commonly used. It allows you to determine the horizontal and vertical positions of the background image using two values.

The four-value syntax looks like this:

background-position: right 20px bottom 10px;

This provides more control over the positioning of the background image. It allows you to specify the position of the background image relative to any corner of the element, as well as the offset from that corner. This makes it possible to position the background image at any point within the element.

Examples of the CSS Background Position Property

The CSS background-position property works with other CSS properties like background-image and background-repeat. Let’s look at examples of each of these. If you want to follow along with the same image, you can download image.jpg here.

Length Values Example

You can use this property with length values. These can be in pixels, ems, or CSS length measurements. The first value specifies the horizontal position, and the second specifies the vertical position.

body {
  background-image: url('image.jpg');
  background-repeat: no-repeat;
  background-position: 50px 100px;
}

css background position

In this example, the background image.jpg is positioned 50 pixels from the left and 100 pixels from the top of the body element. This is useful when you want to position a background image at a specific distance from an element’s top, right, bottom, or left edges.

Percentages Example

Like length values, the first percentage refers to the horizontal position, and the second refers to the vertical position.

body {
  background-image: url('image.jpg');
  background-repeat: no-repeat;
  background-position: 50% 25%;
}

css background position

In this example, the background image.jpg is positioned at 50% from the left and 25% from the top of the body element. This means that the point in the image that is 50% across and 25% down is placed at the point in the element that is 50% across and 25% down.

Keywords Example

Here is an example showing the use of keywords to position the background relative to the edges of the element:

body {
  background-image: url('image.jpg');
  background-repeat: no-repeat;
  background-position: top right;
}

css background position

In this example, the background image.jpg is positioned at the top right of the body element. This is a simple and straightforward way to position a background image, especially if you want it aligned with the edges of an element.

Four-Value Syntax Example

The four-value syntax allows you to position a background image away from a certain edge using a length or percentage.

body {
  background-image: url('image.jpg');
  background-repeat: no-repeat;
  background-position: right 20px bottom 10px;
}

css background position

Assuming you have a paragraph element <p>, using the above CSS, the background image will be placed 20px from the right margin, and it will be placed 10px below the height of <p> element.

Related content: Read our guide to CSS image effect

Generating Responsive Image Backgrounds With Cloudinary

A cloud-based Image and Video API, Cloudinary offers a generous free-forever subscription plan. While on that platform, you can upload images and apply built-in effects, filters, and modifications. You can also create image effects that are difficult or impossible to produce with just CSS.

Cloudinary makes it simple to deliver responsive images by:

Last updated: Jun 2, 2024