Home Sass Operators Relational Sass Relational Operators Definition
Definition and Usage Relational operators compare whether one number is larger or smaller than another: <, <=, >, and >=. Compatible units convert automatically.
Official sample idea. 10px < 17px is true; 96px >= 1in is true after unit conversion.
Unitless mixes. A unitless number can compare with any unit—it adopts that unit for the comparison.
Incompatible units error. 100px > 10s fails at compile time. Compare lengths to lengths, times to times.
Syntax
Syntax $a < $b // less than
$a <= $b // less than or equal
$a > $b // greater than
$a >= $b // greater than or equal
100 > 50; // true
10px < 17px; // true
96px >= 1in; // true
1000ms <= 1s; // true
100 > 50px; // true (unitless)
// Typical use:
@if $width >= 600px { ... } Pattern: compare two numbers → get a boolean → branch with @if (often with and / or ).
Code
SCSS Examples Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Official 2. Unitless 3. vs == 4. Breakpoint 5. Range Example 1 — Official Relational Samples Compare unitless numbers, lengths, and convertible units.
@use "sass:meta";
.probe {
--a: #{meta.inspect(100 > 50)};
--b: #{meta.inspect(10px < 17px)};
--c: #{meta.inspect(96px >= 1in)};
--d: #{meta.inspect(1000ms <= 1s)};
} .probe {
--a: true;
--b: true;
--c: true;
--d: true;
} How It Works > compares plain numbers.< compares same-unit lengths.>= converts inches to pixels.<= converts milliseconds to seconds.Example 2 — Unitless vs Unit Unitless values adopt the other side’s unit for ordering.
@use "sass:meta";
.unitless {
--a: #{meta.inspect(100 > 50px)};
--b: #{meta.inspect(10px < 17)};
} .unitless {
--a: true;
--b: true;
} How It Works 100 is treated like 100px here.17 is treated like 17px.Both comparisons succeed.Matches the official unitless samples.Example 3 — Relational vs Equality 1 == 1px is false, but unitless can still order against px.
@use "sass:meta";
.eq-vs-rel {
--eq: #{meta.inspect(1 == 1px)};
--rel: #{meta.inspect(1 < 2px)};
} .eq-vs-rel {
--eq: false;
--rel: true;
} How It Works Equality requires matching units in Dart Sass.Relational allows unitless adoption.Different jobs : same-value vs ordering.Pick the operator that matches your intent.Example 4 — Breakpoint with >= Inclusive width check for a layout token.
$width: 720px;
.layout {
@if $width >= 600px {
max-width: 60rem;
} @else {
max-width: 100%;
}
} .layout {
max-width: 60rem;
} How It Works 720px >= 600px is true.@if keeps the wide layout branch.Inclusive — exactly 600px would match too.Compile-time only; not a CSS media query.Example 5 — Inclusive Range with and Clamp a font size between min and max using two comparisons.
$size: 14px;
$min: 12px;
$max: 18px;
.text {
@if $size >= $min and $size <= $max {
font-size: $size;
} @else {
font-size: $min;
}
} .text {
font-size: 14px;
} How It Works >= and <= bound the range.and requires both sides.14px sits inside 12–18.Pair relational + boolean operators often. Edge Cases & Pitfalls Most relational bugs are incompatible units or mixing up == with ordering.
units Do not compare px to s Incompatible units throw. Keep dimensions comparable.
== Ordering is not equality 1 == 1px is false in Dart Sass, but 1 < 2px can be true. See equality .
inclusive Prefer >= / <= for inclusive bounds Breakpoints and ranges usually want equal values to match.
compile-time Not a CSS media query @if $width >= 600px chooses CSS while compiling. Browser-responsive checks still need @media.
Toolchain
Sass Support Relational operators are core SassScript on Dart Sass.
Operator Meaning <Less than <=Less than or equal >Greater than >=Greater than or equal Return type Boolean (true / false)
Compile with the Dart Sass CLI .
Remember
Key Takeaways < <= > >= order numbers at compile time.
Compatible units convert ; incompatible units error.
Unitless can compare with any unit.
Use with @if for breakpoints and ranges.
One line: compare Sass numbers with < <= > >=—units convert when compatible.
Practice
Compile Notes Practical checklist:
1. Compare numbers only (not strings/colors)
2. Keep units compatible (or unitless)
3. Prefer >= / <= for inclusive breakpoints
4. Combine with and / or when needed
5. Remember: this is compile-time, not @media Frequently Asked Questions What are Sass relational operators? They compare whether one number is larger or smaller than another. Sass provides <, <=, >, and >=. Each returns true or false at compile time.
Do units matter? Yes. Sass converts between compatible units automatically. Example: 96px >= 1in is true. Comparing incompatible units like 100px > 10s causes a compile error.
Can unitless numbers be compared with units? Yes. Official docs: unitless numbers can be compared with any number and are automatically converted to that number’s unit. Example: 100 > 50px is true.
How is this different from equality (==)? Equality asks whether two values are the same (any type). Relational operators only order numbers. Also, for equality in Dart Sass, 1 != 1px, but for relational comparisons a unitless number can compare with a unit value.
When do I use <= or >= instead of < or >? Use <= and >= when equal values should count as a match—for example breakpoints ($width >= 600px) or inclusive ranges ($size >= $min and $size <= $max).
Do relational operators run in the browser? No. They resolve when Sass compiles. The CSS file only keeps the resulting branch or emitted values.
Next: Sass Numeric Operators Do arithmetic with +, -, *, %, and math.div().
Numeric → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion