Stylus Basics: Streamlining CSS Development
Learn CSS through interactive, bite-sized lessons. Style beautiful websites and master modern CSS techniques.
Start CSS Journey →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.
Key Features of Stylus
- Optional use of colons, semicolons, and braces
- Variables and mixins for reusable code
- Nested selectors for improved readability
- Built-in functions and operations
- Seamless integration with Node.js
Stylus Syntax
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;
}
Variables in Stylus
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 for Reusable Styles
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)
Advantages of Using Stylus
- Cleaner, more readable code
- Improved productivity with less typing
- Easy integration with existing CSS
- Powerful features like functions and loops
- Compatibility with CSS Preprocessor Variables and CSS Preprocessor Mixins
Getting Started with Stylus
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
Best Practices
- Maintain consistent indentation for nested selectors
- Use variables for colors, sizes, and other repeated values
- Leverage mixins for cross-browser compatibility
- Organize your Stylus files modularly
- Consider using Stylus in conjunction with CSS Framework Customization for enhanced flexibility
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.