DEV Community

Cover image for ⚡ Creating Custom Functions in FSCSS (Yes, CSS Can Do This)
FSCSS
FSCSS

Posted on

⚡ Creating Custom Functions in FSCSS (Yes, CSS Can Do This)

One thing about FSCSS is that you can define your own functions… inside your styles. Here’s a simple example of how that works. 👇

Custom randint Function

@define randint(array){
@random(@use(array)) 
} 
Enter fullscreen mode Exit fullscreen mode

What this function does:

  • Takes an array as an argument.
  • Returns a random value directly from that array.

Step 1: Defining Arrays
First, we set up our possible values:

@arr sizes[ 100px, 150px, 200px ] 
Enter fullscreen mode Exit fullscreen mode

Step 2: Using the Function
We call our custom function and assign it to a variable:

$height: @randint(@arr.sizes); 
Enter fullscreen mode Exit fullscreen mode

Now $height dynamically becomes either 100px, 150px, or 200px (randomly selected at compile).

Why This Is Interesting

  • Reusable Logic: You can create complex logic once and use it across your entire design system.
  • Dynamic Design Systems: Build layouts that feel organic and varied rather than static and repetitive.

Example Usage

Applying that logic to a class is straightforward:

.box { 
  height: $height; 
  width: $height; 
  background: #2c5364; 
} 
Enter fullscreen mode Exit fullscreen mode

Every compile can produce a slightly different UI!

Top comments (0)