Canadagnat/css-scope-inline
🌘 Scope your inline style tags in pure vanilla CSS! Only 16 lines. No build. No dependencies.
Featured achievements
The project's latest awards
#1 Repository of the month
#1 Repository of the month
#1 Repository of the month
🌘 CSS Scope Inline
(Art by shahabalizadeh)
Why does this exist?
- You want an easy inline vanilla CSS experience without Tailwind CSS.
- Hate creating unique class names over and over.. to use once.
- You want to co-locate your styles for ⚡️ Locality of Behavior (LoB)
- You wish
thiswould work in ` redgreengreengreenyellowbluegreengreen
Variables and child elements example.
At first glance, Tailwind Example 2 looks very promising, but:
- 🔴 Every child style requires an explicit selector.
- Tailwinds' shorthand advantages start to disappear.
- Tailwind becomes larger than vanilla CSS when more child elements are added.
- This limited example is the best case scenario for Tailwind.
- 🔴 Not visible on github: no highlighting for properties and units becomes painful.
<!-- CSS Scope Inline -->
<div>
<div><a href="#">Home</a></div>
<div><a href="#">Team</a></div>
<div><a href="#">Profile</a></div>
<div><a href="#">Settings</a></div>
<div><a href="#">Log Out</a></div>
</div>
<!-- Tailwind Example 1 -->
<div class="mx-2 my-4">
<div><a href="#" class="block py-2 px-3 my-2 bg-[--color-1] rounded-lg text-center hover:bg-[--color-1-active] hover:text-white">Home</a></div>
<div><a href="#" class="block py-2 px-3 my-2 bg-[--color-1] rounded-lg text-center hover:bg-[--color-1-active] hover:text-white">Team</a></div>
<div><a href="#" class="block py-2 px-3 my-2 bg-[--color-1] rounded-lg text-center hover:bg-[--color-1-active] hover:text-white">Profile</a></div>
<div><a href="#" class="block py-2 px-3 my-2 bg-[--color-1] rounded-lg text-center hover:bg-[--color-1-active] hover:text-white">Settings</a></div>
<div><a href="#" class="block py-2 px-3 my-2 bg-[--color-1] rounded-lg text-center hover:bg-[--color-1-active] hover:text-white">Log Out</a></div>
</div>
<!-- Tailwind Example 2 -->
<div class="mx-2 my-4
[&_div_a]:block [&_div_a]:py-2 [&_div_a]:px-3 [&_div_a]:my-2 [&_div_a]:bg-[--color-1] [&_div_a]:rounded-lg [&_div_a]:text-center
[&_div_a:hover]:bg-[--color-1-active] [&_div_a:hover]:text-white">
<div><a href="#">Home</a></div>
<div><a href="#">Team</a></div>
<div><a href="#">Profile</a></div>
<div><a href="#">Settings</a></div>
<div><a href="#">Log Out</a></div>
</div>
🔎 Technical FAQ
- Why do you use
querySelectorAll()and not just process theMutationObserverresults directly?- This was indeed the original design; it will work well up until you begin recieving subtrees (ex: DOM swaps with htmx, ajax, jquery, etc.) which requires walking all subtree elements to ensure we do not miss a
<style>. This unfortunately involves re-scanning thousands of repeated elements. This is whyquerySelectorAll()ends up the performance (and simplicity) winner.
- This was indeed the original design; it will work well up until you begin recieving subtrees (ex: DOM swaps with htmx, ajax, jquery, etc.) which requires walking all subtree elements to ensure we do not miss a
🌘 Change Log
1.1.0
- Started change log.
- Fix for domain names ending in
.mewhen usedurl()properties.
Similar Projects
Discover other similar projects and AI models.
🗂 The essential checklist for modern web development, for humans and AI agents
前端面试每日 3+1,以面试题来驱动学习,提倡每日学习与思考,每天进步一点!每天早上5点纯手工发布面试题(死磕自己,愉悦大家),6000+道前端面试题全面覆盖,HTML/CSS/JavaScript/Vue/React/Nodejs/TypeScript/ECMAScritpt/Webpack/Jquery/小程序/软技能……
A Collection of application ideas that can be used to improve your coding skills ❤.
The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.
:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS
A Collection of application ideas which can be used to improve your coding skills.
24 Lessons, 12 Weeks, Get Started as a Web Developer
A professional front-end template for building fast, robust, and adaptable web apps or sites.