Stylus is a powerful CSS preprocessor that simplifies and enhances the process of writing stylesheets. It offers a clean, expressive syntax and robust features to make CSS development more efficient and enjoyable.
Stylus offers a flexible syntax that allows developers to write CSS with minimal punctuation. Here's a basic example:
// Stylus syntax
body
font 14px Arial, sans-serif
background #f0f0f0
.container
width 80%
margin 0 auto
This Stylus code compiles to standard CSS:
body {
font: 14px Arial, sans-serif;
background: #f0f0f0;
}
.container {
width: 80%;
margin: 0 auto;
}
Stylus makes it easy to define and use variables, enhancing code maintainability:
// Define variables
primary-color = #3498db
secondary-color = #2ecc71
// Use variables
.button
background-color primary-color
color #fff
&:hover
background-color secondary-color
Mixins allow you to create reusable blocks of CSS properties:
border-radius(n)
-webkit-border-radius n
-moz-border-radius n
border-radius n
.box
border-radius(5px)
To begin using Stylus, you'll need to install it via npm:
npm install stylus -g
Once installed, you can compile Stylus files to CSS using the command line:
stylus input.styl -o output.css
By mastering Stylus basics, you can significantly improve your CSS workflow and create more maintainable stylesheets. As you become more comfortable with Stylus, explore its advanced features to further optimize your development process.