{"id":41,"date":"2026-10-02T10:32:42","date_gmt":"2026-10-02T10:32:42","guid":{"rendered":"https:\/\/webdevpuneet.com\/blog\/?p=41"},"modified":"2026-10-02T10:32:46","modified_gmt":"2026-10-02T10:32:46","slug":"css-clamp-explained-with-10-live-fluid-sizing-demos-you-can-drag","status":"publish","type":"post","link":"https:\/\/webdevpuneet.com\/blog\/css-clamp-explained-with-10-live-fluid-sizing-demos-you-can-drag\/","title":{"rendered":"CSS clamp() Explained, with 10 Live Fluid-Sizing Demos You Can Drag"},"content":{"rendered":"<p><!--\n================================================================\n  POST TITLE (paste into the WordPress title field \u2014 it becomes the page H1)\n  CSS clamp() Explained, with 10 Live Fluid-Sizing Demos You Can Drag\n\n  SEO TITLE (53 chars, target 50\u201360, max 60 \u2014 \"Optimize SEO\" panel \u2192 SEO TITLE)\n  CSS clamp() Explained with 10 Live Fluid-Sizing Demos\n\n  SEO DESCRIPTION (156 chars, target 150\u2013160, max 160 \u2014 \"Optimize SEO\" panel \u2192 SEO DESCRIPTION)\n  Learn CSS clamp() with 10 draggable live demos: fluid type, padding, gaps, radius and spacing tokens. Edit each one free in your browser, no sign-up needed.\n\n  HOW TO PASTE: click the first line after this comment, press Ctrl+Shift+End, Ctrl+C.\n  In WordPress click \"Type \/ to choose a block\", then Ctrl+V.\n  Fallback: \u22ee menu \u2192 Code editor \u2192 paste.\n================================================================\n--><\/p>\n\n\n<p class=\"wp-block-paragraph\">Most people meet <code>clamp()<\/code> through one copy-pasted line of fluid typography and never go further. That is a shame, because <code>clamp()<\/code> is not a typography trick \u2014 it is a general-purpose way to say <em>&#8220;pick a value between these two limits, and let it scale in between.&#8221;<\/em> It works on padding, gaps, border-radius, letter-spacing, custom properties, anything that takes a length. Learn the one function and you delete a stack of media queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post explains the syntax, then gives you <strong>10 interactive demos<\/strong> \u2014 each one a small box with a drag handle on the right edge. Drag it and watch the value in the readout change live, both flattening out at its floor and its ceiling and gliding smoothly in between. That flattening is the whole point of <code>clamp()<\/code>: it is not linear scaling, it is <em>bounded<\/em> linear scaling.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dragging the handle is the fast way to see the concept, but it isn&#8217;t the whole demo. Every embed below has its own <strong>HTML \/ CSS \/ JS<\/strong> tabs with the full working source, plus a <strong>Fork &amp; Edit<\/strong> button that opens that exact snippet \u2014 already loaded, already running \u2014 in <a href=\"https:\/\/webdevpuneet.com\/ui-snippets\/mycode\/\">My Code<\/a>, webdevpuneet.com&#8217;s free in-browser editor. Change a floor, change a ceiling, swap <code>cqw<\/code> for <code>vw<\/code>, break it, fix it. That loop \u2014 predict what a number change will do, then actually watch it happen \u2014 teaches <code>clamp()<\/code> faster than reading ten more paragraphs about it ever will. Nothing you edit there touches this page or leaves your browser until you decide to save it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The syntax, in one breath<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><pre><code>property: clamp(MINIMUM, PREFERRED, MAXIMUM);<\/code><\/pre><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>clamp()<\/code> takes three comma-separated values and returns whichever one applies:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the <strong>preferred<\/strong> value would compute below the minimum, the browser uses the <strong>minimum<\/strong> instead \u2014 the value is floored.<\/li>\n\n\n\n<li>If the preferred value would compute above the maximum, the browser uses the <strong>maximum<\/strong> instead \u2014 the value is capped.<\/li>\n\n\n\n<li>Otherwise, the preferred value is used exactly as computed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The trick that makes it &#8220;fluid&#8221; is that the <strong>preferred<\/strong> argument is usually a relative unit \u2014 <code>vw<\/code> (a percentage of the viewport width), <code>%<\/code>, or as in every demo below, <code>cqw<\/code> (a percentage of a container&#8217;s width). Because that middle value is relative, it recomputes continuously as the viewport or container resizes, and the min\/max simply stop it from ever going too far in either direction. You are looking at three fixed points doing the job of an entire <code>@media<\/code> block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why these demos use cqw, not vw<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The classic fluid-typography formula you&#8217;ll see everywhere is <code>font-size: clamp(1.25rem, 1rem + 2vw, 3rem)<\/code> \u2014 <code>vw<\/code> ties the preferred value to the real browser viewport. That is exactly right for a real page, but it makes for a broken demo: embedded in an iframe on this page, the &#8220;viewport&#8221; is the iframe&#8217;s own width, and you cannot usefully resize an iframe by dragging inside it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So every demo below uses <strong><code>cqw<\/code><\/strong> instead of <code>vw<\/code>. It is CSS Container Query Units \u2014 1<code>cqw<\/code> is 1% of the width of the nearest ancestor with <code>container-type: inline-size<\/code> set on it. Each demo wraps its element in exactly that kind of container, so when you drag the handle and resize <em>that specific box<\/em>, the clamped value responds to it directly \u2014 no need to touch your actual browser window. Everything else about <code>clamp()<\/code> \u2014 the syntax, the floor\/ceiling behaviour, the reasoning for using it \u2014 is identical whether the preferred value is driven by <code>vw<\/code>, <code>%<\/code>, or <code>cqw<\/code>. Swap <code>cqw<\/code> for <code>vw<\/code> in your own project and you get the classic viewport-fluid version; the mechanics you&#8217;re seeing here don&#8217;t change.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>One caveat worth knowing:<\/strong> <code>-webkit-line-clamp<\/code> is a different, unrelated CSS property \u2014 it truncates multi-line text to a fixed number of lines with an ellipsis. It shares a name with <code>clamp()<\/code> and nothing else. If you searched for &#8220;CSS clamp&#8221; hoping to truncate a paragraph, that&#8217;s the property you want instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Fluid Heading Typography<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The canonical clamp() formula: a <code>rem<\/code> floor, a <code>rem<\/code>-plus-relative-unit preferred value, and a <code>rem<\/code> ceiling. Drag the handle and the headline scales continuously between 20px and 48px.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/1.html\" style=\"width:100%;height:340px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Heading Typography \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>font-size: clamp(20px, 6px + 5.2cqw, 48px)<\/code>. The &#8220;base + coefficient&#8221; shape of the middle argument is what lets you tune two things independently: the flat <code>6px<\/code> sets a baseline the relative term always adds to, and <code>5.2cqw<\/code> sets how aggressively it grows as the container widens. Drag all the way left and the heading locks at exactly 20px; drag all the way right and it locks at exactly 48px \u2014 those flat regions at both ends are <code>clamp()<\/code> actually clamping, not the demo running out of room.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> hero headlines and page titles, anywhere you&#8217;d otherwise write three or four <code>font-size<\/code> breakpoints. <strong>Tip:<\/strong> in production, use <code>vw<\/code> in place of <code>cqw<\/code> to tie this to the real viewport instead of a container.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Fluid Body Copy<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Fluid sizing isn&#8217;t only for headlines. This paragraph&#8217;s font-size scales gently between 15px and 19px, which keeps long-form copy comfortable to read whether it&#8217;s sitting in a narrow sidebar or a wide article column.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/2.html\" style=\"width:100%;height:440px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Body Copy \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>font-size: clamp(15px, 13px + 0.74cqw, 19px)<\/code>. Notice the coefficient here (<code>0.74cqw<\/code>) is much smaller than the heading&#8217;s (<code>5.2cqw<\/code>) \u2014 body copy should barely move, while a headline can afford a dramatic range. That&#8217;s a deliberate design decision every fluid clamp() encodes: how much you want each element to actually respond to available space.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> any paragraph, card description, or long-form text block where you want it to breathe a little more on desktop without ballooning past a comfortable reading size. <strong>Tip:<\/strong> keep body-copy ranges tight (a few pixels, like this one) \u2014 large swings in paragraph text size read as a bug, not a feature.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Fluid Card Padding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">clamp() isn&#8217;t limited to font-size. Here it drives the padding on all four sides of a card at once \u2014 cramped padding never happens at the narrow end, and the card never looks like it&#8217;s swimming in empty space at the wide end.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/3.html\" style=\"width:100%;height:360px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Card Padding \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>padding: clamp(12px, 5cqw, 40px)<\/code>. This is the simplest possible clamp() shape \u2014 no additive base term, just a bare relative unit for the preferred value. That&#8217;s the right call whenever you don&#8217;t need independent control over the starting point; the floor and ceiling alone are enough to keep the coefficient sensible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> card components, modal bodies, and any container whose internal spacing should track the container&#8217;s own size rather than the whole page. <strong>Tip:<\/strong> a single clamp() applied to the shorthand <code>padding<\/code> property sets all four sides identically \u2014 if you need different floors per side, write four separate clamp() declarations instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Fluid Grid Gap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The gap between grid tiles is itself a clamp()-driven value. Drag the handle and watch the tiles pull apart or draw together while staying perfectly aligned \u2014 the grid never looks cramped, and it never looks sparse either.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/4.html\" style=\"width:100%;height:660px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Grid Gap \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>gap: clamp(6px, 3cqw, 24px)<\/code>. This is often the most-overlooked place to use clamp() \u2014 teams will fluidly size their fonts and then leave every gap and margin as a single fixed value, which is exactly what breaks the visual rhythm as the layout scales. One clamp() on <code>gap<\/code> keeps grid spacing proportional to everything else that&#8217;s fluidly sized around it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> any CSS Grid or Flexbox layout where the number of columns is fixed but the available width isn&#8217;t \u2014 product grids, image galleries, dashboard tiles. <strong>Tip:<\/strong> pair a fluid gap with <code>grid-template-columns: repeat(auto-fill, minmax(...))<\/code> for a grid that adds columns responsively <em>and<\/em> keeps its spacing proportional.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Fluid Border Radius<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Small, crisp corner rounding at the narrow end; a soft &#8220;squircle&#8221; look at the wide end. One clamp() line, no separate rounded\/extra-rounded utility classes.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/5.html\" style=\"width:100%;height:380px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Border Radius \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>border-radius: clamp(8px, 3.4cqw, 28px)<\/code>. Border-radius is a property most teams hardcode to one value for the whole design system, but it scales the same way font-size does \u2014 a corner that reads as &#8220;subtly rounded&#8221; on a small card can look almost sharp-edged if that exact pixel value is reused on a much larger hero panel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> any component that appears at more than one size across your product \u2014 buttons, cards, image thumbnails, avatars. <strong>Tip:<\/strong> keep the ceiling below half the element&#8217;s smallest expected dimension, or the corners will visually merge into a pill\/circle shape you didn&#8217;t intend.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Fluid Avatar Size<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An avatar circle sized entirely with clamp() \u2014 both its width\/height and its initials&#8217; font-size shrink together in a tight sidebar and stay legible in a wide layout.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/6.html\" style=\"width:100%;height:340px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Avatar Size \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>width: clamp(32px, 12cqw, 96px)<\/code> (and the same expression on <code>height<\/code>, plus a smaller-range clamp() on the initials&#8217; <code>font-size<\/code>). This demo is a good example of <strong>coordinated<\/strong> clamp() usage \u2014 three separate declarations, each with its own floor and ceiling, all driven by the same container so they move in sync rather than three independent values drifting apart at different rates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> user avatars, team member photos, or any circular badge that needs to work identically in a cramped comment thread and a spacious profile header. <strong>Tip:<\/strong> when width and height share an expression like this, they&#8217;ll always stay a perfect circle \u2014 if you only clamp one of them, you&#8217;ll get an oval at every size except the exact midpoint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Fluid Hero Vertical Spacing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Vertical padding on a hero block: tight on a small screen so the call-to-action isn&#8217;t pushed below the fold, generous on a large one so the section doesn&#8217;t feel squeezed.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/7.html\" style=\"width:100%;height:390px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Hero Vertical Spacing \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>padding-block: clamp(24px, 10cqw, 80px)<\/code>. <code>padding-block<\/code> is the logical-property version of &#8220;top and bottom padding&#8221; \u2014 using it instead of separately writing <code>padding-top<\/code>\/<code>padding-bottom<\/code> means this also respects vertical writing modes automatically, at no extra cost.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> hero sections, page headers, and any full-bleed block where vertical rhythm needs to scale with viewport height as much as width. <strong>Tip:<\/strong> vertical spacing is the one place teams most often forget clamp() exists \u2014 it&#8217;s usually reserved for font-size, but a fixed 80px of hero padding on a phone is exactly the kind of thing that pushes your headline off-screen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Fluid Letter Spacing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An uppercase eyebrow label, tightly tracked in a narrow space and opened up once there&#8217;s room to breathe \u2014 the kind of detail that separates a label that looks &#8220;designed&#8221; from one that was just left at its default.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/8.html\" style=\"width:100%;height:340px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Letter Spacing \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>letter-spacing: clamp(0px, 0.6cqw, 4px)<\/code>. Drag this one and you&#8217;ll notice the floor never quite goes flat the way the others do \u2014 that&#8217;s not a bug. With a <code>0px<\/code> floor and a purely positive coefficient, the preferred value can only equal the floor in the limit as the container shrinks toward zero width, which never happens within a normal drag range. The ceiling, on the other hand, engages exactly as expected. It&#8217;s a useful reminder to actually check where your own clamp() floors are reachable, not just assume they are.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> eyebrow labels, section kickers, and all-caps microcopy where tracking needs to scale down gracefully rather than staying fixed and looking cramped on mobile. <strong>Tip:<\/strong> keep letter-spacing ranges small \u2014 a few pixels at most. Large swings read as a font substitution bug before they read as a design choice.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9. Fluid Button Sizing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Horizontal padding on a call-to-action button: comfortable to tap on a small screen, roomy without looking oversized on a large one \u2014 and the font-size scales right alongside it.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/9.html\" style=\"width:100%;height:340px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Button Sizing \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong> <code>padding-inline: clamp(14px, 5cqw, 32px)<\/code>, paired with <code>font: 700 clamp(13px, 3cqw, 16px) system-ui, sans-serif<\/code>. Two clamp() expressions, one on padding and one on font-size, moving together so the button always looks proportionally sized rather than having its text outgrow its padding (or vice versa) at some in-between width.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> primary CTAs, especially ones that appear in both a compact mobile nav and a spacious desktop hero. <strong>Tip:<\/strong> when you clamp two related properties on the same element, make sure their floor-to-ceiling <em>ratios<\/em> are similar \u2014 if one grows 3x and the other only 1.2x, the button&#8217;s proportions will visibly warp as it scales.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. Fluid Spacing Scale (Design Tokens)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every demo so far applied clamp() to one property on one element. This last one shows the pattern that actually scales to a whole design system: three custom properties \u2014 <code>--space-sm<\/code>, <code>--space-md<\/code>, <code>--space-lg<\/code> \u2014 each its own clamp(), used together to drive an entire spacing scale from three lines of CSS.<\/p>\n\n\n\n<iframe src=\"https:\/\/fwdtools.com\/demos\/a1\/01\/10.html\" style=\"width:100%;height:380px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Fluid Spacing Scale Design Tokens \u2014 CSS clamp() Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><pre><code>--space-sm: clamp(6px, 1.8cqw, 14px);\n--space-md: clamp(10px, 3.5cqw, 28px);\n--space-lg: clamp(16px, 6cqw, 48px);<\/code><\/pre><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">defined once on a shared ancestor and then referenced everywhere \u2014 <code>gap: var(--space-md)<\/code>, <code>padding: var(--space-lg)<\/code>, and so on. This is the version of clamp() worth adopting project-wide: instead of writing a bespoke clamp() expression on every element that needs spacing, you define a small fluid scale once and every component just references a token. Change the scale in one place and the whole product adjusts. One implementation note if you build this yourself: reading a custom property&#8217;s <em>resolved pixel value<\/em> in JavaScript requires measuring an element that actually uses it (e.g. <code>getBoundingClientRect().width<\/code>) \u2014 <code>getComputedStyle().getPropertyValue('--space-md')<\/code> alone returns the literal, unresolved <code>\"clamp(...)\"<\/code> text, not the computed number.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> design systems and any codebase with more than a handful of components, where consistent, fluid spacing matters more than any single element&#8217;s exact padding value. <strong>Tip:<\/strong> start with 3-4 tokens (sm\/md\/lg is often enough) rather than trying to clamp every possible spacing value individually \u2014 a scale you can hold in your head is more valuable than one that&#8217;s technically more precise.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common clamp() pitfalls<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Argument order matters, and CSS won&#8217;t warn you.<\/strong> <code>clamp()<\/code> expects minimum, preferred, maximum \u2014 in that order. Get the order wrong and the property doesn&#8217;t become invalid or fall back to anything \u2014 spec-wise, <code>clamp(MIN, VAL, MAX)<\/code> is defined as <code>max(MIN, min(VAL, MAX))<\/code>, so if your minimum is accidentally larger than your maximum, that formula just returns the minimum every time, no matter what the preferred value or the container width is. The property keeps applying; it just silently stops being fluid. There&#8217;s no console error to catch this for you \u2014 if a clamp()&#8217;d value looks frozen, check that the first argument is actually smaller than the third.<\/li>\n\n\n\n<li><strong>vw-only clamps ignore the user&#8217;s font-size zoom.<\/strong> A visitor who bumps their browser&#8217;s default font size to make text more readable expects <em>all<\/em> text to scale, including yours. If your floor and ceiling are in <code>px<\/code> or your preferred value is pure <code>vw<\/code> with no <code>rem<\/code> component, zooming does nothing. Mixing in <code>rem<\/code> for the floor\/ceiling (as in demo #1) keeps clamp()-driven type responsive to accessibility zoom, not just viewport width.<\/li>\n\n\n\n<li><strong><code>-webkit-line-clamp<\/code> is a different property.<\/strong> It truncates text to N lines with an ellipsis and has nothing to do with the <code>clamp()<\/code> function covered here \u2014 they just happen to share a name.<\/li>\n\n\n\n<li><strong>Container query units need a container.<\/strong> <code>cqw<\/code>\/<code>cqh<\/code> only resolve against an ancestor that has <code>container-type: inline-size<\/code> (or <code>size<\/code>) set. Forget that declaration and the browser has nothing to measure against.<\/li>\n\n\n\n<li><strong>clamp() replaces <em>some<\/em> media queries, not all of them.<\/strong> It&#8217;s excellent for continuous scaling of a length value. It can&#8217;t reorder elements, hide something at a breakpoint, or switch a layout from a grid to a stack \u2014 that&#8217;s still a job for <code>@media<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is CSS clamp()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>clamp()<\/code> is a CSS function that takes a minimum, a preferred, and a maximum value, and returns whichever one actually applies at the current size \u2014 it never computes below the minimum or above the maximum, and scales smoothly between them the rest of the time. It works on any property that accepts a length, not just <code>font-size<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the syntax of clamp()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>property: clamp(MINIMUM, PREFERRED, MAXIMUM);<\/code> \u2014 three comma-separated values in that exact order. The preferred value is usually written with a relative unit like <code>vw<\/code>, <code>%<\/code>, or <code>cqw<\/code> so it recomputes continuously as the viewport or container resizes, while the minimum and maximum stay fixed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can clamp() replace media queries?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For continuously scaling a single length value \u2014 a font-size, a padding, a gap \u2014 yes, one <code>clamp()<\/code> line can replace several breakpoints&#8217; worth of <code>@media<\/code> rules. It can&#8217;t do everything a media query can, though: it won&#8217;t reorder elements, hide something at a specific width, or switch a grid layout to a stacked one, so it complements <code>@media<\/code> rather than replacing it outright.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between vw and cqw in clamp()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>vw<\/code> ties the preferred value to the width of the browser viewport; <code>cqw<\/code> ties it to the width of the nearest ancestor with <code>container-type: inline-size<\/code> set on it. Use <code>vw<\/code> for page-level fluid values like a hero headline, and <code>cqw<\/code> when you want a component to respond to its own container&#8217;s width rather than the whole page \u2014 the same card sitting in a narrow sidebar versus a wide layout, for example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can clamp() be used for padding and spacing?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes \u2014 <code>clamp()<\/code> works on any length-accepting property, including <code>padding<\/code>, <code>margin<\/code>, <code>gap<\/code>, and <code>border-radius<\/code>. <code>clamp(12px, 5cqw, 40px)<\/code> on padding, for instance, keeps a card comfortably spaced at both a narrow and a wide container width, without a single fixed value looking cramped at one extreme or oversized at the other.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does clamp() work with CSS custom properties?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, and it&#8217;s one of the more useful patterns: define a custom property like <code>--space-md: clamp(10px, 3.5cqw, 28px)<\/code> once, then reference it with <code>var(--space-md)<\/code> across as many components as you like. Change the <code>clamp()<\/code> expression in one place and every element using that token updates together \u2014 the basis of a fluid spacing scale. One caveat: <code>getComputedStyle().getPropertyValue()<\/code> returns a custom property&#8217;s literal, unresolved text rather than its computed pixel value, so reading the resolved number back in JavaScript means measuring an element that actually uses it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Every demo above ships its full HTML, CSS and JS in the <strong>HTML \/ CSS \/ JS<\/strong> tabs in its own top bar \u2014 open any of them, copy the clamp() line you need, and it drops straight into your own project. But the better way to actually learn this is to click <strong>Fork &amp; Edit<\/strong> instead of just copying: it hands you the same snippet in a live editor where you can change the numbers and watch the preview update in real time. Retype demo #1&#8217;s coefficient from <code>5.2cqw<\/code> to <code>1cqw<\/code> and watch the heading freeze at exactly 20px across the whole drag range \u2014 the preferred value can no longer climb high enough to escape the floor. Push demo #3&#8217;s floor above its ceiling and watch the padding stop scaling entirely, frozen at that floor value no matter how you resize the box. Nudge demo #10&#8217;s <code>--space-md<\/code> ceiling and watch both the row spacing and the &#8220;md&#8221; swatch&#8217;s width move together, since both are wired to that one token. None of that sinks in from reading \u2014 it sinks in from breaking a value and seeing exactly what breaks. Once you&#8217;ve got a version you like, save it to <a href=\"https:\/\/webdevpuneet.com\/ui-snippets\/mycode\/\">My Code<\/a> and it&#8217;s yours to keep, tweak further, or reuse in a real project.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most people meet clamp() through one copy-pasted line of fluid typography and never go further. That is a shame, because clamp() is not a typography trick \u2014 it is a general-purpose way to say &#8220;pick a value between these two limits, and let it scale in between.&#8221; It works on padding, gaps, border-radius, letter-spacing, custom [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":42,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","advanced_seo_description":"Learn CSS clamp() with 10 draggable live demos: fluid type, padding, gaps, radius and spacing tokens. Edit each one free in your browser, no sign-up needed.","jetpack_seo_html_title":"CSS clamp() Explained with 10 Live Fluid-Sizing Demos","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Learn CSS clamp() with 10 draggable live demos: fluid type, padding, gaps, radius and spacing tokens. Edit each one free in your browser, no sign-up needed.\n","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[6,9],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","category-tutorials","et-has-post-format-content","et_post_format-et-post-format-standard"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":29,"url":"https:\/\/webdevpuneet.com\/blog\/how-to-learn-html-css-javascript-and-react-in-the-age-of-ai-without-getting-left-behind\/","url_meta":{"origin":41,"position":0},"title":"How to Learn HTML, CSS, JavaScript and React in the Age of AI (Without Getting Left Behind)","author":"admin","date":"October 1, 2026","format":false,"excerpt":"There's a fear going around, and if you're just starting out you've probably felt it: why learn front-end development at all when AI writes the code? Here's the honest answer. AI writes code fast, and it writes a lot of code that almost works. The gap between \"almost works\" and\u2026","rel":"","context":"In &quot;CSS&quot;","block_context":{"text":"CSS","link":"https:\/\/webdevpuneet.com\/blog\/category\/css\/"},"img":{"alt_text":"How to Learn HTML, CSS, JavaScript and React in the Age of AI (Without Getting Left Behind)","src":"https:\/\/i0.wp.com\/webdevpuneet.com\/blog\/wp-content\/uploads\/2026\/10\/unnamed.webp?fit=1200%2C630&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/webdevpuneet.com\/blog\/wp-content\/uploads\/2026\/10\/unnamed.webp?fit=1200%2C630&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/webdevpuneet.com\/blog\/wp-content\/uploads\/2026\/10\/unnamed.webp?fit=1200%2C630&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/webdevpuneet.com\/blog\/wp-content\/uploads\/2026\/10\/unnamed.webp?fit=1200%2C630&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/webdevpuneet.com\/blog\/wp-content\/uploads\/2026\/10\/unnamed.webp?fit=1200%2C630&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_shortlink":"https:\/\/wp.me\/phrh5V-F","jetpack_featured_media_url":"https:\/\/i0.wp.com\/webdevpuneet.com\/blog\/wp-content\/uploads\/2026\/10\/image1788860138.webp?fit=1200%2C630&ssl=1","_links":{"self":[{"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":1,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions\/43"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/media\/42"}],"wp:attachment":[{"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}