Last active
August 15, 2026 16:42
-
-
Save rebane2001/3a87072d6ca23fc3ad99253e05c9f3cd to your computer and use it in GitHub Desktop.
Monkey-patch Tailwind CSS to also support attributes (outside of classes)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script> | |
| const original_querySelectorAll = document.querySelectorAll; | |
| document.querySelectorAll = function () { | |
| if (arguments?.[0] == "[class]" && (new Error().stack).split("\n").filter(e=>e!="Error")?.[1]?.includes?.("tailwindcss/browser")) | |
| return [...original_querySelectorAll.apply(document,["*"])].map(e=>({classList:[...e.classList,...[...e.attributes].map(x=>x.name.replace(/⁄/g,'/'))]})); | |
| return original_querySelectorAll.apply(document,arguments); | |
| } | |
| const original_push = Array.prototype.push; | |
| Array.prototype.push = function () { | |
| if ((s=(new Error().stack).split("\n").filter(e=>e!="Error")?.[1],s)?.includes?.("enter") | |
| && s.includes("tailwindcss/browser") | |
| && arguments?.[0]?.length == 3 | |
| && arguments?.[0]?.[0]?.[0] == ".") | |
| arguments[0][0] = `${arguments[0][0]},[${arguments[0][0].slice(1).replace(/\//g,'⁄')}]`; | |
| return original_push.apply(this,arguments); | |
| } | |
| const original_observe = MutationObserver.prototype.observe; | |
| MutationObserver.prototype.observe = function () { | |
| if ((new Error().stack).split("\n").filter(e=>e!="Error")?.[1]?.includes?.("tailwindcss/browser") | |
| && arguments?.[1]?.attributeFilter?.[0] == "class") | |
| arguments[1].attributeFilter = undefined; | |
| return original_observe.apply(this,arguments); | |
| } | |
| </script> | |
| <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script> | |
| <p class="text-blue-500">I am styled using a class</p> | |
| <p text-green-500>I am styled using an attribute</p> | |
| <p text-red-500⁄75>The catch is that you have to use ⁄ instead of / in attributes</p> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added firefox support