Bird
Raised Fist0
CSSmarkup~15 mins

Background position in CSS - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Background Position with CSS
📖 Scenario: You are creating a simple webpage section that uses a background image. You want to control where the background image appears inside the section.
🎯 Goal: Build a webpage with a <section> that has a background image. Use CSS to set the background image and then adjust its position to show different parts of the image.
📋 What You'll Learn
Create a <section> element with a class hero in HTML.
Add a background image to the .hero class in CSS.
Set the background position to center initially.
Change the background position to top right later.
Use CSS properties correctly and ensure the background image covers the section.
💡 Why This Matters
🌍 Real World
Background images are often used in website headers, banners, and sections to create visually appealing layouts.
💼 Career
Knowing how to control background images with CSS is a fundamental skill for front-end web developers and designers.
Progress0 / 4 steps
1
Create the HTML structure with a section
Create a <section> element with the class hero inside the <body> of your HTML document.
CSS
Hint

Use the <section> tag and add class="hero" inside the <body>.

2
Add a background image and basic styles
In a <style> block inside the <head>, add CSS for the .hero class. Set the background image to https://via.placeholder.com/600x400, set the height to 20rem, and set background-size to cover.
CSS
Hint

Use background-image with url(), set height to 20rem, and background-size to cover.

3
Set the background position to center
Inside the .hero CSS block, add background-position: center; to center the background image inside the section.
CSS
Hint

Use background-position: center; inside the .hero CSS block.

4
Change the background position to top right
Modify the background-position property in the .hero CSS block to top right so the background image aligns to the top right corner.
CSS
Hint

Change the value of background-position to top right.

Practice

(1/5)
1. What does the CSS property background-position control in a webpage?
easy
A. The border style of the element
B. Where the background image is placed inside an element
C. The color of the background
D. The size of the background image

Solution

  1. Step 1: Understand the property purpose

    The background-position property sets the location of the background image inside an element.
  2. Step 2: Compare with other properties

    Other properties like background-size control size, and background-color controls color, not position.
  3. Final Answer:

    Where the background image is placed inside an element -> Option B
  4. Quick Check:

    Background position = image placement [OK]
Hint: Background-position sets image location inside element [OK]
Common Mistakes:
  • Confusing background-position with background-size
  • Thinking it changes background color
  • Assuming it controls element borders
2. Which of the following is the correct syntax to set the background image position to the top right corner?
easy
A. background-position: left bottom;
B. background-position: top left;
C. background-position: right top;
D. background-position: center center;

Solution

  1. Step 1: Recall keyword order for background-position

    The correct order is horizontal first, then vertical. So top right is incorrect order.
  2. Step 2: Check options carefully

    background-position: top left; uses top left, which is vertical then horizontal, so it is incorrect. background-position: right top; uses right top, which is horizontal then vertical, the correct order.
  3. Final Answer:

    background-position: right top; -> Option C
  4. Quick Check:

    Horizontal then vertical = right top [OK]
Hint: Write horizontal first, then vertical in background-position [OK]
Common Mistakes:
  • Swapping horizontal and vertical keywords
  • Using invalid keyword combinations
  • Forgetting semicolon at end
3. Given the CSS rule:
div { background-image: url('flower.png'); background-position: 50% 100%; }
Where will the background image appear inside the div?
medium
A. At the bottom right corner
B. At the top left corner
C. Centered both horizontally and vertically
D. Centered horizontally and at the bottom vertically

Solution

  1. Step 1: Understand percentage values in background-position

    50% means horizontally centered, 100% means vertically at the bottom edge.
  2. Step 2: Match percentages to position

    So the image is horizontally centered and vertically aligned at the bottom inside the div.
  3. Final Answer:

    Centered horizontally and at the bottom vertically -> Option D
  4. Quick Check:

    50% horizontal + 100% vertical = center bottom [OK]
Hint: Percentages: 50% center, 100% bottom [OK]
Common Mistakes:
  • Mixing up horizontal and vertical order
  • Assuming 100% means top
  • Confusing percentages with pixels
4. Identify the error in this CSS snippet:
section { background-position: 20px 30px 40px; }
medium
A. Too many values for background-position
B. Missing units for values
C. Incorrect property name
D. Background image not specified

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Too many values for background-position -> Option A
  4. Quick Check:

    background-position accepts max 2 values [OK]
Hint: Use max two values: horizontal and vertical [OK]
Common Mistakes:
  • Adding extra values beyond two
  • Forgetting units on length values
  • Confusing property names
5. You want a background image to appear exactly 10px from the left and 20% from the top inside a container. Which CSS rule correctly sets this?
hard
A. background-position: 10px 20%;
B. background-position: 20% 10px;
C. background-position: left 10px top 20%;
D. background-position: 10% 20px;

Solution

  1. Step 1: Understand order of values in background-position

    The first value is horizontal (left to right), the second is vertical (top to bottom).
  2. Step 2: Match values to desired position

    10px from left means horizontal = 10px; 20% from top means vertical = 20%. So background-position: 10px 20%; is correct.
  3. Final Answer:

    background-position: 10px 20%; -> Option A
  4. Quick Check:

    Horizontal then vertical = 10px 20% [OK]
Hint: Horizontal first, vertical second in background-position [OK]
Common Mistakes:
  • Swapping horizontal and vertical values
  • Using wrong units for values
  • Adding keywords incorrectly