dnanocss
v0.0.1
Published
> A lightweight utility-first CSS engine powered by Vanilla JavaScript.
Maintainers
Readme
dnanoCSS ⚡
A lightweight utility-first CSS engine powered by Vanilla JavaScript.
dnanoCSS is a lightweight runtime utility-first CSS engine built with Vanilla JavaScript. It scans your DOM, parses utility classes, and dynamically generates real CSS without any build step or external dependency.
Why dnanoCSS?
Most utility-first frameworks require build tools, configuration, or compilation steps.
dnanoCSS explores a different approach: utility classes are interpreted directly in the browser at runtime using pure JavaScript.
The goal of this project is to deeply understand:
- DOM traversal
- token parsing
- dynamic CSS generation
- runtime styling systems
- utility-first architecture
Features
- Stylesheet Injection — Generates real CSS rules in a
<style>tag (not inline styles) - 60+ Utilities — Spacing, flexbox, colors, typography, borders, sizing, and more
- Color Palette — 30+ named colors + semantic tokens (
primary,danger,success) - Responsive Breakpoints —
dn-md-p-20→@media (min-width:768px) - Pseudo-States —
dn-hover-bg-primary,dn-focus-border-blue - MutationObserver — Auto-styles dynamically added DOM elements
- Modular Architecture —
engine,parser,utilities,constantsseparated cleanly
Installation
npm install dnanocssUsage
import "dnanocss";Environment Support
dnanoCSS currently works best in modern ESM/bundler environments such as:
- Vite
- Webpack
- Parcel
- Next.js
For direct browser usage without a bundler, use relative module imports or a future CDN build.
Quick Start
<!-- 1. Add the script -->
<script type="module" src="./index.js"></script>
<!-- 2. Use utility classes -->
<div class="dn-flex dn-items-center dn-justify-center dn-bg-primary dn-text-white dn-p-24 dn-rounded-12">
Hello dnanoCSS!
</div>Utility Reference
Spacing
dn-p-{n} dn-pt-{n} dn-pb-{n} dn-pl-{n} dn-pr-{n}
dn-px-{n} dn-py-{n}
dn-m-{n} dn-mt-{n} dn-mb-{n} dn-ml-{n} dn-mr-{n}
dn-mx-{n} dn-my-{n} dn-mx-autoColors
dn-bg-{color} dn-text-{color} dn-border-{color}Available: black, white, primary, secondary, danger, success, warning, info, red, blue, green, purple, pink, teal, gray, gray-100…gray-900, and more.
Typography
dn-fs-{n} dn-fw-{weight} dn-lh-{n}
dn-text-center dn-text-left dn-text-right
dn-uppercase dn-lowercase dn-capitalize
dn-italic dn-underline dn-truncateLayout / Flexbox
dn-flex dn-col dn-grid dn-block dn-hidden
dn-justify-center dn-justify-between dn-justify-evenly
dn-items-center dn-items-start dn-items-end
dn-gap-{n} dn-wrap dn-nowrapBorders
dn-border dn-border-2 dn-border-4 dn-border-0
dn-rounded-{n}Sizing
dn-w-{n} dn-h-{n} dn-w-full dn-h-screen dn-w-screen
dn-max-w-{n} dn-min-h-{n}Responsive
Prefix any utility with a breakpoint:
dn-sm-p-20 → @media (min-width: 640px) { padding: 20px }
dn-md-p-20 → @media (min-width: 768px) { padding: 20px }
dn-lg-p-20 → @media (min-width: 1024px) { padding: 20px }
dn-xl-p-20 → @media (min-width: 1280px) { padding: 20px }Pseudo-States
dn-hover-bg-primary → .dn-hover-bg-primary:hover { background-color: #2563eb }
dn-focus-border-blue → .dn-focus-border-blue:focus { border-color: #3b82f6 }
dn-active-text-white → .dn-active-text-white:active { color: #ffffff }Architecture
dnanoCSS/
├── index.js Entry point, boots engine on DOMContentLoaded
│
├── demo/
│ ├── index.html Main demo page showcasing utilities and features
│ ├── style.css Demo-specific styles
│ └── examples.html Utility showcase
│
├── src/
│ ├── engine.js DOM scanner, style injector, MutationObserver
│ ├── parser.js Tokenizer — converts "dn-p-20" → { padding: "20px" }
│ ├── utilities.js All utility mappings
│ └── constants.js Colors, breakpoints, config
│
├── package.json
├── README.md
├── LICENSE
└── .gitignoreHow the Engine Works
- DOM Ready —
DOMContentLoadedfires,initDnanoCSS()is called - Scan — Every element's
classListis checked fordn-prefixed classes - Parse —
parser.jssplits tokens, extracts breakpoints, pseudo-states, and values - Generate — Valid CSS rule strings are built (
camelCase→kebab-case) - Inject — Rules are inserted into a single
<style id="dnano-styles">tag - Observe —
MutationObserverhandles new elements added dynamically
Future Improvements
- [ ] Dark mode variant (
dn-dark-bg-gray-900) - [ ] CSS custom property output (
--dn-spacing-4: 16px) - [ ] CLI extractor for static HTML → pure CSS file
- [ ] Plugin system for custom utilities
- [ ] Caching layer with localStorage for repeat visits
- [ ]
!importantmodifier (dn-!p-0)
License
MIT — free to use, modify, and extend.
