Sass Relational Operators

Beginner
8 min read
Updated: Nov 2026
5 examples
Dart Sass

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

SCSS
$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).

Units

How units affect comparisons:

CaseResult
Same unitDirect numeric compare
Compatible units (px / in, ms / s)Converted, then compared
Unitless vs unitAllowed — unitless adopts the other unit
Incompatible (px vs s)Compile error
vs ==Relational orders numbers; equality also cares about type/units differently

SCSS Examples

Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official Relational Samples

Compare unitless numbers, lengths, and convertible units.

SCSS
@use "sass:meta";

.probe {
  --a: #{meta.inspect(100 > 50)};
  --b: #{meta.inspect(10px < 17px)};
  --c: #{meta.inspect(96px >= 1in)};
  --d: #{meta.inspect(1000ms <= 1s)};
}

How It Works

  1. > compares plain numbers.
  2. < compares same-unit lengths.
  3. >= converts inches to pixels.
  4. <= converts milliseconds to seconds.

Example 2 — Unitless vs Unit

Unitless values adopt the other side’s unit for ordering.

SCSS
@use "sass:meta";

.unitless {
  --a: #{meta.inspect(100 > 50px)};
  --b: #{meta.inspect(10px < 17)};
}

How It Works

  1. 100 is treated like 100px here.
  2. 17 is treated like 17px.
  3. Both comparisons succeed.
  4. Matches the official unitless samples.

Example 3 — Relational vs Equality

1 == 1px is false, but unitless can still order against px.

SCSS
@use "sass:meta";

.eq-vs-rel {
  --eq: #{meta.inspect(1 == 1px)};
  --rel: #{meta.inspect(1 < 2px)};
}

How It Works

  1. Equality requires matching units in Dart Sass.
  2. Relational allows unitless adoption.
  3. Different jobs: same-value vs ordering.
  4. Pick the operator that matches your intent.

Example 4 — Breakpoint with >=

Inclusive width check for a layout token.

SCSS
$width: 720px;

.layout {
  @if $width >= 600px {
    max-width: 60rem;
  } @else {
    max-width: 100%;
  }
}

How It Works

  1. 720px >= 600px is true.
  2. @if keeps the wide layout branch.
  3. Inclusive — exactly 600px would match too.
  4. 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.

SCSS
$size: 14px;
$min: 12px;
$max: 18px;

.text {
  @if $size >= $min and $size <= $max {
    font-size: $size;
  } @else {
    font-size: $min;
  }
}

How It Works

  1. >= and <= bound the range.
  2. and requires both sides.
  3. 14px sits inside 12–18.
  4. 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.

Sass Support

Relational operators are core SassScript on Dart Sass.

OperatorMeaning
<Less than
<=Less than or equal
>Greater than
>=Greater than or equal
Return typeBoolean (true / false)

Compile with the Dart Sass CLI.

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.

Compile Notes

Practical checklist:

checklist.txt
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

They compare whether one number is larger or smaller than another. Sass provides <, <=, >, and >=. Each returns true or false at compile time.
Yes. Sass converts between compatible units automatically. Example: 96px >= 1in is true. Comparing incompatible units like 100px > 10s causes a compile error.
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.
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.
Use <= and >= when equal values should count as a match—for example breakpoints ($width >= 600px) or inclusive ranges ($size >= $min and $size <= $max).
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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