DEV Community

Leo
Leo

Posted on Originally published at news.html.to

Spatial CSS preview puts 3D models on transform and anchor positioning

Put three 3D models on a page and you get three separate worlds. Each <model> has its own space and its own lighting. If you want one block to sit on a board, you work out the numbers yourself or you write JavaScript to do it.

In a post on the WebKit blog dated November 16, Brandel Zachernuk proposes giving that job to CSS. Safari on visionOS 27.3 includes a limited feature preview of the proposed spatial CSS API, starting with the <model> element. The post says spatial CSS "is made of a lot of small tweaks to existing syntax." That's what makes it interesting to me. The new parts look a lot like CSS you already write.

To try it on Vision Pro or in the visionOS Simulator, turn on Settings > Apps > Safari > Advanced > Feature flags > CSS Portals.

One container, one shared space

Everything starts with spatial: portal. Per WebKit, any element that can contain other elements, such as a <figure> or a <div>, can act as a portal, and all of its children share one space. The preview focuses on models, but the post says the specification is written so the approach should work for all content.

Once models share a portal, transform moves, rotates and scales them. The post says it's the same DOMMatrix class that produces 2D and pseudo-3D transforms elsewhere, but inside a portal it becomes a true 3D transform. This is WebKit's starter scene, with a board and two blocks moved off to the sides:

<div class="world" style="spatial:portal;">
  <model id="board" src="board.usdz"></model>
  <model id="octagon" src="octagon.usdz"></model>
  <model id="star" src="star.usdz"></model>
</div>
<style>
  #octagon { transform: translateX(-50cm); }
  #star { transform: translateX(50cm); }
</style>
Enter fullscreen mode Exit fullscreen mode

Each model moves on its own. A model's animation timeline can only play through one sequence, and WebKit says independent transforms give you the extra freedom you need for interaction or configuration.

Anchor positioning gets a third axis

This is the part I like most. <model> is a void element, so you can't nest one model inside another to attach it. WebKit borrows from anchor positioning instead. You name an anchor on one model, point another model at it, and add position-context: anchor so the alignment applies.

<style>
  #board { anchor-name: --board; }
  #octagon {
    position-anchor: --board#octagon-position;
    position-context: anchor;
  }
</style>
Enter fullscreen mode Exit fullscreen mode

The #octagon-position part is the clever bit. In WebKit's example, the board file includes two reference positions, one per slot. 3D editors call these "null objects" or "empties". The CSS points straight at a marker inside the model. You don't copy coordinates out of the 3D editor or guess at a transform. Per the post, these anchor points can be animated as well.

If you've placed a tooltip with anchor-name and position-anchor, you already know how this works. It's the same idea with one more axis.

Lighting becomes an at-rule

An environment map is the lighting that reflects off an object. It started out as an HTML attribute on <model>. Now that more elements can share one, WebKit has moved it into an at-rule modelled on @keyframes. You define it once and refer to it by name.

@environment-map {
  name: "spooky";
  format: equirectangular;
  src: url("night.hdr");
}

.world.spooky {
  environment-map: "spooky";
}
Enter fullscreen mode Exit fullscreen mode

The preview supports 360-degree HDR images. Because the lighting is attached with a class, changing it is a matter of changing the class, and the cascade does the rest.

Orbiting, and leaving the window

The preview has two more pieces. portal-action: orbit lets users rotate the scene in place. It mirrors the stagemode attribute on <model>, and WebKit says it's more flexible as CSS. The post mentions pan-x and pan-y as modes that could be useful later.

The only JavaScript API is requestVolumetricScene(). When a user action calls it, the content leaves its portal and appears in the user's space as what WebKit calls a Connected Volumetric Scene. The page stays connected to the scene, so you can still add, remove or move objects.

launchSpaceButton.addEventListener("click", event => {
  const world = document.querySelector(".world");
  world.requestVolumetricScene();
});
Enter fullscreen mode Exit fullscreen mode

Why CSS and not a script

WebKit's reasoning holds up. When spatial settings live in declarative CSS, media queries for aspect ratio, color theme and reduced motion can adjust the scene too. That means less JavaScript and no complicated state to keep track of. The numbers can be animated and transitioned like any other value. The post also says spatial web standards shouldn't be headset-only, since 3D content now appears on phones, tablets and computers.

Where it actually stands

To be clear about status: this is a preview behind a flag, it runs only on visionOS, and the API is a proposal. The <model> element is the part that has shipped. Per the post, it's in Safari 27 on all platforms. Spatial CSS has not shipped. The WebKit post covers only WebKit and says nothing about other engines, so there's no cross-browser story to tell yet.

If you have the simulator, rebuild the board-and-blocks demo and see how position-anchor behaves in three dimensions. WebKit is asking authors to try the preview and report issues at bugs.webkit.org. Early feedback from people building real pages is how a proposal like this gets better before it settles.

Top comments (0)