Dev.to WebDev 🛠 Dev 👁 0 📖 1 min read

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

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, l

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[] {
        #AFA@arr.gradient[@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.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.