The Wayback Machine - https://web.archive.org/web/20240913104358/https://www.geeksforgeeks.org/css-clamp-method/
Open In App

CSS clamp() Method

Last Updated : 05 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The clamp() CSS function allows us to set a value that adjusts responsively between a defined minimum and maximum range, depending on the viewport size. This function is particularly useful for creating responsive layouts and typography.

The clamp() method in CSS allows you to specify a value within a range. It takes three parameters:

  1. Minimum value: The lower bound.
  2. Preferred value: The value you want to use if it falls within the specified range.
  3. Maximum value: The upper bound.

The browser selects the preferred value if it falls within the specified range; otherwise, it chooses the minimum or maximum value. This function is useful for various properties like font-size, width, and more.

It’s important to consider the minimum value when the desired value is smaller than the minimum and to consider the maximum value when the desired value is larger than the maximum. The desired value is useful when it falls between the minimum and maximum values.

Syntax : 

clamp(value1, value2, value3)

Parameters:

  • value1 represents the minimum value.
  • value2 represents the preferred value
  • value3 represents the maximum value.

Example 1:

In this example, we see how easily the width and font-size are adjusted according to viewport with the help of the clamp function. The clamp() function is very useful for typography and for creating fluid layout

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS clamp() Example</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
            background: #f0f0f0;
        }

        .container {
            text-align: center;
        }

        h1 {
            font-size: clamp(2rem, 4vw, 4rem);
            color: #eb4034;
            margin-bottom: 20px;
        }

        .box {
            width: clamp(150px, 50%, 400px);
            height: 8rem;
            background: #5f76e8;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-size: clamp(1rem, 2vw, 1.5rem);
            border-radius: 8px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Welcome To GFG</h1>
        <div class="box">
            Responsive Box
        </div>
    </div>
</body>

</html>

Output:

a1


In this example, the h1 font size and the box width adjust smoothly based on the viewport size. The clamp() function is particularly useful for creating fluid layouts and typography.

Example 2: In below example we have created a card which adjusts its size and padding based on the viewport width using the clamp() function.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS clamp() Example - Responsive Card</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
            background: #f7f7f7;
        }

        .card {
            width: clamp(250px, 50%, 600px);
            padding: clamp(1rem, 2vw, 2rem);
            background: white;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .card h2 {
            font-size: clamp(1.5rem, 3vw, 2.5rem);
            color: #333;
            margin-bottom: 10px;
        }

        .card p {
            font-size: clamp(1rem, 2vw, 1.5rem);
            color: #666;
            margin: 0;
        }
    </style>
</head>

<body>
    <div class="card">
        <h2>Responsive Card</h2>
        <p>This card adjusts its size and padding based on the viewport width using the clamp() function.</p>
    </div>
</body>

</html>

Output:

a

Supported Browsers:



Similar Reads

CSS webkit-line-clamp Property
The -webkit-line-clamp property is used to limit the amount of lines that a block container may contain. This property only has an effect when the display property is set to '-webkit-box' or '-webkit-inline-box' and the '-webkit-box-orient' property is set to 'vertical'. Syntax: -webkit-line-clamp: none | integer | initial | inherit Property Values
3 min read
How to use line-clamp Class for Text Truncation in Tailwind CSS ?
The line-clamp-[number] Class in Tailwind CSS is used for text truncation by limiting the number of displayed lines. This is particularly useful for creating ellipsis in multi-line text when the content exceeds a specified number of lines. Syntax&lt;div class="line-clamp-3"&gt; &lt;!-- Text content --&gt;&lt;/div&gt;&lt;div class="truncate"&gt;
1 min read
Lodash _.clamp() Method
Lodash _.clamp() method is used to clamp numbers within the inclusive range within the lower and upper bounds. Syntax:_.clamp(number, lower, upper);Parameters: number: This parameter holds the number to clamp.lower: This parameter holds the lower bound.upper: This parameter holds the upper bound.Return Value:This method returns the clamped number.
1 min read
D3.js | d3.continuous.clamp() Function
The d3.continuous.clamp() function in D3.js is used to enable or disable the clamp. If the clamp is disabled then the range of domain's value might be out of range but if the clamp is enabled then the range of domain's value will always be in range. Syntax: continuous.clamp( Value ) Parameters: This function accept single parameter Value which is e
2 min read
D3.js pow.clamp() Function
The pow.clamp() function in d3.js is used to enable the clamp or disable the clamp only if the clamp is specified. The range of the return value may be outside the given range if the clamp is disabled. Syntax: pow.clamp(clamp); Property Values: This function accepts single parameter as given above and described below. clamp: It accepts a boolean va
2 min read
D3.js log.clamp() Function
The log.clamp() function is used to enable the clamp or disable the clamp. If the clamp is disabled then the range of the return value may be outside the given range through extrapolation. Syntax: log.clamp(clamp); Parameters: This function accepts single parameter as mentioned above and described below. clamp: A boolean value. Return Value: This f
2 min read
D3.js time.clamp() Function
The time.clamp() function in D3.js is used to enable or disable the clamp. If the clamp is disabled then the range of the return value may be outside the given range. Syntax: time.clamp( clamp ) Parameters: This function accepts only one parameter as given above and described below: clamp: It is a boolean value that defines if the clamp is enabled
2 min read
Difference between bootstrap.css and bootstrap-theme.css
Bootstrap is an HTML, CSS and JS front-end-framework used for developing responsive and mobile-first projects on the web. It is the framework that comes up with predefined terms, thus giving you the ability to use these codes instead of having to create the codes from scratch. It is a collection of HTML, CSS and JS code that is designed to help bui
5 min read
Difference between CSS Grid and CSS Flexbox
Grid:CSS Grid Layout, is a two-dimensional grid-based layout system with rows and columns, making it easier to design web pages without having to use floats and positioning. Like tables, grid layout allow us to align elements into columns and rows. To get started you have to define a container element as a grid with display: grid, set the column an
4 min read
Which CSS framework is better Tailwind CSS or Bootstrap ?
In this article, we will see which CSS framework is better tailwind CSS or bootstrap. Both are widely used CSS frameworks for styling the front end. We will understand each one of them in detail. Both frameworks have their own pros and cons so it depends upon the type of application you are going to build and accordingly you can choose the best fra
5 min read
CSS Cheat Sheet - A Basic Guide to CSS
What is CSS? CSS i.e. Cascading Style Sheets is a stylesheet language used to describe the presentation of a document written in a markup language such as HTML, XML, etc. CSS enhances the look and feel of the webpage by describing how elements should be rendered on screen or in other media. What is a CSS Cheat Sheet? CSS Cheat Sheet provides you wi
13 min read
How to identify unused CSS definitions from multiple CSS files in a project ?
In this article, we will learn how to identify unused CSS definitions from multiple CSS files in a project. What are unused CSS definitions? Unused CSS definitions are the stylesheets that are completely useless for rendering and loading the page. If you remove these unused CSS definitions then nothing will happen to your webpage. Why you should re
2 min read
Create a CSS Fireflies background using HTML/CSS
In this article, we will see how to create the CSS Fireflies background using HTML &amp; CSS, along with understanding its implementation through the illustration. The CSS fireflies effect can be created by utilizing the CSS animation and we will implement 3 animations to achieve the fireflies effect. To create randomness, we used different animati
7 min read
Using Primer CSS - A Simple and Transparent CSS Library for Web Development
Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation of a document written in HTML or XML. CSS helps you to control the layout and appearance of web pages. In this article, we will discuss the concept of Primer CSS, a CSS library that provides a simple and easy-to-use set of styles for web development. Obvious an
3 min read
How to Add a @tailwind CSS Rule for CSS Checker ?
Tailwind CSS is basically a Utility first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. In this article, we will ex
4 min read
CSS pseudo elements every CSS developer should know
CSS pseudo-elements are used to add special effects/styles to a specific part of the selected elements. A CSS selector selects the HTML element(s) for styling purposes. CSS selectors select HTML elements according to its id, class, type, attribute etc. For example, the ::before and ::after pseudo-elements can be used to insert content before or aft
5 min read
CSS Complete Guide - A to Z CSS Concepts
What is CSS ? CSS stands for "Cascading Style Sheet". It is used to style HTML Documents. CSS simplifies the process of making web pages presentable. It describes how web pages should look it prescribes colors, fonts, spacing, and much more. What is CSS Complete Guide ? CSS Complete Guide is a list of A to Z CSS concepts from beginner to advanced l
5 min read
Styling React Components: CSS vs CSS-in-JS
When it comes to styling React components, developers often face the dilemma of choosing between traditional CSS and CSS-in-JS solutions. Both approaches offer unique advantages and drawbacks, influencing how developers design and maintain their application's user interface. In this article, we delve into the differences between CSS and CSS-in-JS,
5 min read
How to use CSS Animations with Tailwind CSS ?
Tailwind CSS classes are used to style elements and apply animations effortlessly. Utilize Tailwind's animation utility classes to add dynamic visual effects. Combine Tailwind CSS with custom CSS animations for versatile and engaging web designs. Table of Content Tailwind CSS Animation Utility ClassesCSS Animations with Tailwind CSS and JavaScriptT
3 min read
How to override the CSS properties of a class using another CSS class ?
To override CSS properties with another class, use the `!important` directive in CSS, emphasizing a style's importance, overriding others. Applying a new class to an element replaces or modifies styles from its original class, allowing targeted adjustments to appearance, layout, or behavior. Table of Content Using !important propertyUsing specific
3 min read
How to Play and Pause CSS Animations using CSS Custom Properties ?
In this article, we will see how to control CSS animations using custom properties, along with knowing their implementation through examples. CSS Animations are the automatic transitions that can be made on the web page which lets an element change from one style to another. CSS Custom Properties are variables defined by CSS developers that contain
5 min read
How to use CSS in different dimension (CSS-in-JS) ?
CSS is awesome and easy to get started with, but front-end applications have been scaling at a massive and complex rate that doesn't make the current CSS designed for the job. CSS-in-JS is a real deal and in many ways, is very much the best nowadays when it comes to building and styling applications on the web. What is CSS-in-JS: Just as the name i
6 min read
jQuery css() Method
The css() method in jQuery is used to change the style property of the selected element. This method can be used in different ways. The css() method can be used to get/return the present value of the property for the selected element. Syntax: $(selector).css(property)or $(selector).css(property, value)or $(selector).css(property, function(index, cu
2 min read
CSS Style Declaration removeProperty() Method
The removeProperty() method is used to remove the existing CSS property. Syntax: It is used to remove the specified CSS property. object.removeProperty(propertyname)Parameters: It accepts a single parameter: propertyname: It is a required parameter that contains a string that represents the name of the property to be removed.Return value: It return
1 min read
What is the use of css() method in jQuery ?
In this article, we will see how to use css() method to set the styles on elements dynamically using jQuery. The css() method is used to change the style property of the selected element. basically, The CSS() method is used to get the value of a certain CSS property that has been applied to a specific HTML element. Additionally, it is used to set t
2 min read
How to Set background Image using jQuery css() Method ?
This article will show you how to set the background image using jQuery. To set the background image, we are using the jQuery css() method. jQuery css() MethodThis method sets/returns one or more style properties for the specified elements. Syntax: Return a CSS property:$(selector).css("propertyname");Set a CSS property:$(selector).css("propertynam
2 min read
How to use steps() method in CSS Animations ?
The steps() method is a timing function in animation property that divides an animation into n steps, displaying each step at equal intervals of time. For example, if n is 2 then it will divide the animation into 2 parts. It divides the duration from 0% to 100% into 2 parts which are 0% - 50% and 50% - 100% respectively. Syntax:steps( n , jumpterm
5 min read
Difference between Class.method and Class.prototype.method
JavaScript is an object-oriented programming language, but unlike its peers (which are class-based), JavaScript is a prototype-based language. It means that in JavaScript, you can create an object (prototype object) that acts as a template for new objects. These new objects can be provided with new properties either when you create them or at run t
2 min read
Node.js Http2ServerRequest.method Method
The Http2ServerRequest.method is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the string representation of the request method. Syntax: const request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the string represe
3 min read
Node.js http.IncomingMessage.method Method
The http.IncomingMessage.method is an inbuilt application programming interface of class Incoming Message within the inbuilt http module which is used to get the type of request method as a string. Syntax: request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the request type name as a
2 min read