Sass Site Documentation

repository·main·Indexed 18 days ago

https://github.com/sass/sass-site

The open-source repository for the official Sass website. This documentation covers the site's development environment using Docker Compose, the Eleventy static site generation configuration, ESLint rules, and a collection of technical blog posts detailing Sass features such as the @extend algorithm, Maps, selector functions, and interpolation behavior.

Tokens
101.9K
Snippets
407
Records
537
Agent score
62%

What's inside sass-site

  1. Overview of Sass At-Rules

    main

    Sass extends standard CSS functionality by adding several custom at-rules. These rules allow for modularity, logic, and debugging capabilities that are not available in plain CSS.

    Module System

    • @use: Loads mixins, functions, and variables from other Sass stylesheets and combines CSS from multiple stylesheets.
    • @forward: Loads a Sass stylesheet and makes its members (mixins, functions, variables) available to the stylesheet that subsequently @usees it.
    • @import: (Legacy) Extends the CSS at-rule to load styles, mixins, functions, and variables from other stylesheets.

    Style Reusability and Inheritance

    • @mixin and @include: Used to define and re-use chunks of styles.
    • @extend: Allows selectors to inherit styles from other selectors.

    Logic and Functions

    • @function: Defines custom functions for use in SassScript expressions.
    • Flow Control: Rules like @if, @each, @for, and @while control the conditional emission or repetition of styles.

    Debugging and Error Handling

    • @error: Stops compilation and outputs an error message.
    • @warn: Prints a warning message but allows compilation to continue.
    • @debug: Prints a message for debugging purposes.

    Scoping

    • @at-root: Forces styles within the rule to be placed at the root of the CSS document, bypassing nesting.
  2. Supported color formats in Sass

    main

    Sass supports various ways to define colors, including hex codes, CSS color names, and color functions.

    • Hex codes: #f2ece4 or #b37399aa (supports alpha channel).
    • CSS color names: midnightblue, transparent.
    • Color functions: rgb(), lab(), color(), etc.

    Note: LibSass, Ruby Sass, and older versions of Dart Sass do not support hex colors with an alpha channel or color spaces other than rgb and hsl.

    @debug #f2ece4; // #f2ece4
    @debug #b37399aa; // rgba(179, 115, 153, 67%)
    @debug midnightblue; // #191970
    @debug rgb(204 102 153); // #c69
    @debug lab(32.4% 38.4 -47.7 / 0.7); // lab(32.4% 38.4 -47.7 / 0.7)
    @debug color(display-p3 0.597 0.732 0.576); // color(display-p3 0.597 0.732 0.576)
  3. Use Sass flow control rules

    main

    Sass provides four at-rules for flow control. These rules allow you to control whether styles are emitted, emit them multiple times with variations, or write algorithms within @mixins and @functions.

    • @if: Controls whether or not a block of code is evaluated based on a condition.
    • @each: Evaluates a block for every element in a list or every pair in a map.
    • @for: Evaluates a block a specific number of times.
    • @while: Evaluates a block repeatedly until a specific condition is no longer met.
  4. Choose a Sass installation method

    main

    Sass can be installed via several different methods depending on your workflow and operating system:

    Node.js Libraries

    • sass package: A pure JavaScript implementation. It is slower but highly portable and can be installed on any platform that supports Node.js.
    • sass-embedded package: A wrapper around the Dart VM. It is significantly faster but only supports Windows, Mac OS, and Linux.

    Desktop Applications

    For a GUI-based experience, you can use:

    • CodeKit (Paid, Mac)
    • Prepros (Paid, Mac, Windows, Linux)

    Command Line Interface (CLI)

    Installing via the CLI allows you to run the sass executable directly to compile .sass and .scss files to .css files.

  5. How numeric precision works in Sass

    main

    Sass represents numbers internally as 64-bit floating point values. However, it applies a precision limit of 10 digits after the decimal point when serializing to CSS or performing equality checks.

    Key behaviors of the 10-digit precision limit:

    • CSS Output: Only the first ten digits after the decimal point are included in generated CSS.
    • Equality: Comparison operators like == and >= consider numbers equivalent if they match up to the tenth decimal digit.
    • Integer Conversion: If a number is less than 0.0000000001 away from an integer, it is treated as that integer for functions requiring integer arguments (like list.nth()).
    • Lazy Rounding: Math functions use the full internal value to prevent accumulating rounding errors; rounding to 10 digits only happens when precision becomes relevant (e.g., during serialization).
    @debug 0.012345678912345; // 0.0123456789
    @debug 0.01234567891 == 0.01234567899; // true
    @debug 1.00000000009; // 1
    @debug 0.99999999991; // 1
  6. Understand @extend scope and module boundaries

    main

    The scope of an @extend depends on how you load your stylesheets:

    • With @use or @forward: Extensions are scoped to upstream modules. An @extend in a module will only affect style rules in modules that were loaded by that stylesheet (the modules it @uses or @forwards). This makes extensions predictable.
    • With @import: Extensions are global. An @extend will affect every stylesheet that imports the current stylesheet, as well as everything those stylesheets import, creating a wide-reaching side effect.
  7. Use special functions in color constructors

    main

    You can pass special functions like calc() or var() in place of any argument to a global color constructor. You can even use var() in place of multiple arguments. When a color function is called this way, it returns an unquoted string using the same signature it was called with.

    @debug rgb(0 51 102 / var(--opacity)); // rgb(0 51 102 / var(--opacity))
    @debug color(display-p3 var(--peach)); // color(display-p3 var(--peach))
  8. How the Node Package Importer resolves Sass files

    main

    The Node Package Importer follows a specific resolution order to find Sass or CSS files within a dependency. This allows it to work with modern conditional exports as well as older package structures.

    1. Conditional Exports

    It first looks for the sass, style, or default conditions within the exports field of the package's package.json that resolve to a Sass or CSS file.

    2. Root Exports (if no subpath is provided)

    If you are importing the package root (e.g., @use "pkg:library";), it searches the package.json root for:

    1. A sass key.
    2. A style key.
    3. An index file at the package root (resolving for file extensions and partials).

    3. Subpath Resolution

    If a subpath is provided (e.g., @use "pkg:library/utils";), the importer resolves that path relative to the package root, searching for file extensions and partials.

  9. Simplification of calculation expressions

    main

    Sass automatically simplifies calculations whenever possible:

    1. Compile-time math: If adjacent operations use compatible units (e.g., 1in + 10px or 5s * 2), Sass simplifies them.
    2. Single value reduction: If a calculation can be reduced to a single number (e.g., clamp(0px, 30px, 20px)), Sass returns that number (e.g., 20px).
    3. Nested calculations: If a calc() is nested inside another calculation, Sass removes the inner calc() call and replaces it with a plain operation.

    Note for Library Authors: Because a calculation might be simplified into a plain number, use meta.type-of() to check the resulting type if your logic depends on it being a calculation expression.

    $width: calc(400px + 10%);
    
    .sidebar {
      width: $width;
      padding-left: calc($width / 4);
    }
  10. Understand the rollout stages for the slash separator change

    main

    The transition from / as a division operator to / as a list separator is being rolled out in three stages to manage breaking changes:

    Stage 1: Preparation (No breaking changes)

    • divide() function is added (works like / but warns on non-number arguments).
    • slash-list() function is added to create slash-separated lists.
    • Deprecation warnings are issued for all / operations interpreted as division.

    Stage 2: Breaking Change

    • / becomes exclusively a list separator.
    • divide() throws errors for non-number arguments.
    • slash-list() is deprecated.

    Stage 3: Cleanup

    • The slash-list() function is removed (planned for a future major version).
  11. Nest CSS selectors

    main

    Sass allows you to nest your CSS selectors to follow the visual hierarchy of your HTML. This helps organize CSS and improves readability, but avoid overly deep nesting to prevent overly-qualified CSS.

    nav {
      ul {
        margin: 0;
        padding: 0;
        list-style: none;
      }
    
      li { display: inline-block; }
    
      a {
        display: block;
        padding: 6px 12px;
        text-decoration: none;
      }
    }