Skip to content

tmac/css-scope-inline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

49 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌘 CSS Scope Inline

cover (Art by shahabalizadeh)

Why does this exist?

  • You want an easy inline vanilla CSS experience without Tailwind CSS.
  • Hate typing individual class selectors and ids over.. and over..
  • You want to co-locate your <style> tags for ⚑️ Locality of Behavior (LoB)
  • You wish this would work in <style> tags.
  • Access to all features including CSS Nesting and animations.
  • No build step. Dependency-free. Only 15 lines!
  • Pairs well with Surreal and htmx
  • Want fewer layers, less complexity. Are aware of the cargo cult. ✈️

✨ Want to also inline scope your <script> tags? See our companion project, Surreal

πŸ‘οΈ How does it look?

<div>
    <style>
        .me { background: red; } /* ✨ .this and .self also work! */
        .me button { background: blue; }
    </style>
    <button>I'm blue</button>
</div>

Get a taste- see the Live Example! Then view source.

🌘 How does it work?

This uses MutationObserver to monitor the DOM, and the moment a <style> tag is seen, it scopes the styles to whatever the parent element is. No flashing or popping.

This method also leaves your existing styles untouched, allowing you to mix and match at your liesure.

πŸ€” Why consider this over Tailwind CSS?

Use whatever you'd like, but there's a few advantages with this approach:

  • No suffering from FOUC (a flash of unstyled content) as experienced in Tailwind CDN, Twind, UnoCSS.
  • No high risk of eventually requiring a build step. It just works.
  • No chance of deprecations. 15 lines is infinitely maintainable.
  • Use the browser inspector to live edit / copy / paste styles.
  • Universal vanilla CSS. No special syntax or editor plugins to install.

About

🌘 Scope your inline style tags in pure vanilla CSS! Only 16 lines. No build. No dependencies.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 78.6%
  • JavaScript 21.4%