Stylus Mixins and Functions
Short answer: in Stylus, mixins and functions share the same syntax. A definition like name(args) can either output properties (used as a mixin) or return a value (used as a function). You can even call a mixin without parentheses, as if it were a property.
A simple mixin
center()
display flex
justify-content center
align-items center
.hero
center()
min-height 60vh
Calling center() inserts the properties into the rule.
Mixins with arguments and defaults
button(bg, color = white, radius = 6px)
background bg
color color
border-radius radius
padding .5rem 1rem
.btn-primary
button(#2563eb)
.btn-danger
button(#dc2626, white, 20px)
Transparent mixins
Stylus lets you call a mixin like a property, with no parentheses and no colon:
size(w, h = w)
width w
height h
.avatar
size 48px // width: 48px; height: 48px
size 100px 60px // width: 100px; height: 60px
This is handy for creating your own shorthand properties.
Vendor prefix helper
vendor(prop, args)
-webkit-{prop} args
-moz-{prop} args
{prop} args
border-radius()
vendor('border-radius', arguments)
.card
border-radius 8px
The special arguments variable holds every argument passed to a mixin. Today most prefixes are handled by tools such as Autoprefixer, so use helpers like this only when needed.
Functions that return values
rem(px, base = 16)
(px / base)rem
h1
font-size rem(32) // 2rem
double(n)
return n * 2
.box
padding double(8px) // 16px
A function returns the value of its last expression, or you can use return explicitly.
Conditionals and loops in functions
contrast(bg)
if lightness(bg) > 50%
return #111
else
return #fff
.badge
background #1e293b
color contrast(#1e293b) // #fff
Block mixins
breakpoint(width)
@media (min-width: width)
{block}
.container
padding 1rem
+breakpoint(768px)
padding 2rem
The + prefix passes an indented block to the mixin, which receives it as {block}. This is a clean way to write media queries.
@extend
.message
padding 1rem
border 1px solid #ccc
.error
@extend .message
border-color red
Use @extend to share rules by combining selectors. Placeholder selectors ($name) avoid outputting the base rule itself.
Built-in functions worth knowing
| Function | Use |
|---|---|
lighten(c, 10%) / darken(c, 10%) | Adjust color lightness |
rgba(c, 0.5) | Add transparency to a color |
unit(5, px) | Set or remove units |
percentage(0.25) | Turn a fraction into a percentage |
lookup('@width') | Read a property or variable by name |
Best practices
- Keep mixins small and descriptive.
- Prefer plain properties when a mixin adds no real value.
- Store helpers in separate files and import them where needed.
Frequently asked questions
What is the difference between a mixin and a function in Stylus?
They share the same syntax. A definition that outputs properties acts like a mixin, while one that returns a value acts like a function. The way you call it decides the behavior.
Can Stylus mixins accept default arguments?
Yes. Give a parameter a default value with an equals sign, for example size = 10px, and callers can omit it.
Are Stylus mixins hard to debug?
The flexible syntax can hide mistakes, so use clear names and consistent formatting. The warn() and error() built-ins help show values and stop on invalid input.
Next steps
Read Stylus syntax, then compare the Sass approach in SCSS mixins and functions.
