learnxinyminutes-docs/stylus.html.markdown

228 lines
4.3 KiB
Markdown
Raw Normal View History

---
language: stylus
filename: learnStylus.styl
contributors:
- ["Salomão Neto", "https://github.com/salomaosnff"]
- ["Isaac Henrique", "https://github.com/Isaachi1"]
2019-01-10 13:46:34 +03:00
translators:
- ["Divay Prakash", "https://github.com/divayprakash"]
---
2019-01-10 13:46:34 +03:00
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.
```scss
/* 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
2019-01-10 14:34:30 +03:00
/* Getting parent element reference
2019-01-10 13:46:34 +03:00
==============================*/
a {
color: #0088dd;
&:hover {
color: #DD8800;
}
}
2019-01-10 14:34:30 +03:00
/* Variables
2019-01-10 13:46:34 +03:00
==============================*/
2019-01-10 14:34:30 +03:00
/*
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.
2019-01-10 13:46:34 +03:00
*/
$primary-color = #A3A4FF
$secondary-color = #51527F
$body-font = 'Roboto', sans-serif
2019-01-10 14:34:30 +03:00
/* You can use variables throughout your style sheet.
Now, if you want to change the color, you only have to make the change once. */
2019-01-10 13:46:34 +03:00
body
background-color $primary-color
color $secondary-color
font-family $body-font
2019-01-10 14:34:30 +03:00
/* After compilation: */
2019-01-10 13:46:34 +03:00
body {
background-color: #A3A4FF;
color: #51527F;
font-family: 'Roboto', sans-serif;
}
2019-01-10 14:34:30 +03:00
/ *
This is much easier to maintain than having to change color
each time it appears throughout your style sheet.
2019-01-10 13:46:34 +03:00
* /
2019-01-10 14:34:30 +03:00
/* Mixins
2019-01-10 13:46:34 +03:00
==============================*/
2019-01-10 14:34:30 +03:00
/* 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.
2019-01-10 13:46:34 +03:00
center()
display block
margin-left auto
margin-right auto
left 0
right 0
2019-01-10 14:34:30 +03:00
/* Using the mixin */
2019-01-10 13:46:34 +03:00
body {
center()
background-color: $primary-color
}
2019-01-10 14:34:30 +03:00
/* After compilation: */
2019-01-10 13:46:34 +03:00
div {
display: block;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
background-color: #A3A4FF;
}
2019-01-10 14:34:30 +03:00
/* You can use mixins to create a shorthand property. */
2019-01-10 13:46:34 +03:00
size($width, $height)
width $width
height $height
.rectangle
size(100px, 60px)
.square
size(40px, 40px)
2019-01-10 14:34:30 +03:00
/* You can use a mixin as a CSS property. */
2019-01-10 13:46:34 +03:00
circle($ratio)
width $ratio * 2
height $ratio * 2
border-radius $ratio
.ball
circle 25px
2019-01-10 14:34:30 +03:00
/* Interpolation
2019-01-10 13:46:34 +03:00
==============================*/
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
2019-01-10 14:37:51 +03:00
2019-01-10 14:34:30 +03:00
/* Functions
2019-01-10 13:46:34 +03:00
==============================*/
2019-01-10 14:34:30 +03:00
/* Functions in Stylus allow you to perform a variety of tasks, such as recalling some data. */
2019-01-10 13:46:34 +03:00
body {
2019-01-10 14:34:30 +03:00
background darken(#0088DD, 50%) // Dim color #0088DD by 50%
2019-01-10 13:46:34 +03:00
}
2019-01-10 14:34:30 +03:00
/* Creating your own function */
add(a, b)
2019-01-10 13:46:34 +03:00
a + b
body
2019-01-10 14:34:30 +03:00
padding add(10px, 5)
2019-01-10 13:46:34 +03:00
2019-01-10 14:37:51 +03:00
2019-01-10 14:34:30 +03:00
/* Conditions
2019-01-10 13:46:34 +03:00
==============================*/
2019-01-10 14:34:30 +03:00
compare(a, b)
2019-01-10 13:46:34 +03:00
if a > b
2019-01-10 14:34:30 +03:00
bigger
2019-01-10 13:46:34 +03:00
else if a < b
2019-01-10 14:34:30 +03:00
smaller
2019-01-10 13:46:34 +03:00
else
2019-01-10 14:34:30 +03:00
equal
2019-01-10 13:46:34 +03:00
2019-01-10 14:34:30 +03:00
compare(5, 2) // => bigger
compare(1, 5) // => smaller
compare(10, 10) // => equal
2019-01-10 13:46:34 +03:00
2019-01-10 14:37:51 +03:00
2019-01-10 14:34:30 +03:00
/* Iterations
2019-01-10 13:46:34 +03:00
==============================*/
2019-01-10 14:34:30 +03:00
/*
Repeat loop syntax for:
2019-01-10 13:46:34 +03:00
for <val-name> [, <key-name>] in <expression>
2019-01-10 14:34:30 +03:00
*/
2019-01-10 13:46:34 +03:00
2019-01-10 14:34:30 +03:00
for $item in (1..2) /* Repeat block 12 times */
2019-01-10 13:46:34 +03:00
.col-{$item}
2019-01-10 14:34:30 +03:00
width ($item / 12) * 100% /* Calculate row by column number */
2019-01-10 13:46:34 +03:00
```
2019-01-10 14:34:30 +03:00
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.