Stylus Syntax Explained
Short answer: Stylus lets you write CSS with or without braces, colons and semicolons, using indentation for structure. Variables use plain assignment (size = 14px), nesting uses indentation, and & refers to the parent selector.
Flexible syntax
// Full CSS syntax is valid Stylus
body {
font-size: 14px;
}
// Indentation instead of braces
body
font-size: 14px
// Indentation, no colon or semicolon
body
font-size 14px
You can mix these styles, but agree on one convention in a team. The most common choice is indentation with no braces or semicolons.
Variables
primary = #2563eb
spacing = 16px
font-stack = system-ui, sans-serif
body
font-family font-stack
padding spacing
.link
color primary
You can also use a $ prefix ($primary = ...) to make variables stand out. Variables can hold numbers, colors, strings, lists and hashes.
Property lookup
.box
width 200px
margin-left -(@width / 2) // refers to this block's width
Stylus can reference another property in the same block with the @ prefix, which avoids repeating values.
Nesting and the parent reference
.nav
display flex
li
margin-right 1rem
a
color primary
&:hover
text-decoration underline
The & symbol is the parent selector, as in Sass and Less. Use ^[0] and ~/ for more advanced selector references.
Interpolation
vendor(prop, args)
-webkit-{prop} args
{prop} args
.box
vendor(border-radius, 6px)
for name in primary danger
.text-{name}
color red
Curly braces insert values into selectors and property names.
Operators and math
.col
width (100% / 3)
padding (spacing * 2)
margin -(spacing / 2)
Parentheses help avoid ambiguity, particularly with division and negative numbers.
Comments
// Single-line comment (not in the output)
/* Multi-line comment (kept in the output) */
/*! Important comment (kept even when minified) */
Importing files
@import 'variables'
@import 'mixins/*' // glob: import every file in a folder
Conditionals and loops
size = 3
.item
if size > 2
font-size 20px
else
font-size 14px
for i in 1..3
.m-{i}
margin (i * 4px)
Pitfalls of the flexible syntax
- Omitting colons can make a property look like a mixin call or a selector. When in doubt, add the colon.
- Mixed tabs and spaces in indentation produce errors.
- Unparenthesized math can be interpreted in unexpected ways.
Tips
- Pick one syntax style and enforce it with a linter such as stylint.
- Use meaningful variable names and group them in a settings file.
- Check compiled CSS from time to time.
Frequently asked questions
Should I use braces in Stylus?
You can, and it can help when moving from CSS or sharing code with people who dislike indentation-only syntax. Consistency matters most, so pick a style and apply it across the project.
What is property lookup in Stylus?
It lets you reference another property value in the same block with the @ prefix, for example width: 100px; height: @width, so you avoid declaring a variable.
What causes the most Stylus errors?
Inconsistent indentation, mixing tabs and spaces, and ambiguous lines where a property could be read as a function call. A linter and an editor setting for spaces help.
Next steps
Continue with mixins and functions and the overview in what Stylus is.
