FSCSS: turn an array into a gradient in one line with .prefix

FSCSS: turn an array into a gradient in one line with .prefix

●1 ●31 ●67
calendar_today ago • schedule1 min read
— Originally published at dev.to

You need a multi-stop linear-gradient from a list of numbers. Each stop should look like #AFA120, #AFA334, …

There are two ways to get there in FSCSS.


The long way

Import a helper, build an index array, loop inside inline(), and index back into the data:

@import((mirror as make-index) from fscss:micros)

@arr gradient[120, 334, 676, 456, 679, 111, 056, 561]
@make-index(gradient, index)

.card {
  size: 200px;
  bg: linear-gradient(
    inline(`
      empty{ }
      @arr.index[] {
        #*Emails are not allowed*ient[@arr.index[]],
      }
    `)
    #f0ff0f
  );
}

That works. It’s also a lot of ceremony for “stick #AFA in front of every item.”


The short way

Array methods already know how to map a prefix onto every value:

@arr gradient[120, 334, 676, 456, 679, 111, 056, 561]

.color-box {
  size: 200px;
  bg: linear-gradient(@arr.gradient!.prefix(#AFA));
}

One expression. No import, no index array, no inline() scaffolding.


What comes out

Long path (with the extra end stop):

.card {
  width: 200px;
  height: 200px;
  background: linear-gradient(
    #AFA120,
    #AFA334,
    #AFA676,
    #AFA456,
    #AFA679,
    #AFA111,
    #AFA056,
    #AFA561,
    #f0ff0f
  );
}

Short path:

.color-box {
  width: 200px;
  height: 200px;
  background: linear-gradient(
    #AFA120,
    #AFA334,
    #AFA676,
    #AFA456,
    #AFA679,
    #AFA111,
    #AFA056,
    #AFA561
  );
}

Same idea: a list of numbers becomes a list of hex-ish stops. The method does the mapping at compile time.


Why .prefix() fits here

Piece Role
@arr gradient[...] Source values
! Method mode
.prefix(#AFA) Prepend #AFA to each item
Result Comma-ready list inside linear-gradient(...)

You still use loops and mirror when the mapping is structural (selectors, dual-pass polygons, parallel index arrays). For “decorate every value the same way,” methods like .prefix(), .unit(), .surround(), and .join() are the right tool.


Mental model

  • Transform values → array methods (.prefix, .unit, .join, …)
  • Transform structure / selectors → @arr.name[] loops, mirror, inline()
  • Prefer the smallest tool that still compiles to clear CSS

Going from a multi-block import + index + inline loop down to:

bg: linear-gradient(@arr.gradient!.prefix(#AFA));

is exactly that trade.



Figured list in, standard gradient out.

Want more #frontend & #showdev deep dives?

Join 6,170+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

I Wrote a Script to Fix Audible's Unreadable PDF Filenames

snapsynapseverified - Apr 20

Stop Turning Every REST Endpoint into an MCP Tool: A Better Approach to AI Tool Design

zeekwrites - Nov 30

StarlingX 13.0 Turns Two Edge Upgrades Into One Reboot. Test the Targets Yourself.

Tom Smithverified - Nov 24

Layout Grids in CSS: Designing with Millimeter Precision Before Writing Code

Joemetry - Sep 22
chevron_left
1.9k Points • 99 Badges
31Posts
20Comments
15Connections
A full-stack web developer: Building smart solutions, designing meaningful experiences, and continuo... Show more

Related Jobs

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!