What characters are valid for use in SCSS variable names?
Sass uses $ to distinguish variables (such as $highlight-color , $sidebar-width ). The dollar sign was chosen because it's visually distinctive, it's aesthetically pleasing, and it's not used elsewhere in CSS and thus doesn't come into conflict with any present or future CSS syntax. Older versions of Sass used !
The period, the underscore, and the characters $, #, and @ can be used within variable names. For example, A. _$@#1 is a valid variable name.
A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
If you check out the source for the SASS lexer, you'll see:
# A hash of regular expressions that are used for tokenizing.
REGULAR_EXPRESSIONS = {
:whitespace => /\s+/,
:comment => COMMENT,
:single_line_comment => SINGLE_LINE_COMMENT,
:variable => /(\$)(#{IDENT})/,
:ident => /(#{IDENT})(\()?/,
:number => /(-)?(?:(\d*\.\d+)|(\d+))([a-zA-Z%]+)?/,
:color => HEXCOLOR,
:bool => /(true|false)\b/,
:null => /null\b/,
:ident_op => %r{(#{Regexp.union(*IDENT_OP_NAMES.map{|s| Regexp.new(Regexp.escape(s) + "(?!#{NMCHAR}|\Z)")})})},
:op => %r{(#{Regexp.union(*OP_NAMES)})},
}
Which references the IDENT
character set defined in a separate file:
s = if Sass::Util.ruby1_8?
'\200-\377'
elsif Sass::Util.macruby?
'\u0080-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF'
else
'\u{80}-\u{D7FF}\u{E000}-\u{FFFD}\u{10000}-\u{10FFFF}'
end
H = /[0-9a-fA-F]/
UNICODE = /\\#{H}{1,6}[ \t\r\n\f]?/
NONASCII = /[#{s}]/
ESCAPE = /#{UNICODE}|\\[ -~#{s}]/
NMSTART = /[_a-zA-Z]|#{NONASCII}|#{ESCAPE}/
NMCHAR = /[a-zA-Z0-9_-]|#{NONASCII}|#{ESCAPE}/
IDENT = /-?#{NMSTART}#{NMCHAR}*/
So, it looks like variable names can contain:
!"#$%&'()*+,./:;<=>?@[]^{|}~
) and spaces, if escaped with a backslash.0080-D7FF
, E000-FFFD
, or 10000-10FFFF
.\00E4
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With