Effects & Modern CSSPro· 30 min read

Modern CSS: calc(), clamp() & Filters

A tour of powerful modern features that make layouts smarter and effects richer.

What you will learn

  • Do math in CSS with calc(), min(), max(), clamp()
  • Apply visual filters
  • Know that @supports checks feature availability

Math functions

CSS can do arithmetic. calc() mixes units; clamp(min, ideal, max) gives fluid-but-bounded sizing — fantastic for responsive typography.

clamp() and calc() in action
<style>
  .fluid { font-size: clamp(18px, 4vw, 36px); color:#4338ca; font-weight:700; }
  .bar { width: calc(100% - 40px); background:#06b6d4; color:#fff; padding:12px; margin:20px; border-radius:8px; }
</style>
<p class="fluid">I scale with the screen, but never too small or too big.</p>
<div class="bar">width: calc(100% - 40px)</div>
Live preview

Two functions that let CSS do math:

  • clamp(18px, 4vw, 36px) on .fluid reads as min, ideal, max: the heading wants to be 4vw (4% of the screen width) so it scales with the window, but it can never go below 18px or above 36px. Fluid, but never too small or too huge — no media queries needed.
  • calc(100% - 40px) on .bar mixes units: take the full width and subtract 40px. This is something no single unit can express, which is exactly what calc() is for.

Filters

The filter property applies Photoshop-style effects: blur, grayscale, brightness, contrast, and more.

CSS filters
<style>
  img { width:120px; border-radius:10px; margin:6px; }
  .gray { filter: grayscale(100%); }
  .blur { filter: blur(2px); }
  .bright { filter: brightness(1.3) saturate(1.4); }
</style>
<img src="https://picsum.photos/120" alt="normal">
<img class="gray" src="https://picsum.photos/120" alt="grayscale">
<img class="blur" src="https://picsum.photos/120" alt="blur">
<img class="bright" src="https://picsum.photos/120" alt="bright">
Live preview

The same photo appears four times, each with a different filter — Photoshop-style effects applied with one line. grayscale(100%) drains all colour to black-and-white, blur(2px) softens it out of focus, and brightness(1.3) saturate(1.4) stacks two effects to make it lighter and more vivid (you can chain several in one filter value). The first image has no filter, as a reference. Filters are great for hover effects — for example, grey out an image normally and remove the filter on :hover.

Tip: clamp(18px, 4vw, 36px) is the modern trick for responsive font sizes without media queries — it scales with the viewport but stays within sensible limits.

Q. What does clamp(1rem, 4vw, 2rem) do for font-size?

Answer: clamp(min, preferred, max) grows/shrinks with the preferred value but is bounded by the min and max — fluid, safe sizing.

✍️ Practice

  1. Give a heading a fluid clamp() font-size and resize to watch it adapt.
  2. Apply grayscale to images and remove it on hover with a transition.

🏠 Homework

  1. Use clamp() for your headings so they scale smoothly across devices.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →