Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

ray(): a straight motion path from an origin

Place an element along a direction using offset-distance, with an optional allowance for its size.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

offset-path: ray(90deg closest-side contain at 50% 50%); offset-distance: 100%;

ray() defines a straight path for offset-path; it does not start an animation by itself. The angle starts upwards at 0deg and turns clockwise, so 90deg points right. at sets the origin, while offset-distance selects the position along the path. offset-anchor chooses the point of the moving element placed there; offset-rotate controls its orientation.

The size keyword determines the distance represented by 100%. closest-side uses the nearest side distance, while other keywords use different side or corner distances. contain shortens the path by half the larger dimension of the moving element, never below zero. It is useful for a centered square in this example, but it is not a universal guarantee that every shape, anchor and rotation will remain inside the container.

The example first measures a 200×200 container, then adds a 20×20 square with a centered anchor and rotation fixed at 0deg. At distances 0%, 50% and 100%, its center is respectively (100,100), (150,100) and (200,100). Adding contain moves the final center to (190,100), keeping this square inside the right edge. Measuring the container before inserting the square ensures a defined reference geometry for the initial read.

Examples

const stage=document.createElement('div');stage.style.cssText='position:fixed;left:0;top:0;width:200px;height:200px';
document.body.append(stage);stage.getBoundingClientRect();
const box=document.createElement('div');box.style.cssText='position:absolute;left:0;top:0;width:20px;height:20px;offset-anchor:center;offset-rotate:0deg;offset-path:ray(90deg closest-side at 50% 50%)';stage.append(box);
const center=distance=>{box.style.offsetDistance=distance;const r=box.getBoundingClientRect();return (r.x+r.width/2).toFixed(0)+','+(r.y+r.height/2).toFixed(0)};
const start=center('0%'),half=center('50%'),end=center('100%');
box.style.offsetPath='ray(90deg closest-side contain at 50% 50%)';
console.log('start='+start+'; half='+half+'; end='+end+'; contain-end='+center('100%')+'; size='+box.offsetWidth+'x'+box.offsetHeight+'; supported='+CSS.supports('offset-path','ray(90deg closest-side contain at 50% 50%)'));

Result

start=100,100; half=150,100; end=200,100; contain-end=190,100; size=20x20; supported=true

Verified in Chromium 157.0.8103.51. Provide a normal-position fallback for unsupported browsers; the test checks static distances, not animation smoothness or all ray-size variants.

Sources