Background Position & Repeat
By the end of this lesson you will be able to place a background image exactly where you want it, using keywords, lengths, percentages or offsets from any edge. You will also understand why percentages behave differently from lengths, and you will control tiling with repeat-x, repeat-y, space and round. This lesson builds on Background Images, so read that one first if url() and layers are new to you.
The positioning area
Every background image is placed inside a rectangle called the positioning area. By default this is the element's padding box: the content plus the padding, but not the border. (You will learn to change it with background-origin in Background Size & Attachment.)
background-position says where the image sits inside that rectangle. Its default is 0% 0%, which means the top-left corner. That is why an image with no position always starts at the top left.
Keywords
The easiest way to place an image is with keywords. For the horizontal direction use left, center or right. For the vertical direction use top, center or bottom.
.card { background-position: right bottom; } /* bottom-right corner */
.card { background-position: top; } /* top edge, centred across */
.card { background-position: center; } /* middle of the box */With two keywords the order does not matter: right bottom and bottom right mean the same thing, because the browser can tell which keyword is horizontal. If you give only one keyword, the other direction becomes center.
Step 1: keywords on a cricket scorecard
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Position keywords</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; display: flex; flex-wrap: wrap; gap: 1rem; }
.box {
width: 160px;
height: 110px;
border: 2px solid #94a3b8;
background: #f1f5f9 url("https://course.ethicscomputer.in/assets/samples/icon.svg") no-repeat;
font: 600 0.8rem ui-monospace, monospace;
padding: 0.25rem;
}
.a { background-position: left top; }
.b { background-position: center; }
.c { background-position: bottom right; }
.d { background-position: top; }
</style>
</head>
<body>
<div class="box a">left top</div>
<div class="box b">center</div>
<div class="box c">bottom right</div>
<div class="box d">top</div>
</body>
</html>You should see four grey boxes with the star in the top-left corner, the middle, the bottom-right corner and the top centre. Notice that the image covers the label text in some boxes. Backgrounds never move content out of the way.
Lengths
You can also give exact distances, such as 20px 10px. The first value is the horizontal distance from the left edge. The second value is the vertical distance from the top edge. With lengths, the order matters: x first, then y.
.ticket { background-position: 20px 10px; } /* 20px from left, 10px from top */
.ticket { background-position: -10px 0; } /* slides 10px past the left edge */A length moves the image's top-left corner. A negative length pushes part of the image outside the box, where it is cut off.
How percentages really work
Percentages look like lengths, but they follow a different rule. A percentage lines up the same point on the image and on the box. 50% 50% puts the centre of the image on the centre of the box. 100% 100% puts the bottom-right corner of the image on the bottom-right corner of the box.
Think of pinning a photo on a notice board. With 25%, you mark a point a quarter of the way across the photo, and pin it on a point a quarter of the way across the board.
The browser turns this into a pixel offset with one formula:
offset = (box size - image size) x percentageTake a box 300px wide and an image 64px wide:
| Value | Calculation | Image's left edge sits at |
|---|---|---|
0% | (300 - 64) x 0 | 0px |
25% | (300 - 64) x 0.25 | 59px |
50% | (300 - 64) x 0.5 | 118px (exactly centred) |
100% | (300 - 64) x 1 | 236px (right edge touches the box) |
300px (a length) | none | 300px (completely outside) |
The last row shows why 100% and a full-width length are not the same. The percentage takes the image's own size into account, so the image never runs off the edge.
Now the interesting case: an image bigger than the box. A 800px wide image in a 300px box gives (300 - 800) x 0.5 = -250px for 50%. The image moves left by 250px, so its middle part shows in the box. This is exactly what you want when cropping a large photo, and it is why center works well with big hero images.
Offsets from any edge
Sometimes you want an image a fixed distance from the right or bottom edge, such as a badge 12px from the bottom-right corner. Lengths only measure from the left and top. The four-value syntax solves this: write an edge keyword followed by an offset, for both directions.
.order { background-position: right 12px bottom 12px; }This reads as "12px from the right edge, 12px from the bottom edge". It stays in the corner however wide the card grows. In older code you may see calc(100% - 12px) for the same job. It works, because the 100% part keeps the image inside the box, but the four-value form is easier to read.
There are also longhands, background-position-x and background-position-y, for changing one direction only. They are handy when a hover effect should move an image sideways without touching its vertical position.
Step 2: percentages and edge offsets
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Percentages and offsets</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; }
.track {
width: 300px;
height: 64px;
margin-bottom: 0.5rem;
background: #e0f2fe url("https://course.ethicscomputer.in/assets/samples/icon.svg") no-repeat;
}
.p0 { background-position: 0% 0; }
.p25 { background-position: 25% 0; }
.p50 { background-position: 50% 0; }
.p100 { background-position: 100% 0; }
.len { background-position: 300px 0; }
.order {
max-width: 22rem;
margin-top: 1.5rem;
padding: 1rem 1rem 3.5rem;
border-radius: 10px;
background: #fef3c7 url("https://course.ethicscomputer.in/assets/samples/icon.svg") no-repeat right 12px bottom 12px;
}
</style>
</head>
<body>
<div class="track p0"></div>
<div class="track p25"></div>
<div class="track p50"></div>
<div class="track p100"></div>
<div class="track len"></div>
<article class="order">
<h3>Order #2147, Kabir, Lucknow</h3>
<p>2 samosa, 1 lassi. Ready in 10 minutes.</p>
</article>
</body>
</html>The five strips show the star at 0px, 59px, 118px and 236px. The last strip is empty, because a 300px length puts the image just past the right edge. The order card keeps its star 12px from the bottom-right corner. Try changing the card's max-width and watch the star follow the corner.
Repeating
By default a background image repeats in both directions to fill the box, like tiles on a floor. background-repeat changes this:
| Value | Result |
|---|---|
repeat | Default. Tiles across and down; tiles at the edges are cut |
no-repeat | One copy only |
repeat-x | One row of tiles across |
repeat-y | One column of tiles down |
space | Only whole tiles; the spare room becomes equal gaps between them |
round | Only whole tiles; tiles are stretched or squashed to fit with no gap |
Tiling starts from the position you set and spreads in both directions. So background-position: center with repeat-x puts one tile in the middle and cuts the tiles at both ends equally.
background-repeat also takes two values, one for each direction. repeat no-repeat is the same as repeat-x, and space round uses a different rule across and down.
Think of tiling a kitchen wall. repeat uses full tiles and cuts the last ones to size. space refuses to cut tiles and leaves wider grout gaps. round orders slightly bigger or smaller tiles so a whole number fits exactly.
With space, the first and last tiles touch the edges of the box, and background-position is ignored. The one exception is when not even two tiles fit: then the browser draws a single tile and places it with background-position.
Step 3: comparing the repeat values
Every box here is 230px wide and 140px tall, and the star is 64px square.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Repeat values</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; display: flex; flex-wrap: wrap; gap: 1rem; }
figure { margin: 0; }
figcaption { font: 600 0.85rem ui-monospace, monospace; margin-bottom: 0.25rem; }
.demo {
width: 230px;
height: 140px;
outline: 2px solid #475569;
background-color: #f8fafc;
background-image: url("https://course.ethicscomputer.in/assets/samples/icon.svg");
}
</style>
</head>
<body>
<figure><figcaption>repeat</figcaption><div class="demo" style="background-repeat: repeat"></div></figure>
<figure><figcaption>repeat-x</figcaption><div class="demo" style="background-repeat: repeat-x"></div></figure>
<figure><figcaption>repeat-y</figcaption><div class="demo" style="background-repeat: repeat-y"></div></figure>
<figure><figcaption>space</figcaption><div class="demo" style="background-repeat: space"></div></figure>
<figure><figcaption>round</figcaption><div class="demo" style="background-repeat: round"></div></figure>
</body>
</html>With repeat, the stars on the right and bottom edges are cut. With space, 3 whole stars fit across (192px), and the spare 38px becomes two gaps of 19px. Down, 2 stars fit with a 12px gap. With round, 230 / 64 is about 3.6, which rounds to 4, so each tile becomes 57.5px wide. Down, 140 / 64 is about 2.2, which rounds to 2, so each tile becomes 70px tall. No tile is cut. Our star is an SVG file, and an SVG keeps its own shape inside a resized tile, so it shrinks a little and leaves thin gaps. A JPG or PNG would be stretched to 57.5 by 70 instead.
Common mistakes
- Mixing up length order.
10px 40pxis 10px across and 40px down, not the other way round. - Treating percentages like lengths.
100%puts the image's right edge on the box's right edge; it does not push the image out. - Forgetting
no-repeat. A small icon placed in a corner still tiles over the whole box. - Expecting
spaceto obeybackground-position. It ignores the position unless only one tile fits. - Using
roundon photos. The squashing is fine for patterns but distorts faces and scenes.
Interview questions
1. How does a percentage value in background-position work?
It aligns the point at that percentage of the image with the point at the same percentage of the positioning area. The offset is (area size - image size) x percentage. So 50% centres the image and 100% aligns its far edge with the box's far edge.
2. Why does background-position: 100% keep the image inside the box while background-position: 300px in a 300px box does not?
A length moves the image's top-left corner by that amount, so 300px puts the image just outside. A percentage subtracts the image size first, so 100% places it flush with the right edge.
3. How do you place a background 20px from the bottom-right corner?
Use the four-value syntax: background-position: right 20px bottom 20px. Each edge keyword is followed by an offset from that edge. The older alternative is calc(100% - 20px) calc(100% - 20px).
4. What is the difference between space and round?
Both show only whole tiles. space keeps the tile size and spreads the leftover room as equal gaps, with the outer tiles touching the edges. round resizes the tiles so a whole number fits with no gaps.
5. What is the default value of background-position, and what is the positioning area by default?
The default is 0% 0%, the top-left corner. The positioning area is the padding box, set by background-origin: padding-box.
Key points
background-positionplaces an image inside the positioning area, which is the padding box by default.- One keyword sets one direction; the other becomes
center. Two keywords can come in any order. - Lengths move the image's top-left corner: x first, then y.
- Percentages align the same point on image and box: offset = (box - image) x percentage.
- The four-value syntax, such as
right 12px bottom 12px, measures from any edge. background-repeattakesrepeat,no-repeat,repeat-x,repeat-y,spaceorround, or one value per direction.
Exercise
Make a "Today's Special" card for a dhaba in Ludhiana. Use the logo sample image as a single badge 16px from the top-right corner. Add a second layer: the icon sample repeated as a border strip along the bottom of the card only, with whole tiles and even gaps. Give the card enough bottom padding that the text does not overlap the strip. Then change the card width and check that the badge and strip adapt.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Today's special</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; }
.special {
max-width: 26rem;
padding: 6.5rem 1.25rem 5.5rem;
border-radius: 12px;
background-color: #fff7ed;
background-image: url("https://course.ethicscomputer.in/assets/samples/logo.svg"), url("https://course.ethicscomputer.in/assets/samples/icon.svg");
background-position: right 16px top 16px, left 0 bottom 8px;
background-repeat: no-repeat, space no-repeat;
}
.special h2 { margin-top: 0; }
</style>
</head>
<body>
<article class="special">
<h2>Today's Special</h2>
<p>Sarson da saag with makki di roti, ₹180.</p>
</article>
</body>
</html>The logo uses the four-value syntax, so it stays in the top-right corner. The star layer uses space no-repeat, which spaces whole tiles across and keeps a single row, and its position puts that row 8px above the bottom edge.
Quick Quiz
1. A box is 400px wide and the image is 100px wide. Where does the image's left edge sit with background-position: 50% 0?
(400 - 100) x 0.5 = 150px, which centres the 100px image.
2. Which value places an image 10px from the right edge and 10px from the bottom edge?
Edge keywords followed by offsets measure from that edge.
3. What does background-repeat: round do when the box is 230px wide and the image is 64px wide?
230 / 64 is about 3.6, which rounds to 4 tiles of 57.5px each.
4. Which declaration is the same as background-repeat: repeat-y?
The first value is for the horizontal direction and the second for the vertical.
Learn CSS with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
