cheatsheets/stylus.md

465 lines
6.7 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
2012-10-11 08:13:19 +00:00
title: Stylus
2015-11-24 04:32:36 +00:00
category: CSS
2017-09-20 13:53:39 +00:00
prism_languages: [stylus]
2017-09-20 13:56:46 +00:00
weight: -3
2020-07-04 13:33:09 +00:00
updated: 2017-10-30
2017-10-29 16:18:27 +00:00
tags: [Featurable]
2013-10-14 02:36:58 +00:00
---
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
Getting started
---------------
{: .-three-column}
### CSS syntax
```stylus
.box {
color: blue;
.button {
color: red;
}
}
```
Stylus is a CSS pre-processor.
See: [stylus-lang.com](http://stylus-lang.com/)
### Indent syntax
```stylus
.box
color: blue
.button
color: red
```
Also works! The colon is optional, as well. This is typically the syntax used with Stylus documents.
### Mixins
```stylus
caps-type()
text-transform: uppercase
letter-spacing: 0.05em
```
{: data-line="1"}
```stylus
h5
caps-type()
```
{: data-line="2"}
See [Mixins](#mixins-1) below.
2012-10-11 08:13:19 +00:00
### Variables
2017-09-20 13:53:39 +00:00
```stylus
royal-blue = #36a
```
{: data-line="1"}
```stylus
div
color: royal-blue
```
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
Mixins
------
{: .-three-column}
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
### Without arguments
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
```stylus
red-border()
border: solid 2px red
```
{: data-line="1"}
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
```stylus
div
red-border()
```
{: data-line="2"}
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
See: [Mixins](http://stylus-lang.com/docs/mixins.html)
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
### With arguments
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
```stylus
border-radius(n)
-webkit-border-radius: n
border-radius: n
```
{: data-line="1"}
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
```stylus
div
border-radius: 2px
border-radius(2px)
```
{: data-line="2,3"}
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
Mixins can be applied in two different ways.
### Argument defaults
```stylus
2017-09-20 13:54:41 +00:00
border-radius(n = 2px)
2017-09-20 13:53:39 +00:00
-webkit-border-radius: n
```
2017-09-20 13:54:41 +00:00
{: data-line="1"}
2017-09-20 13:53:39 +00:00
### Block mixins
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
```stylus
mobile()
@media (max-width: 480px)
{block}
```
{: data-line="3"}
```stylus
+mobile()
width: 10px
```
{: data-line="1"}
See: [Block mixins](http://stylus-lang.com/docs/mixins.html#block-mixins)
### Rest params
```stylus
shadow(offset-x, args...)
box-shadow: offset-x args
margin-top: offset-x
```
{: data-line="1"}
```stylus
#login
shadow: 1px 2px 5px #eee
```
See: [Rest params](http://stylus-lang.com/docs/vargs.html)
Functions
---------
{: .-three-column}
2012-10-11 08:13:19 +00:00
### Functions
2017-09-20 13:53:39 +00:00
```stylus
add(a, b)
a + b
```
{: data-line="1"}
```stylus
body
padding: add(10px, 5)
```
{: data-line="2"}
See: [Functions](http://stylus-lang.com/docs/functions.html)
### Argument defaults
```stylus
add(a, b = 2)
a + b
```
{: data-line="1"}
See: [Argument defaults](http://stylus-lang.com/docs/functions.html#argument-defaults)
### Named parameters
```stylus
shadow(x, y)
x y (y * 1.5) #000
```
```stylus
.button
box-shadow: shadow(x: 2, y: 4)
```
{: data-line="2"}
See: [Named parameters](http://stylus-lang.com/docs/functions.html#named-parameters)
### Multiple return values
```stylus
sizes()
8px 16px
```
{: data-line="2"}
```stylus
sizes()[0] // → 8px
sizes()[1] // → 16px
```
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
See: [Multiple return values](http://stylus-lang.com/docs/functions.html#multiple-return-values)
Values
------
{: .-three-column}
### Conditional assignment
```stylus
royal-blue = #36a
royal-blue ?= #89f
```
{: data-line="2"}
```stylus
div
color: royal-blue // #36a
```
`?=` will only set a variable if it's previously unset.
See: [Conditional assignment](https://stylus-lang.com/docs/operators.html#conditional-assignment--)
2017-09-20 13:53:39 +00:00
### Property lookup
```stylus
.logo
width: w = 150
margin-left: -(w / 2)
// or
height: 80px
margin-top: -(@height / 2)
2017-09-20 13:53:39 +00:00
```
{: data-line="2,3"}
See: [Property lookup](https://stylus-lang.com/docs/variables.html#property-lookup)
2012-10-11 08:13:19 +00:00
### Interpolation
2017-09-20 13:53:39 +00:00
```stylus
-{prefix}-border-radius: 2px
```
See: [Interpolation](https://stylus-lang.com/docs/interpolation.html)
2012-10-11 08:13:19 +00:00
2014-03-22 04:03:13 +00:00
### Color operators
2017-09-20 13:53:39 +00:00
```stylus
2017-09-20 13:54:41 +00:00
#888 + 50% // → #c3c3c3 (lighten)
#888 - 50% // → #444 (darken)
#f00 + 50deg // → #ffd500 (hue)
2017-09-20 13:53:39 +00:00
```
2014-03-22 04:03:13 +00:00
2014-09-24 09:09:41 +00:00
### Casting
2017-09-20 13:53:39 +00:00
```stylus
n = 5px
```
```stylus
foo: (n)em
foo: (n * 5)%
```
{: data-line="1,2"}
2014-09-24 09:09:41 +00:00
### Lookup
2017-09-20 13:53:39 +00:00
```stylus
light-blue = #3bd
name = 'blue'
lookup('light-' + name)
```
{: data-line="3"}
2014-09-24 09:09:41 +00:00
See: [lookup](https://stylus-lang.com/docs/bifs.html#lookupname)
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
Advanced features
-----------------
{: .-three-column}
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
### Conditional
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
```stylus
if color == blue
display: block
else if true and true
display: inline
else if 'hey' is not 'bye'
display: flex
else
display: none
```
2015-03-05 06:39:18 +00:00
2017-09-20 13:53:39 +00:00
Aliases:
2015-03-05 06:39:18 +00:00
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
| `==` | `is` |
| `!=` | `is not` |
| `!=` | `isnt` |
2015-03-05 06:39:18 +00:00
See: [Conditionals](https://stylus-lang.com/docs/functions.html#conditionals)
2015-03-05 06:39:18 +00:00
2017-09-20 13:53:39 +00:00
### For loops
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
```stylus
font-size-1 = 10px
font-size-2 = 20px
font-size-3 = 30px
2015-03-05 06:39:18 +00:00
2017-09-20 13:53:39 +00:00
for i in 1..3
.text-{i}
font-size: lookup('font-size-' + i)
```
{: data-line="5"}
2015-03-05 06:39:18 +00:00
2017-09-20 13:53:39 +00:00
### Definition check
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
```stylus
if ohnoes is defined
color: blue
```
{: data-line="1"}
2012-10-11 08:13:19 +00:00
See: [is defined](https://stylus-lang.com/docs/operators.html#variable-definition-is-defined)
2012-10-11 08:13:19 +00:00
2017-09-20 13:53:39 +00:00
### False values
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
```stylus
0
null
false
''
```
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
### Type check
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
```stylus
if val is a 'string'
if val is a 'ident'
2017-09-20 13:54:41 +00:00
if #fff is a 'rgba' // → true
2017-09-20 13:53:39 +00:00
```
2014-09-24 09:09:41 +00:00
See: [Instance check](https://stylus-lang.com/docs/operators.html#instance-check-is-a)
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
Built-in functions
------------------
{: .-three-column}
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
### Color functions
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
```stylus
2017-09-20 13:54:41 +00:00
alpha(#fff) //→ 1
alpha(rgba(0, 0, 0, 0.2)) //→ 0.2
2017-09-20 13:53:39 +00:00
```
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
```stylus
2017-09-20 13:54:41 +00:00
dark(black) //→ true
light(black) //→ false
2017-09-20 13:53:39 +00:00
```
```stylus
2017-09-20 13:54:41 +00:00
hue(#0a0) //→ 50deg
saturation(#f00) //→ 100%
lightness(#f00) //→ 50%
luminosity(#f00) //→ 0.2126
2017-09-20 13:53:39 +00:00
```
```stylus
hue(#0a0, 0deg)
saturation(#f00, 50%)
lightness(#f00)
```
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
```stylus
lighten(color, 10%)
darken(color, 10%)
saturate(color, 10%)
desaturate(color, 10%)
invert(color)
```
```stylus
tint(color, 50%) // mix with white
shade(color, 50%) // mix with black
```
```stylus
unquote(string)
```
See: [Built-in functions](http://stylus-lang.com/docs/bifs.html)
### Image size
```stylus
width: image-size('tux.png')[0]
height: image-size('tux.png')[1]
```
Returns the width and height of a given image.
See: [image-size](http://stylus-lang.com/docs/bifs.html#image-sizepath)
### Caching
```stylus
size($width)
+cache('w' + $width)
width: $width
.a { size: 10px }
.b { size: 10px }
```
```stylus
// yields: .a, b { width: 10px }
```
Applies its contents to the given selector on the first call, but would @extend the first calls selector at the second call with the same params.
See: [cache](http://stylus-lang.com/docs/bifs.html#cachekeys)
### Add Property
```stylus
gradient(color)
add-property('background-image', linear-gradient(top, color, darken(color, 20%)))
color
```
```stylus
body
background: gradient(red)
```
See: [add-property](http://stylus-lang.com/docs/bifs.html#add-propertyname-expr)
2014-09-24 09:09:41 +00:00
### sprintf
2017-09-20 13:53:39 +00:00
```stylus
'-webkit-gradient(%s, %s, %s)' % (linear (0 0) (0 100%))
2017-09-20 13:54:41 +00:00
// → -webkit-gradient(linear, 0 0, 0 100%)
2017-09-20 13:53:39 +00:00
```
```stylus
s("rgba(0, 0, 0, %s)", 0.3)
```
See: [s](http://stylus-lang.com/docs/bifs.html#sfmt-)
### Embed URL
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
```
background: embedurl('logo.png')
2017-09-20 13:54:41 +00:00
// → background: url("data:image/png;base64,…")
2017-09-20 13:53:39 +00:00
```
2014-09-24 09:09:41 +00:00
2017-09-20 13:53:39 +00:00
See: [embedurl](http://stylus-lang.com/docs/bifs.html#embedurlpath-encoding)