Favorites

ray() CSS Function

ray()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The ray() function defines a straight line from a starting point at an angle, used by offset-path to move an element along it.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The ray() CSS function defines a straight line segment, or "ray", that starts at a point and travels outward at a given angle. It is used with the offset-path property to move an element along that line, with offset-distance controlling how far along it the element sits. This makes it easy to create radial menus, burst effects and elements that fly out from a centre point.

The angle is measured clockwise from straight up, so 0deg points up and 90deg points right. The size keyword sets the length of the ray: closest-side, closest-corner, farthest-side, farthest-corner (the default) or sides, measured from the start point to the edges of the containing block. Adding contain shortens the ray so the element stays completely inside the containing block at 100%. The optional at <position> sets the start point; if it is omitted, the element's offset-position is used.

/* Fly an element out from the centre at 45 degrees */
.spark {
  offset-path: ray(45deg closest-side contain at 50% 50%);
  animation: fly 1s ease-out forwards;
}

@keyframes fly {
  from { offset-distance: 0%; }
  to { offset-distance: 100%; }
}

By default, an element following a ray is rotated to point along it, controlled by offset-rotate. Set offset-rotate: 0deg to keep it upright.

Interactive Demo

Demo Code
HTML Code
<div class="demo-area">
	<div class="demo-element"></div>
</div>
CSS Code
.demo-area {
	position: relative;
	width: 300px;
	height: 190px;
	background: #f4f4f4 radial-gradient(circle, #4361ee 3px, transparent 4px) no-repeat;
	outline: 1px dashed #b0b0b0;
}
.demo-element {
	position: absolute;
	left: 135px;
	top: 80px;
	width: 30px;
	height: 24px;
	background: #f72585;
	clip-path: polygon(0 0, 70% 0, 100% 50%, 70% 100%, 0 100%, 30% 50%);
	offset-distance: 70%;
	offset-rotate: auto;
	offset-path: ray(0deg);
}

Syntax

ray() = ray( <angle> && <ray-size>? && contain? && [ at <position> ]? )

<ray-size> = closest-side | closest-corner | farthest-side | farthest-corner | sides

Values

  • <angle>The direction of the ray, measured clockwise from straight up (0deg).
  • <ray-size>Optional. The length of the ray: closest-side, closest-corner, farthest-side, farthest-corner (default) or sides.
  • containOptional. Shortens the ray so the element stays fully inside its containing block at offset-distance: 100%.
  • at <position>Optional. The start point of the ray. Defaults to the element's offset-position.

Example

<div class="wrap">
<h1>CSS ray() examples</h1>
<div class="card"><div class="stage">
<span class="dot" style="--a: 0"></span>
<span class="dot" style="--a: 45"></span>
<span class="dot" style="--a: 90"></span>
<span class="dot" style="--a: 135"></span>
<span class="dot" style="--a: 180"></span>
<span class="dot" style="--a: 225"></span>
<span class="dot" style="--a: 270"></span>
<span class="dot" style="--a: 315"></span>
</div>
<div class="label">offset-path: ray(&lt;angle&gt; closest-side contain at 50% 50%)</div></div>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* ray() examples */
.stage {
position: relative;
height: 240px;
border-radius: 12px;
background: radial-gradient(circle, #e0e7ff, #f8fafc 70%);
}

.dot {
position: absolute;
width: 24px;
height: 24px;
border-radius: 50%;
background: hsl(calc(var(--a) * 1) 75% 50%);
offset-path: ray(calc(var(--a) * 1deg) closest-side contain at 50% 50%);
offset-rotate: 0deg;
animation: fly 1.6s ease-in-out infinite alternate;
}

@keyframes fly {
from { offset-distance: 0%; }
to { offset-distance: 100%; }
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS ray() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.