Background position lets you choose where a background image appears inside an element. It helps you place the image exactly where you want it.
Background position in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
background-position: horizontal vertical;Horizontal and vertical can be keywords (like left, center, right, top, bottom) or length values (like 10px, 2rem) or percentages (like 50%).
If you use one value, the second defaults to center.
background-position: center center;background-position: top right;background-position: 20px 30px;
background-position: 75% 50%;
This example shows a box with a small background image placed at the bottom right corner using background-position: bottom right;. The box has a border so you can see the edges clearly.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Background Position Example</title> <style> .box { width: 300px; height: 200px; border: 2px solid #333; background-image: url('https://via.placeholder.com/100'); background-repeat: no-repeat; background-position: bottom right; } </style> </head> <body> <main> <section> <h1>Background Position Demo</h1> <div class="box" aria-label="Box with background image positioned at bottom right"></div> <p>The small image is placed at the bottom right corner inside the box.</p> </section> </main> </body> </html>
Using keywords like center, top, left is easier for common positions.
Percentages move the image relative to the element's size, which helps with responsive design.
Remember to set background-repeat: no-repeat; if you want only one image and no tiling.
Background position controls where a background image sits inside an element.
You can use keywords, lengths, or percentages to set horizontal and vertical positions.
It helps you create neat, visually balanced designs by placing images exactly where you want.
Practice
background-position control in a webpage?Solution
Step 1: Understand the property purpose
Thebackground-positionproperty sets the location of the background image inside an element.Step 2: Compare with other properties
Other properties likebackground-sizecontrol size, andbackground-colorcontrols color, not position.Final Answer:
Where the background image is placed inside an element -> Option BQuick Check:
Background position = image placement [OK]
- Confusing background-position with background-size
- Thinking it changes background color
- Assuming it controls element borders
Solution
Step 1: Recall keyword order for background-position
The correct order is horizontal first, then vertical. Sotop rightis incorrect order.Step 2: Check options carefully
background-position: top left; usestop left, which is vertical then horizontal, so it is incorrect. background-position: right top; usesright top, which is horizontal then vertical, the correct order.Final Answer:
background-position: right top; -> Option CQuick Check:
Horizontal then vertical = right top [OK]
- Swapping horizontal and vertical keywords
- Using invalid keyword combinations
- Forgetting semicolon at end
div { background-image: url('flower.png'); background-position: 50% 100%; } Where will the background image appear inside the div?Solution
Step 1: Understand percentage values in background-position
50% means horizontally centered, 100% means vertically at the bottom edge.Step 2: Match percentages to position
So the image is horizontally centered and vertically aligned at the bottom inside the div.Final Answer:
Centered horizontally and at the bottom vertically -> Option DQuick Check:
50% horizontal + 100% vertical = center bottom [OK]
- Mixing up horizontal and vertical order
- Assuming 100% means top
- Confusing percentages with pixels
section { background-position: 20px 30px 40px; }Solution
Step 1: Check the number of values for background-position
The property accepts one or two values: horizontal and optional vertical. Here, three values are given, which is invalid.Step 2: Verify other options
Units are present (px), property name is correct, and background image can be set separately, so those are not errors here.Final Answer:
Too many values for background-position -> Option AQuick Check:
background-position accepts max 2 values [OK]
- Adding extra values beyond two
- Forgetting units on length values
- Confusing property names
Solution
Step 1: Understand order of values in background-position
The first value is horizontal (left to right), the second is vertical (top to bottom).Step 2: Match values to desired position
10px from left means horizontal = 10px; 20% from top means vertical = 20%. Sobackground-position: 10px 20%;is correct.Final Answer:
background-position: 10px 20%; -> Option AQuick Check:
Horizontal then vertical = 10px 20% [OK]
- Swapping horizontal and vertical values
- Using wrong units for values
- Adding keywords incorrectly
