Skip to content

Members for Tailwind CSS simplifies styling an ancestor element based on the state of its descendants.

License

Notifications You must be signed in to change notification settings

brandonmcconnell/tailwindcss-members

Repository files navigation

Members for Tailwind CSS

minified size license version twitter

Members for Tailwind CSS offers an inverse functionality to the built-in groups feature of Tailwind CSS, providing a more declarative way to customize a parent element based on the state of its descendants, helping you to simplify complex scenarios and build user interfaces more intuitively.

Currently, the common solution for this is to use the has-[...] variant with an arbitrary value, targeting a descendant selector explicitly. Arbitrary values in Tailwind CSS are powerful, but in many cases, it's better to reserve them as a last resort or escape hatch when a native-like solution is available.

Members for Tailwind CSS fills this gap by providing a declarative API which handles that :has(...) check under the hood.

Installation

You can install the plugin via npm:

npm install tailwindcss-members

Include it in your tailwind.config.js:

module.exports = {
  plugins: [
    require('tailwindcss-members'),
  ]
}

Usage

The plugin introduces the member variant, which can be used to apply styles based on a descendant's state.

Here's an example comparing the traditional approach with the new members approach:

Example: Without Members

<div class="bg-red-400 has-[>form:valid]:bg-green-400">
  <form>
    <input type="checkbox" required /> ← Check this checkbox
  </form>
</div>

Open this example in Tailwind Play: https://play.tailwindcss.com/wVPSR6UF7W


Example: With Members

<div class="bg-red-400 member-valid:bg-green-400">
  <form class="member">
    <input type="checkbox" required /> ← Check this checkbox
  </form>
</div>

Open this example in Tailwind Play: https://play.tailwindcss.com/0YMyI2NpIf



I hope you find tailwindcss-members a valuable addition to your projects. If you have any issues or suggestions, don't hesitate to open an issue or pull request.

If you liked this, you might also like my other Tailwind CSS plugins:

About

Members for Tailwind CSS simplifies styling an ancestor element based on the state of its descendants.

Resources

License

Stars

Watchers

Forks

Packages

No packages published