### SCSS String Indexing Examples Source: https://sass-lang.com/documentation/values/strings Demonstrates using string.index and string.slice with SCSS syntax, noting that indexes start at 1. ```scss @use "sass:string"; @debug string.index("Helvetica Neue", "Helvetica"); // 1 @debug string.index("Helvetica Neue", "Neue"); // 11 @debug string.slice("Roboto Mono", -4); // "Mono" ``` -------------------------------- ### Many-to-Many Mode Examples Source: https://sass-lang.com/documentation/cli/ruby-sass Example invocations showing file-to-file, multiple file, and directory compilation. Output shows status messages and generated .css and .map files. Directory compilation ignores partial files starting with underscore. ```bash $ sass style.scss:style.css write style.css write style.css.map $ sass light.scss:light.css dark.scss:dark.css write light.css write light.css.map write dark.css write dark.css.map $ sass themes:public/css write public/css/light.css write public/css/light.css.map write public/css/dark.css write public/css/dark.css.map ``` -------------------------------- ### math.unit() SCSS example Source: https://sass-lang.com/documentation/modules/math SCSS example using @use 'sass:math' to get unit strings for various numbers. Outputs "", "px", "px*px", and "px/s". ```scss @use 'sass:math'; @debug math.unit(100); // "" @debug math.unit(100px); // "px" @debug math.unit(5px * 10px); // "px*px" @debug math.unit(math.div(5px, 1s)); // "px/s" ``` -------------------------------- ### SCSS opacify and fade-in examples Source: https://sass-lang.com/documentation/modules/color Examples of using opacify() and fade-in() in SCSS. ```scss @debug opacify(rgba(#6b717f, 0.5), 0.2); // rgba(107, 113, 127, 0.7) @debug fade-in(rgba(#e1d7d2, 0.5), 0.4); // rgba(225, 215, 210, 0.9) @debug opacify(rgba(#036, 0.7), 0.3); // #036 ``` -------------------------------- ### CSS output for keyword arguments example Source: https://sass-lang.com/documentation/at-rules/function The resulting CSS after compiling the keyword arguments example. ```css .banner { background-color: #036; color: rgb(4%, 52%, 100%); } ``` -------------------------------- ### color.saturation examples in SCSS Source: https://sass-lang.com/documentation/modules/color SCSS examples showing color.saturation() with different colors and their resulting saturation percentages. ```SCSS @use 'sass:color'; @debug color.saturation(#e1d7d2); // 20% @debug color.saturation(#f2ece4); // 30% @debug color.saturation(#dadbdf); // 7.2463768116% ``` -------------------------------- ### math.percentage usage examples (SCSS) Source: https://sass-lang.com/documentation/modules/math SCSS examples demonstrating math.percentage with a decimal and with a division result. ```scss @use 'sass:math'; @debug math.percentage(0.2); // 20% @debug math.percentage(math.div(100px, 50px)); // 200% ``` -------------------------------- ### Hue function examples Source: https://sass-lang.com/documentation/modules/color Examples of retrieving the hue of a color. ```scss @use 'sass:color'; @debug color.hue(#e1d7d2); // 20deg @debug color.hue(#f2ece4); // 34.2857142857deg @debug color.hue(#dadbdf); // 228deg ``` -------------------------------- ### color.whiteness examples in SCSS Source: https://sass-lang.com/documentation/modules/color SCSS examples showing color.whiteness() with different colors and their resulting whiteness percentages. ```SCSS @use 'sass:color'; @debug color.whiteness(#e1d7d2); // 82.3529411765% @debug color.whiteness(white); // 100% @debug color.whiteness(black); // 0% ``` -------------------------------- ### alpha/opacity SCSS examples Source: https://sass-lang.com/documentation/modules/color Examples of using alpha and opacity in SCSS syntax. ```scss @use 'sass:color'; @debug color.alpha(#e1d7d2); // 1 @debug color.opacity(rgb(210, 225, 221, 0.4)); // 0.4 @debug alpha(opacity=20); // alpha(opacity=20) ``` -------------------------------- ### CSS output for load-css example Source: https://sass-lang.com/documentation/modules/meta The resulting CSS generated by the meta.load-css example. ```css body.dark code { background-color: #6b717f; color: #d2e1dd; border-color: #dadbdf; } ``` -------------------------------- ### CSS output for arbitrary arguments example Source: https://sass-lang.com/documentation/at-rules/function The resulting CSS after compiling the arbitrary arguments example. ```css .micro { width: 180px; } ``` -------------------------------- ### blue SCSS examples Source: https://sass-lang.com/documentation/modules/color Examples of using blue in SCSS syntax. ```scss @use 'sass:color'; @debug color.blue(#e1d7d2); // 210 @debug color.blue(white); // 255 @debug color.blue(black); // 0 ``` -------------------------------- ### list.length SCSS examples Source: https://sass-lang.com/documentation/modules/list SCSS examples of list.length showing lengths of lists and a map. ```SCSS @use 'sass:list'; @debug list.length(10px); // 1 @debug list.length(10px 20px 30px); // 3 @debug list.length((width: 10px, height: 20px)); // 2 ``` -------------------------------- ### CSS output for optional argument example Source: https://sass-lang.com/documentation/at-rules/function The resulting CSS after compiling the optional argument function example. ```css .header { background-color: rgb(32%, 20%, 8%); } ``` -------------------------------- ### CSS Output: @extend example Source: https://sass-lang.com/documentation/at-rules/extend CSS output generated from the SCSS/Sass example, showing the extended selectors. ```CSS p.info { background-color: #dee9fc; } .guide .info, .guide .content nav.sidebar, .content .guide nav.sidebar { border: 1px solid rgba(0, 0, 0, 0.8); border-radius: 2px; } main.content .info, main.content nav.sidebar { font-size: 0.8em; } ``` -------------------------------- ### FileImporter example: redirecting ~ URLs to node_modules Source: https://sass-lang.com/documentation/js-api/interfaces/FileImporter Example of a FileImporter that redirects relative URLs starting with "~" to node_modules. It uses pathToFileURL from the 'url' module and returns a URL for the resolved file. This importer can be passed to sass.compile. ```javascript const {pathToFileURL} = require('url'); sass.compile('style.scss', { importers: [{ // An importer that redirects relative URLs starting with "~" to // `node_modules`. findFileUrl(url) { if (!url.startsWith('~')) return null; return new URL(url.substring(1), pathToFileURL('node_modules')); } }] }); ``` -------------------------------- ### SCSS Syntax for @use Source: https://sass-lang.com/documentation/at-rules/use Example showing how to load foundation modules using the SCSS syntax. ```scss // foundation/_code.scss code { padding: .25em; line-height: 0; } ``` ```scss // foundation/_lists.scss ul, ol { text-align: left; & & { padding: { bottom: 0; left: 0; } } } ``` ```scss // style.scss @use 'foundation/code'; @use 'foundation/lists'; ``` -------------------------------- ### Use --help to view documentation Source: https://sass-lang.com/documentation/cli/ruby-sass Prints a summary of the CLI documentation. ```bash $ sass --help Usage: sass [options] [INPUT] [OUTPUT] Description: Converts SCSS or Sass files to CSS. ... ``` -------------------------------- ### Initialize and use a synchronous Compiler Source: https://sass-lang.com/documentation/js-api/functions/initCompiler Demonstrates creating a compiler instance, performing multiple compilations, and disposing of the compiler. Note that attempting to use the compiler after disposal will throw an error. ```javascript const sass = require('sass'); function setup() { const compiler = sass.initCompiler(); const result1 = compiler.compileString('a {b: c}').css; const result2 = compiler.compileString('a {b: c}').css; compiler.dispose(); // throws error const result3 = sass.compileString('a {b: c}').css; } ``` -------------------------------- ### SCSS Syntax Imports Source: https://sass-lang.com/documentation/at-rules/import Example of using partials and index files in SCSS syntax. ```scss // foundation/_code.scss code { padding: .25em; line-height: 0; } ``` ```scss // foundation/_lists.scss ul, ol { text-align: left; & & { padding: { bottom: 0; left: 0; } } } ``` ```scss // foundation/_index.scss @import 'code', 'lists'; ``` ```scss // style.scss @import 'foundation'; ``` -------------------------------- ### Sass Syntax for @use Source: https://sass-lang.com/documentation/at-rules/use Example showing how to load foundation modules using the indented Sass syntax. ```sass // foundation/_code.sass code padding: .25em line-height: 0 ``` ```sass // foundation/_lists.sass ul, ol text-align: left & & padding: bottom: 0 left: 0 ``` ```sass // style.sass @use 'foundation/code' @use 'foundation/lists' ``` -------------------------------- ### math.unit() Sass syntax example Source: https://sass-lang.com/documentation/modules/math Sass syntax (indented) example using @use 'sass:math' to get unit strings for various numbers. Outputs "", "px", "px*px", and "px/s". ```sass @use 'sass:math' @debug math.unit(100) // "" @debug math.unit(100px) // "px" @debug math.unit(5px * 10px) // "px*px" @debug math.unit(math.div(5px, 1s)) // "px/s" ``` -------------------------------- ### Get red channel with color.getR() Source: https://sass-lang.com/documentation/js-api/classes/types.Color Returns the red channel as an integer from 0 to 255. Examples include #6b717f returning 107 and #b37399 returning 179. ```javascript // color is `#6b717f`. color.getR(); // 107 // color is `#b37399`. color.getR(); // 179 ``` -------------------------------- ### Get green channel with color.getG() Source: https://sass-lang.com/documentation/js-api/classes/types.Color Returns the green channel as an integer from 0 to 255. Examples include #6b717f returning 113 and #b37399 returning 115. ```javascript // color is `#6b717f`. color.getG(); // 113 // color is `#b37399`. color.getG(); // 115 ``` -------------------------------- ### Get blue channel with color.getB() Source: https://sass-lang.com/documentation/js-api/classes/types.Color Returns the blue channel as an integer from 0 to 255. Examples include #6b717f returning 127 and #b37399 returning 153. ```javascript // color is `#6b717f`. color.getB(); // 127 // color is `#b37399`. color.getB(); // 153 ``` -------------------------------- ### Example migration with --migrate-deps Source: https://sass-lang.com/documentation/cli/migrator Example session showing a style.scss using @import and a custom variable, then running sass-migrator with --migrate-deps, and the resulting file that uses @use with configuration and prefixed references. This example also uses the --migrate-deps option. ```shell cat style.scss $body-bg: #000; $body-color: #111; @import "bootstrap"; @include media-breakpoint-up(sm) { .navbar { display: block; } } ``` ```shell sass-migrator --migrate-deps module style.scss ``` ```shell cat style.scss @use "bootstrap" with ( $body-bg: #000, $body-color: #111 ); @include bootstrap.media-breakpoint-up(sm) { .navbar { display: block; } } ``` -------------------------------- ### OutputStyle configuration example Source: https://sass-lang.com/documentation/js-api/interfaces/Options Demonstrates the effect of the style option on compiled CSS output using expanded and compressed styles. ```javascript const source = ` h1 { font-size: 40px; code { font-face: Roboto Mono; } }`; let result = sass.compileString(source, {style: "expanded"}); console.log(result.css.toString()); // h1 { // font-size: 40px; // } // h1 code { // font-face: Roboto Mono; // } result = sass.compileString(source, {style: "compressed"}) console.log(result.css.toString()); // h1{font-size:40px}h1 code{font-face:Roboto Mono} ``` -------------------------------- ### Get alpha channel with color.getA() Source: https://sass-lang.com/documentation/js-api/classes/types.Color Returns the alpha channel as a number from 0 to 1. Examples contrast an opaque color (#6b717f) returning 1 and transparent returning 0. ```javascript // color is `#6b717f`. color.getA(); // 1 // color is `transparent`. color.getA(); // 0 ``` -------------------------------- ### math.div usage examples (SCSS) Source: https://sass-lang.com/documentation/modules/math SCSS examples demonstrating math.div with various unit combinations: unitless division, canceling units, and mixed units resulting in compound units. ```scss @use 'sass:math'; @debug math.div(1, 2); // 0.5 @debug math.div(100px, 5px); // 20 @debug math.div(100px, 5); // 20px @debug math.div(100px, 5s); // 20px/s ``` -------------------------------- ### Run Sass in interactive mode with --interactive Source: https://sass-lang.com/documentation/cli/ruby-sass Starts an interactive Sass session where SassScript expressions can be entered and evaluated. The example shows arithmetic and map operations with their results. ```bash $ sass --interactive >> 1px + 1in 97px >> $map: ("width": 100px, "height": 70px) ("width": 100px, "height": 70px) >> map-get($map, "width") 100px ``` -------------------------------- ### color.ie-hex-str SCSS example Source: https://sass-lang.com/documentation/modules/color Shows how to use color.ie-hex-str in SCSS to get an unquoted string in #AARRGGBB format for Internet Explorer's -ms-filter property. Colors are converted to rgb and gamut-mapped if necessary. ```SCSS @use 'sass:color'; @debug color.ie-hex-str(#b37399); // #FFB37399 @debug color.ie-hex-str(rgba(242, 236, 228, 0.6)); // #99F2ECE4 @debug color.ie-hex-str(oklch(70% 10% 120deg)); // #FF9BA287 ``` -------------------------------- ### SCSS: @extend example Source: https://sass-lang.com/documentation/at-rules/extend SCSS syntax showing @extend usage with comments explaining behavior. ```SCSS .content nav.sidebar { @extend .info; } // This won't be extended, because `p` is incompatible with `nav`. p.info { background-color: #dee9fc; } // There's no way to know whether `