Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

background-position-x: change only the horizontal position

Set a physical horizontal coordinate for each background layer without replacing its vertical position.

Version: CSS Modules

Syntax

background-position-x: 25%, calc(100% - 10px);

background-position-x controls the horizontal component of a background position. Use left, center, right, a length or a percentage; an omitted edge means left. Its initial value is 0%. Percentages use the positioning-area width minus the already sized image width, so center aligns centers rather than putting the image’s left edge halfway across the box. calc(100% - 10px) leaves a 10px gap at the right edge. A comma-separated list sets separate layers; a shorter list repeats to match background-image.

This is the physical x axis, not the inline axis. Changing direction or writing-mode does not turn an explicit left-based percentage into a logical start offset. The CSS Backgrounds Level 4 draft also discusses newer logical directions; their presence in a draft is not a claim that every browser supports them. Setting this longhand preserves background-position-y, but a later background-position or background shorthand can set that axis again. Image order also matters because the first layer is painted above the rest.

The example uses two non-overlapping layers in a 200×80px box: red is 20×10px at y=10px, blue is 30×10px at y=40px. The x list 25%, calc(100% - 10px) places them at 45px and 160px. The console confirms that the y list stays 10px, 40px. A second box uses vertical-rl with rtl direction and gives the same painted physical coordinates. PNG inspection checks both colored rectangles, independently of the computed-value strings.

Examples

['horizontal-tb','vertical-rl'].forEach((writingMode,i)=>{
  const box=document.createElement('div');box.id='x-'+i;
  box.style.cssText='position:absolute;top:0;width:200px;height:80px;direction:rtl;background-image:linear-gradient(red,red),linear-gradient(blue,blue);background-size:20px 10px,30px 10px;background-repeat:no-repeat;background-position-y:10px,40px';
  box.style.left=(i*220)+'px';box.style.writingMode=writingMode;box.style.backgroundPositionX='25%, calc(100% - 10px)';document.body.append(box);
  const s=getComputedStyle(box);console.log(writingMode+': x='+s.backgroundPositionX+'; y='+s.backgroundPositionY);
});

Result

horizontal-tb: x=25%, calc(100% - 10px); y=10px, 40px
vertical-rl: x=25%, calc(100% - 10px); y=10px, 40px

Executed in Chromium 157.0.8103.51 with two gradient layers, explicit no-repeat and device pixel ratio 1. The example verifies physical x positioning in two writing modes; it does not execute draft logical keywords or cross-browser compatibility tests.

Sources