Introduction
Sass (Syntactically Awesome Stylesheets) is a CSS preprocessor that adds power and elegance to the basic language. It allows you to use variables, nested rules, mixins, functions, and more, all with a fully CSS-compatible syntax.
Key Concepts
- Variables
Variables in Sass allow you to store values that you can reuse throughout your stylesheet. This makes it easier to manage and update your CSS.
// Define variables $primary-color: #3498db; $font-stack: Helvetica, sans-serif; // Use variables body { font: 100% $font-stack; color: $primary-color; }
- Nesting
Sass allows you to nest your CSS selectors in a way that follows the same visual hierarchy of your HTML.
nav { ul { margin: 0; padding: 0; list-style: none; } li { display: inline-block; } a { display: block; padding: 6px 12px; text-decoration: none; } }
- Partials and Import
Partials are Sass files that are meant to be included in other Sass files. They help you modularize your CSS.
// _reset.scss * { margin: 0; padding: 0; box-sizing: border-box; } // main.scss @import 'reset'; body { font-family: $font-stack; color: $primary-color; }
- Mixins
Mixins allow you to create reusable chunks of CSS that you can include in other selectors.
@mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; -ms-border-radius: $radius; border-radius: $radius; } .box { @include border-radius(10px); }
- Functions
Sass functions allow you to create reusable pieces of code that return values.
@function calculate-rem($size) { @return $size / 16px * 1rem; } body { font-size: calculate-rem(18px); }
- Extends
The @extend
directive lets you share a set of CSS properties from one selector to another.
.message { border: 1px solid #ccc; padding: 10px; color: #333; } .success { @extend .message; border-color: green; } .error { @extend .message; border-color: red; }
Practical Example
Let's create a simple Sass file that demonstrates the use of variables, nesting, mixins, and partials.
Step 1: Create Partials
_variables.scss
_mixins.scss
Step 2: Main Sass File
main.scss
@import 'variables'; @import 'mixins'; body { font-family: $font-stack; color: $primary-color; } .container { @include flex-center; height: 100vh; background-color: $secondary-color; .content { text-align: center; padding: 20px; background: white; border-radius: 10px; } }
Exercise
Task
Create a Sass file that uses variables, nesting, mixins, and partials to style a simple webpage with a header, main content area, and footer.
Solution
_variables.scss
_mixins.scss
main.scss
@import 'variables'; @import 'mixins'; body { font-family: $font-stack; color: $primary-color; margin: 0; padding: 0; } header, footer { background-color: $primary-color; color: white; padding: 20px; text-align: center; } main { @include flex-center; height: calc(100vh - 160px); // Adjust height considering header and footer background-color: $secondary-color; .content { text-align: center; padding: 20px; background: white; border-radius: 10px; } }
Conclusion
In this lesson, you learned the basics of Sass, including variables, nesting, partials, mixins, functions, and extends. These features make your CSS more maintainable and scalable. Practice using these features in your projects to become proficient in Sass.
CSS Mastery: From Beginner to Advanced
Module 1: Introduction to CSS
- What is CSS?
- CSS Syntax and Selectors
- How to Add CSS to HTML
- Basic CSS Properties
- CSS Colors
- CSS Units and Measurements
Module 2: Text and Font Styling
- Text Properties
- Font Properties
- Google Fonts Integration
- Text Alignment and Spacing
- Text Decoration and Transformation
Module 3: Box Model and Layout
- Understanding the Box Model
- Margin and Padding
- Border and Outline
- Width and Height
- Box Sizing
- CSS Display Property
Module 4: Positioning and Floating
- CSS Position Property
- Static, Relative, Absolute, and Fixed Positioning
- CSS Float and Clear
- Creating Layouts with Float
- CSS Z-Index
Module 5: Flexbox
- Introduction to Flexbox
- Flex Container Properties
- Flex Item Properties
- Creating Layouts with Flexbox
- Responsive Design with Flexbox
Module 6: CSS Grid
- Introduction to CSS Grid
- Grid Container Properties
- Grid Item Properties
- Creating Layouts with CSS Grid
- Responsive Design with CSS Grid
Module 7: Advanced CSS Techniques
Module 8: Responsive Design
- Introduction to Responsive Design
- Media Queries
- Responsive Typography
- Responsive Images
- Mobile-First Design
Module 9: Preprocessors and Frameworks
- Introduction to CSS Preprocessors
- Sass Basics
- Less Basics
- Introduction to CSS Frameworks
- Using Bootstrap