summaryrefslogtreecommitdiffhomepage
path: root/stylus.html.markdown
blob: 7300dabf15e0c75d64de8591a0ae7e26500455c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
language: stylus
filename: learnStylus.styl
contributors:
  - ["Salomão Neto", "https://github.com/salomaosnff"]
  - ["Isaac Henrique", "https://github.com/Isaachi1"]
translators:
  - ["Divay Prakash", "https://github.com/divayprakash"]
---

Stylus is a dynamic stylesheet preprocessor language that is compiled into CSS. It aims to add functionality to CSS without breaking compatibility across web browsers.
It does this using variables, nesting, mixins, functions and more.

Stylus syntax is very flexible. You can use standard CSS syntax and leave the semicolon (;), colon (:) and even the ({) and (}) optional, making your code even more readable.

Stylus does not provide new style options, but gives functionality that lets you make your CSS much more dynamic.

```sass
/* Code style
==============================*/

/* Keys, semicolon, and colon are optional in Stylus. */

body {
  background: #000;
}

body {
  background: #000
}

body {
  background #000
}

body
  background #000

body
  background: #000;

body
  background: #000

// Single-line comments are removed when Stylus is compiled into CSS.

/* Multi-line comments are preserved. */


/* Selectors
==============================*/

/* Selecting elements within another element */
body {
  background: #000000;
  h1 {
    color: #FF0000;
  }
}

/* Or if you prefer... */
body
  background #000000
  h1
    color #FF0000


/* Getting parent element reference
==============================*/
a {
  color: #0088dd;
  &:hover {
    color: #DD8800;
  }
}


/* Variables
==============================*/


/*
  You can store a CSS value (such as the color) of a variable.
  Although it is optional, it is recommended to add $ before a variable name
  so you can distinguish a variable from another CSS value.
*/

$primary-color = #A3A4FF
$secondary-color = #51527F
$body-font = 'Roboto', sans-serif

/* You can use variables throughout your style sheet.
Now, if you want to change the color, you only have to make the change once. */

body
	background-color $primary-color
	color $secondary-color
	font-family $body-font

/* After compilation: */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}

/ *
This is much easier to maintain than having to change color
each time it appears throughout your style sheet.
* /


/* Mixins
==============================*/

/* If you find that you are writing the same code for more than one
element, you may want to store that code in a mixin.

center()
  display block
	margin-left auto
	margin-right auto
	left 0
	right 0

/* Using the mixin */
body {
  center()
  background-color: $primary-color
}

/* After compilation: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}

/* You can use mixins to create a shorthand property. */

size($width, $height)
  width $width
  height $height

.rectangle
  size(100px, 60px)

.square
	size(40px, 40px)

/* You can use a mixin as a CSS property. */
circle($ratio)
  width $ratio * 2
  height $ratio * 2
  border-radius $ratio

.ball
  circle 25px


/* Interpolation
==============================*/

vendor(prop, args)
  -webkit-{prop} args
  -moz-{prop} args
  {prop} args

border-radius()
  vendor('border-radius', arguments)

box-shadow()
  vendor('box-shadow', arguments)

button
  border-radius 1px 2px / 3px 4px


/* Functions
==============================*/

/* Functions in Stylus allow you to perform a variety of tasks, such as recalling some data. */

body {
  background darken(#0088DD, 50%) // Dim color #0088DD by 50%
}

/* Creating your own function */
add(a, b)
  a + b

body
  padding add(10px, 5)


/* Conditions
==============================*/
compare(a, b)
  if a > b
    bigger
  else if a < b
    smaller
  else
    equal

compare(5, 2)   // => bigger
compare(1, 5)   // => smaller
compare(10, 10) // => equal


/* Iterations
==============================*/

/*
Repeat loop syntax for:
for <val-name> [, <key-name>] in <expression>
*/

for $item in (1..2) /* Repeat block 12 times */
  .col-{$item}
    width ($item / 12) * 100% /* Calculate row by column number */
```

Now that you know a little about this powerful CSS preprocessor, you're ready to create more dynamic style sheets. To learn more, visit the official stylus documentation at http://stylus-lang.com.