Style html elements with TailwindCSS using data and aria attributes
npm install tailwindcss-attributesbash
pnpm add -D tailwindcss-attributes
`
#### Using npm
`bash
npm install tailwindcss-attributes --save-dev
`
#### Using yarn
`bash
yarn add -D tailwindcss-attributes
`
Then add the plugin to your tailwind.config.js file:
`javascript
// tailwind.config.js
module.exports = {
theme: {},
corePlugins: {
aria: false,
},
plugins: [require("tailwindcss-attributes")],
}
`
Usage
Use aria- and data- attributes to style html elements
`html
aria-checked="true"
class="group-data-switch-on:bg-blue-500 bg-gray-200 relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
type="button"
>
Use setting
aria-hidden="true"
class="group-data-switch-on:translate-x-5 translate-x-0 pointer-events-none inline-block h-5 w-5 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-50"
data-switch
>
`
$3
Use these vanilla-headless attributes by usingdata-{name}-{value}
`html
....
...
`
| Attribute | Variant |
|:-------------------|:-----------------|
| data-state="open" | data-state-open |
| data-state="close" | data-state-close |
| data-switch="on" | data-switch-on |
| data-switch="off" | data-switch-off |
$3
These aria-* attribute all translate to a value of true
`css
[aria-pressed="true"].aria-pressed\:shadow-sm {
--tw-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
--tw-shadow-colored: 0 1px 2px 0 var(--tw-shadow-color);
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);
}
`
$3
The following attribute all evaluate to true
- aria-atomic:
- aria-busy:
- aria-checked:
- aria-current:
- aria-disabled:
- aria-expanded:
- aria-grabbed:
- aria-haspopup:
- aria-hidden:
- aria-invalid:
- aria-live:
- aria-modal:
- aria-multiline:
- aria-multiselectable:
- aria-pressed:
- aria-readonly:
- aria-required:
- aria-selected:
$3
Use modifier to access attribute value
`html
``