Hands-on with Beer CSS Modular Version
If you love Material Design 3 and semantic HTML, you've likely come across Beer CSS. It lets you build clean UIs using semantic tags rather than nesting dozens of utility classes.The modular build allows developers to import only the specific components and helpers required, reducing stylesheet bundle sizes. Here is an overview of how the modular architecture works and how to set it up.
π§© The Anatomy: Settings, Helpers, and Elements
Beer CSS breaks down modular assets into three tiers:
Settings: Base styles, resets, theming tokens, and light/dark modes (all.css, reset, theme, etc.).
Helpers: Utility-like classes covering elevation, spacing, alignment, layout, and animations (elevate, form, space, etc.).
Elements: Standalone UI components (button, card, dialog, field, snackbar, etc.).
Every CSS module comes in standard (.css, .min.css) and scoped variations (.scoped.css, .scoped.min.css) to prevent global style bleeding.
β‘ How to Load Modules
1) The Query String Trick (Instant CDN) for lightweight setups or prototypes, load modules via CDN using URL query parameters:
<script
type="module"
src="https://cdn.jsdelivr.net/npm/beercss@5.0.3/dist/cdn/beer.loader.js?elements=button,dialog&helpers=form">
</script>
2) Using the Dynamic ui() LoaderStyles can also be loaded dynamically in vanilla JavaScript or component lifecycles using the beer.loader.js module:
<script type="module">
import ui from "https://cdn.jsdelivr.net/npm/beercss@5.0.3/dist/cdn/beer.loader.js";
// Dynamically load only what this view needs
ui("import", "elements=button,dialog&helpers=form");
</script>
3) Bundler Import (Vite, Webpack, Rollup) for build pipelines, install via npm and import specific parts directly into the application entry point:
npm install beercss
// 1) Settings (always first)
import "beercss/dist/cdn/settings/all.css";
// 2) Helpers (in alphabetical order)
import "beercss/dist/cdn/helpers/form.css";
// 3) Elements (CSS + JS if interactive)
import "beercss/dist/cdn/elements/button.css";
import "beercss/dist/cdn/elements/dialog.css";
import "beercss/dist/cdn/elements/dialog.js";
// 4) Runtime Loader
import "beercss/loader";
4) Custom Web ComponentWhen building micro-frontends or embedding components inside external CMS setups, the custom element is scoped by default:
<script
type="module"
src="https://cdn.jsdelivr.net/npm/beercss@5.0.3/dist/cdn/beer.custom-element.js?settings=&elements=&helpers=">
</script>
<beer-css import="elements=button,dialog&helpers=form">
<button class="chip">Beer CSS inside Web Component!</button>
</beer-css>
β οΈ Important Rules: Order Matters!
The cascade still applies to modular CSS. When importing manually, follow these ordering practices:
β Do:
Import tiers strictly in order: Settings β Helpers β Elements. Import within each tier in alphabetical order to avoid unexpected CSS specificity overrides. Import settings/all.css rather than trying to handpick partial configuration files.
π« Don't:
Mix your import order (e.g., loading Elements before Settings). Import incomplete settings like only importing dark.css without base settings.
π§ββοΈ Vite Pro Tip - If you're using Vite, update your vite.config.js:
export default {
build: {
assetsInlineLimit: 0, // Keeps the original size of CSS files from inlining into JS chunks
},
};
Setting assetsInlineLimit: 0 keeps individual CSS assets cleanly split and prevents unwanted base64 bundling. The modular version makes Beer CSS well-suited for high-performance sites where keeping network transfer size low is critical.
πͺΆ Although the framework is already extremely lightweight, you can make it up to 10 times lighter.
Donβt load the entire puzzle if you only need one piece. With the modular version of Beer CSS, you can use components starting at 1.4 KB (or the full framework for just 14.5 KB).
Top comments (0)