learnxinyminutes-docs/stylus.html.markdown

228 lines
4.3 KiB
Markdown
Raw Normal View History

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