Property Declarations
In Sass as in CSS, property declarations define how elements that match a selector are styled. But Sass adds extra features to make them easier to write and to automate. First and foremost, a declaration’s value can be any SassScript expression, which will be evaluated and included in the result.
SCSS Syntax
.circle {
$size: 100px;
width: $size;
height: $size;
border-radius: $size * 0.5;
}
Sass Syntax
.circle
$size: 100px
width: $size
height: $size
border-radius: $size * 0.5
CSS Output
.circle {
width: 100px;
height: 100px;
border-radius: 50px;
}
InterpolationInterpolation permalink
A property’s name can include interpolation, which makes it possible to dynamically generate properties as needed. You can even interpolate the entire property name!
SCSS Syntax
@mixin prefix($property, $value, $prefixes) {
@each $prefix in $prefixes {
-#{$prefix}-#{$property}: $value;
}
#{$property}: $value;
}
.gray {
@include prefix(filter, grayscale(50%), moz webkit);
}
Sass Syntax
@mixin prefix($property, $value, $prefixes)
@each $prefix in $prefixes
-#{$prefix}-#{$property}: $value
#{$property}: $value
.gray
@include prefix(filter, grayscale(50%), moz webkit)
CSS Output
.gray {
-moz-filter: grayscale(50%);
-webkit-filter: grayscale(50%);
filter: grayscale(50%);
}
NestingNesting permalink
Many CSS properties start with the same prefix that acts as a kind of namespace.
For example, font-family
, font-size
, and font-weight
all start with
font-
. Sass makes this easier and less redundant by allowing property
declarations to be nested. The outer property names are added to the inner,
separated by a hyphen.
SCSS Syntax
.enlarge {
font-size: 14px;
transition: {
property: font-size;
duration: 4s;
delay: 2s;
}
&:hover { font-size: 36px; }
}
Sass Syntax
.enlarge
font-size: 14px
transition:
property: font-size
duration: 4s
delay: 2s
&:hover
font-size: 36px
CSS Output
.enlarge {
font-size: 14px;
transition-property: font-size;
transition-duration: 4s;
transition-delay: 2s;
}
.enlarge:hover {
font-size: 36px;
}
Some of these CSS properties have shorthand versions that use the namespace as the property name. For these, you can write both the shorthand value and the more explicit nested versions.
SCSS Syntax
.info-page {
margin: auto {
bottom: 10px;
top: 2px;
}
}
Sass Syntax
.info-page
margin: auto
bottom: 10px
top: 2px
CSS Output
.info-page {
margin: auto;
margin-bottom: 10px;
margin-top: 2px;
}
Hidden DeclarationsHidden Declarations permalink
Sometimes you only want a property declaration to show up some of the time. If a
declaration’s value is null
or an empty unquoted string, Sass won’t
compile that declaration to CSS at all.
Custom PropertiesCustom Properties permalink
- Dart Sass
- ✓
- LibSass
- since 3.5.0
- Ruby Sass
- since 3.5.0
Older versions of LibSass and Ruby Sass parsed custom property declarations just like any other property declaration, allowing the full range of SassScript expressions as values. Even when using these versions, it’s recommended that you use interpolation to inject SassScript values for forwards-compatibility.
See the breaking change page for more details.
CSS custom properties, also known as CSS variables, have an unusual declaration syntax: they allow almost any text at all in their declaration values. What’s more, those values are accessible to JavaScript, so any value might potentially be relevant to the user. This includes values that would normally be parsed as SassScript.
Because of this, Sass parses custom property declarations differently than other property declarations. All tokens, including those that look like SassScript, are passed through to CSS as-is. The only exception is interpolation, which is the only way to inject dynamic values into a custom property.
SCSS Syntax
$primary: #81899b;
$accent: #302e24;
$warn: #dfa612;
:root {
--primary: #{$primary};
--accent: #{$accent};
--warn: #{$warn};
// Even though this looks like a Sass variable, it's valid CSS so it's not
// evaluated.
--consumed-by-js: $primary;
}
Sass Syntax
$primary: #81899b
$accent: #302e24
$warn: #dfa612
:root
--primary: #{$primary}
--accent: #{$accent}
--warn: #{$warn}
// Even though this looks like a Sass variable, it's valid CSS so it's not
// evaluated.
--consumed-by-js: $primary
CSS Output
:root {
--primary: #81899b;
--accent: #302e24;
--warn: #dfa612;
--consumed-by-js: $primary;
}
⚠️ Heads up!
Unfortunately, interpolation removes quotes from strings, which makes it
difficult to use quoted strings as values for custom properties when they come
from Sass variables. As a workaround, you can use the meta.inspect()
function to preserve the quotes.
SCSS Syntax
@use "sass:meta";
$font-family-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto;
$font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas;
:root {
--font-family-sans-serif: #{meta.inspect($font-family-sans-serif)};
--font-family-monospace: #{meta.inspect($font-family-monospace)};
}
Sass Syntax
@use "sass:meta"
$font-family-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto
$font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas
:root
--font-family-sans-serif: #{meta.inspect($font-family-sans-serif)}
--font-family-monospace: #{meta.inspect($font-family-monospace)}
CSS Output
:root {
--font-family-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto;
--font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas;
}