Sass Relational Operators

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
< <= > >= · numbers

What You’ll Learn

Sass relational operators answer “is this number larger or smaller?” Use <, <=, >, and >= on numbers. This page covers unit conversion, unitless values, incompatible units, and five compiled examples.

01

Operators

< <= > >=

02

Returns

true / false

03

Units

Auto-convert

04

Unitless

Compares freely

05

Errors

Incompatible units

06

Practice

5 examples

What Are Relational Operators?

Official docs: relational operators determine whether numbers are larger or smaller than one another. They automatically convert between compatible units.

  • They work on numbers (with or without units).
  • Results are booleans: true or false.
  • Most often used inside @if, clamps, and design-token guards.
  • Comparison happens when Sass compiles—not in the browser.
  • Pair with equality (== / !=) when you need “same value” checks.
💡
Beginner tip

Think of a ruler: “Is this width at least 600px?” becomes $width >= 600px. Sass answers before CSS is written.

📝 Syntax

styles.scss
// Less than
$a < $b

// Less than or equal
$a <= $b

// Greater than
$a > $b

// Greater than or equal
$a >= $b

Operators

OperatorNameReturns true when…
<less thanThe first number is smaller than the second
<=less than or equalThe first is smaller than or equal to the second
>greater thanThe first number is larger than the second
>=greater than or equalThe first is larger than or equal to the second

Return value

TypeResult
Booleantrue or false

⚖️ Units & Automatic Conversion

Official docs: relational operators automatically convert between compatible units.

ExpressionResultWhy
100 > 50truePlain numbers
10px < 17pxtrueSame unit
96px >= 1intrueCompatible length units convert
1000ms <= 1strueCompatible time units convert

🔢 Unitless Numbers

Official docs: unitless numbers can be compared with any number. They are automatically converted to that number’s unit.

ExpressionResult
100 > 50pxtrue
10px < 17true
💡
Different from equality

With == in Dart Sass, 1 != 1px. With relational operators, unitless values can compare against unit values. Choose the operator that matches your intent.

⚠️ Incompatible Units

Official docs: numbers with incompatible units can’t be compared. Sass stops with a compile error.

styles.scss
// Error: Incompatible units px and s.
@debug 100px > 10s;
  • Length vs time (px vs s) fails.
  • Length vs angle (px vs deg) fails.
  • Keep both sides in the same measurement family.

🔁 Relational vs Equality

RelationalEquality
Operators< <= > >=== !=
QuestionLarger or smaller?Same value?
TypesNumbers onlyAny Sass type
Unitless + unitCan compareNot equal in Dart Sass

⚡ Quick Reference

GoalCode
Less than$a < $b
Less than or equal$a <= $b
Greater than$a > $b
Greater than or equal$a >= $b
Breakpoint branch@if $width >= 600px { ... }
Inclusive range$size >= $min and $size <= $max
Compatible units96px >= 1intrue
Inspect a booleanmeta.inspect($a > $b)

Examples Gallery

Each example uses relational operators at compile time. Open View Compiled CSS for verified output (booleans shown with meta.inspect).

📚 Getting Started

Official number comparisons with and without units.

Example 1 — Basic Comparisons

Plain numbers, same units, and compatible unit conversion.

styles.scss
@use "sass:meta";

.probe {
  --gt: #{meta.inspect(100 > 50)};
  --lt: #{meta.inspect(10px < 17px)};
  --ge: #{meta.inspect(96px >= 1in)};
  --le: #{meta.inspect(1000ms <= 1s)};
}

How It Works

Matches the official docs: 96px >= 1in and 1000ms <= 1s succeed after unit conversion.

Example 2 — Unitless With Units

Unitless numbers convert to the other side’s unit during comparison.

styles.scss
@use "sass:meta";

.probe {
  --ul1: #{meta.inspect(100 > 50px)};
  --ul2: #{meta.inspect(10px < 17)};
  --eq-edge: #{meta.inspect(100 >= 100)};
  --strict: #{meta.inspect(100 > 100)};
}

