tailwindcss-anchors
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Anchors for Tailwind CSS

minified size license version twitter

Anchors for Tailwind CSS is a plugin that brings declarative support for the CSS Anchor Positioning API to Tailwind, allowing you to define and position elements relative to custom anchors. It adds utilities for anchor-name, position-anchor, position-area, anchor() and anchor-size() expressions.

It also lays the groundwork for using View Transitions to animate any anchored elements, which would require separate JS (for now 👀).

Installation

  1. Install the plugin from npm with your preferred package manager:

    npm install -D tailwindcss-anchors
  2. Then include it in your Tailwind CSS or JS config file:

    CSS (Tailwind CSS v4+)
    /* style.css */
    @import "@toolwind/anchors";
    JS (Tailwind CSS v3 compatible)
    // tailwind.config.js
    import anchors from "@toolwind/anchors";

Usage

Defining an anchor

Use the anchor/{name} utility to define an anchor point:

<div class="anchor/my-anchor"></div>

The CSS anchor-name property requires a dashed ident. For convenience, Anchors for Tailwind CSS automatically converts simple names into dashed idents.

For example, the above utility generates this CSS, prefixed with --tw-anchor_:

.anchor\/my-anchor {
  anchor-name: --tw-anchor_my-anchor;
}

If you need to use a specific dashed ident for an anchor name, the plugin will respect and preserve the original name if it encounters an ident that is already dashed.

<div class="anchor/--my-anchor"></div>

This utility also accepts arbitrary values, so if you want to pass a name via a CSS variable, you can do so using the square bracket syntax, like this:

<div class="[--custom-property:--my-anchor]">
  <div class="anchor/[var(--custom-property)]"></div>
</div>

🚧 Note that names passed via a custom property (square bracket syntax) resolves to a dashed ident already, as it's not possible for a plugin to read and manipulate runtime values.

Positioning relative to an anchor

Once an anchor has been defined, you can anchor other elements to it.

Use anchored/{name} to attach an element to an anchor:

<div class="anchored/my-anchor"></div>

Use anchored-{side} to specify the position area of the anchored element. For example, anchored-top-center will position the element at the top center of its anchor, touching the anchored element:

<div class="anchored-top-center"></div>

Or, put both together for shorthand syntax:

<div class="anchored-top-center/my-anchor"></div>

This sets:

position-anchor: --tw-anchor_my-anchor;
position-area: top center;
position: absolute;
view-transition-name: --tw-anchor-view-transition-313d192p322r2w3336;
/* ☝️ View Transition-ready, with encoded view-transition-name */

Both the position and view-transition-name are applied with zero specificity (via :where()), making them easy to overwrite with other values, if you choose. As a rule of thumb, anchored elements must use absolute or fixed positioning. This plugin defaults to absolute positioning, but you can add fixed to use fixed positioning instead.

This plugin strives to strike a balance between abstracting away the complexity of the CSS Anchor Positioning API and empowering developers to fully leverage it.

Supported Utilities

anchor/{name}

Sets anchor-name: --tw-anchor_{name}

anchored/{name}

Sets:

  • position-anchor: --tw-anchor_{name}
  • view-transition-name (automatically generated per anchor)

anchor-{position}

Sets position-area. Examples:

  • anchor-top-centertop center
  • anchor-bottom-span-left
  • anchor-top-right, etc.

{top|right|bottom|left|inset}-anchor-{side}-{offset}/{name?}

Generates directional offset using anchor():

<div class="top-anchor-bottom"></div>

Results in:

top: anchor(bottom);

With offset support:

<div class="top-anchor-bottom-4"></div>
top: calc(anchor(bottom) + 1rem); // assuming 4 = theme('spacing.4')

{w|h}-anchor{-size?}/{name?}]

Size utilities using anchor-size():

  • Omitting the anchor size (i.e. default size/dimension)

    w-anchorwidth: anchor-size(width) If the {size} is omitted, the dimension defaults to the <anchor-size> keyterm that matches the axis of the property in which the function is included. For example, width: anchor-size(); is equivalent to width: anchor-size(width);. (source: MDN)

  • Declaring the anchor-size (width/height/etc.) to use as a length

    w-anchor-heightwidth: anchor-size(height)

  • Setting/omitting the anchor name

    • w-anchorwidth: anchor-size(width)
    • w-anchor/--namewidth: anchor-size(--name width)
    • w-anchor/namewidth: anchor-size(--tw-anchor_name width)

    Specifying an <anchor-name> inside an anchor-size() function neither associates nor tethers an element to an anchor; it only defines which anchor the element's property values should be set relative to. (source: MDN)

View Transition API Integration

Every anchored/{name} class includes a view-transition-name, making your anchored elements animatable with document.startViewTransition():

document.startViewTransition(() => {
  el.classList.remove('anchor-top-left')
  el.classList.add('anchor-bottom-right')
})

This animates position shifts smoothly using the browser-native View Transitions API.

Why use Anchors for Tailwind CSS? 🤔

  • Declarative anchor positioning with Tailwind utility syntax
  • Full support for native anchor-related CSS properties and functions
  • Easy offset control via calc(anchor(...) + theme spacing) (under the hood)
  • Built-in support for View Transitions
  • Fully JIT-compatible, no runtime required

Additional Resources 📚

Coming soon 👀🤞

Some relevant features that are not part of this plugin yet are:

  • anchor-center
  • position-try-order | position-try-fallbacks | position-try
  • position-visibility

If you liked this plugin...

Check out more by @branmcconnell:

Package Sidebar

Install

npm i tailwindcss-anchors

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

18 kB

Total Files

7

Last publish

Collaborators

  • brandonmcconnell