This content originally appeared on DEV Community and was authored by FSCSS tutorial
FSCSS @fun
Guide: Supercharge Your CSS with Modular Magic
Welcome to the world of Figured Shorthand CSS (FSCSS), where the @fun
directive is your secret weapon for crafting clean, reusable, and scalable stylesheets. Think of @fun
as a superpower for defining modular style blocks—variables, properties, or entire design systems—that you can sprinkle throughout your CSS with ease. Ready to level up your styling game? Let’s dive in!
What is @fun
? The Big Picture
The @fun(name){...}
directive lets you create functional blocks—named containers for style values like sizes, colors, or property sets. You can then reference these blocks anywhere in your stylesheet using intuitive dot-notation (e.g., @fun.name.key.value
). This approach keeps your styles organized, reusable, and ridiculously easy to maintain. Whether you’re building a small site or a massive design system, @fun
is your ticket to cleaner code.
Defining Reusable Sizes with @fun(e)
Tired of hardcoding pixel values everywhere? Use @fun(e)
to centralize measurements for spacing, dimensions, or anything else.
@fun(e) {
sm: 10px;
md: 20px;
lg: 30px;
}
Access these values like a pro:
-
@fun.e.sm.value
→10px
-
@fun.e.lg.value
→30px
This keeps your measurements consistent and makes tweaking them a breeze.
Building a Color Palette with @fun(col)
Consistency in colors is a designer’s dream. With @fun(col)
, you can define a palette once and reuse it everywhere.
@fun(col) {
primary: #007bff;
secondary: #6c757d;
accent: #ffc107;
}
Apply them effortlessly:
-
@fun.col.primary.value
→#007bff
(vibrant blue) -
@fun.col.accent.value
→#ffc107
(sunny yellow)
Change a color? Update it in one place, and your entire design stays in sync.
Grouping Properties with @fun(pr)
Got a set of CSS properties you reuse often, like a fancy border or typography combo? Bundle them in a @fun(pr)
block.
@fun(pr) {
card-border: 1px solid #ddd;
card-radius: 8px;
card-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
Use them individually or all at once:
-
@fun.pr.card-border.value
→1px solid #ddd
- Inject the whole block:
@fun.pr
applies all properties in one go.
Applying @fun
in Your Styles
Here’s where the magic happens. Use @fun
values to style elements with precision and flexibility.
body {
background: @fun.col.primary.value; /* Vibrant blue background */
}
.card {
@fun.pr; /* Injects all card properties */
padding: @fun.e.md.value; /* 20px padding */
background: linear-gradient(@fun.col.primary.value, @fun.col.accent.value);
}
This keeps your styles DRY (Don’t Repeat Yourself) and makes updates a snap.
Leveling Up: A Real-World Example
Let’s style a button component using multiple @fun
blocks for maximum flexibility.
@fun(e) {
sm: 8px;
md: 16px;
lg: 24px;
}
@fun(col) {
btn-primary: #007bff;
btn-hover: #0056b3;
}
@fun(pr) {
btn-base: {
border: 1px solid @fun.col.btn-primary.value;
border-radius: @fun.e.sm.value;
padding: @fun.e.md.value @fun.e.lg.value;
}
}
button {
@fun.pr.btn-base; /* Injects border, radius, padding */
background: @fun.col.btn-primary.value;
transition: background 0.3s;
&:hover {
background: @fun.col.btn-hover.value;
}
%2(width, height[:@fun.e.lg.value;]); /* Sets width & height to 24px */
}
What’s Happening Here?
-
Unified Styling: The
@fun.pr.btn-base
block applies a consistent button style in one line. -
Dynamic Colors: The button’s background switches between
btn-primary
andbtn-hover
on interaction. -
FSCSS Shorthand:
%2(width, height[:@fun.e.lg.value;])
sets bothwidth
andheight
to24px
—efficient and readable. -
Scalability: Update
@fun.e.lg
to32px
, and every button adjusts instantly.
Why @fun
is a Developer’s Best Friend
Here’s why @fun
will make you love writing CSS again:
- Modular: Centralize your style tokens for a clean, organized codebase.
- Readable: Dot-notation makes your styles intuitive and self-documenting.
- Reusable: Define once, use everywhere—say goodbye to duplicate code.
- Maintainable: Perfect for design systems, large projects, or quick tweaks without breaking a sweat.
Pro Tips for @fun
Mastery
-
Name Intuitively: Use names like
@fun.spacing
,@fun.colors
, or@fun.typography
to keep things clear. -
Nest with Care: You can nest
@fun
blocks for advanced use cases, but keep it simple to avoid confusion. -
Combine with FSCSS Features: Pair
@fun
with FSCSS shorthands like%2
for maximum efficiency. - Version Control Friendly: Centralized values make git diffs cleaner and collaboration smoother.
Ready to Get Started?
The @fun
directive is your key to writing CSS that’s modular, maintainable, and just plain fun. Whether you’re taming a sprawling design system or keeping a small project tidy, @fun
empowers you to style smarter, not harder. So, grab your editor, define some @fun
blocks, and start building interfaces that scale with ease!
Have questions or want to share your @fun
creations? Drop a post on X or dev community and tag the FSCSS community—we’d love to see your work!
This content originally appeared on DEV Community and was authored by FSCSS tutorial