How It Works

Unitless sides adapt. Also note inclusive vs exclusive: 100 >= 100 is true, while 100 > 100 is false.

📈 Practical Patterns

Breakpoints, inclusive ranges, and type-scale checks.

Example 3 — Breakpoint With @if

Emit different styles when a compile-time width clears a threshold.

styles.scss
@use "sass:meta";

$width: 720px;

.card {
  @if $width >= 600px {
    --layout: wide;
    max-width: 100%;
  } @else {
    --layout: narrow;
  }
  --ok: #{meta.inspect($width >= 600px)};
}

How It Works

Prefer >= for breakpoints so the exact threshold value still counts as a match. Only the chosen branch appears in CSS.

Example 4 — Inclusive Range Check

Combine >= and <= (with and) for min/max clamps.

styles.scss
@use "sass:meta";

$min: 12px;
$max: 24px;
$size: 18px;

.clamp-demo {
  --in-range: #{meta.inspect($size >= $min and $size <= $max)};
  --below: #{meta.inspect(10px < $min)};
  --above: #{meta.inspect(30px > $max)};
}

How It Works

Inclusive bounds keep the edges inside the range. Use strict < / > when you want open intervals.

Example 5 — Type Scale Steps

Compare computed sizes in a modular type scale.

styles.scss
@use "sass:meta";

$base: 16px;
$step: 4px;

.scale {
  --md-gt-sm: #{meta.inspect(($base + $step) > $base)};
  --lg-ge: #{meta.inspect(($base + $step * 2) >= 24px)};
}

How It Works

Arithmetic runs first, then the relational check. Handy for asserting scale steps or guarding helper arguments.

🚀 Real-World Use Cases

  • Breakpoints@if $width >= 768px for layout variants.
  • Clamps — keep font or spacing tokens inside a min/max range.
  • Token validation — reject sizes that are too small or too large.
  • Type scales — assert each step is larger than the previous.
  • Timing tokens — compare durations after converting ms and s.

🧠 How Compilation Works

1

Write a comparison

Use <, <=, >, or >= between two numbers.

Source
2

Convert compatible units

Sass aligns units (or errors if they are incompatible).

Compile
3

Drive a branch or value

Feed the boolean into @if or store it for debugging.

Emit
4

CSS ships

Browsers only see finished CSS like max-width: 100%.

⚠️ Common Pitfalls

  • Incompatible units100px > 10s fails at compile time.
  • Exclusive vs inclusive> excludes equality; >= includes it.
  • Confusing with == — relational ops do not compare strings or colors.
  • Unitless habits — relational unitless rules differ from equality rules.
  • Expecting browser runtime — these operators never appear in CSS.

💡 Best Practices

✅ Do

  • Use >= / <= for inclusive breakpoints and ranges
  • Keep both sides in the same unit family
  • Rely on automatic conversion for px/in or ms/s
  • Combine with and for min/max checks
  • Debug booleans with meta.inspect

❌ Don’t

  • Compare length to time or angle units
  • Use relational operators on strings or colors
  • Assume unitless rules match == behavior
  • Forget that equal values fail a strict > check
  • Expect browsers to evaluate these operators

Key Takeaways

Knowledge Unlocked

Five things to remember about Sass relational operators

Order numbers at compile time with <, <=, >, and >=.

5
Core concepts
📦 02

Result

true / false

Output
🔗 03

Units

auto-convert

Rule
🔢 04

Unitless

compares freely

Rule
05

Use

@if & ranges

Pattern

❓ 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.
Did you know?

Official Sass docs show 96px >= 1in as true—relational operators convert compatible units, and unitless numbers can compare with any unit value.

Conclusion

Sass relational operators <, <=, >, and >= order numbers at compile time. Lean on unit conversion, remember unitless rules, and avoid incompatible units—then drive clear @if branches and range checks.

Continue with numeric operators or browse equality operators.

Next: Sass Numeric Operators

Add, subtract, multiply, modulo, and divide with math.div().

Numeric operators →

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.

5 people found this page helpful