KodingKaro
CSS · Advanced · Modern CSS Functions

Modern color and relational functions

Modern CSS functions can derive colors and conditional values close to where they are used, reducing repeated hard-coded variants.

✓ What you will learn in this topic

  • Explain the key idea: Modern functions are powerful; pair them with clear intent and appropriate fallbacks.
  • Apply the concept: The border and surface use blended colors while the spacing never falls below one rem.
  • Avoid this common mistake:

Concept explanation

Color functions such as color-mix() can blend compatible colors. Relational functions such as min() and max() can choose among dimensions. Newer functions may have varying browser support, so check the project’s support targets and retain a fallback when a missing value would harm usability.

Do not build an opaque math puzzle. Use named custom properties for important design roles and comment only when the formula’s intent is not obvious.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo {
  border-color: color-mix(in srgb, #1d4ed8 35%, white);
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo {
  border-color: color-mix(in srgb, #1d4ed8 35%, white);
}

Expected result: The border and surface use blended colors while the spacing never falls below one rem.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card {
  padding: max(1rem, 2vw);
  background: color-mix(in srgb, var(--surface, white) 92%, var(--brand, blue));
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Combining units with calc() without checking the resulting value or providing a sensible bound.

Best practice

Use readable expressions, retain minimum and maximum constraints, and verify support for any newly adopted feature.

Practice exercise

Create a fluid heading size and page gutter that scale smoothly but remain usable at extreme viewport widths. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Modern functions are powerful; pair them with clear intent and appropriate fallbacks.
  • Use readable expressions, retain minimum and maximum constraints, and verify support for any newly adopted feature.

Knowledge check

Why may a project need a fallback for a newer CSS function?

Reveal the explanation

Check the support matrix for the project and provide a safe earlier declaration when needed.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo {
  border-color: color-mix(in srgb, #1d4ed8 35%, white);
}

Expected result:

The border and surface use blended colors while the spacing never falls below one rem.

Hands-on practice task

Create a fluid heading size and page gutter that scale smoothly but remain usable at extreme viewport widths.

In this section

Other lessons in Modern CSS Functions

Broaden your knowledge

Related learning topics