- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS Function - clamp()
The CSS clamp() function keeps a value within a range defined by a minimum and a maximum value.
It has three parameters: a minimum value, a preferred value, and a maximum allowed value.
Possible Values
The clamp(min, val, max) function takes three comma-separated expressions as parameters.
min - The minimum value represents the smallest or most negative value and is the lower limit of the allowable range. If the preferred value is lower than this minimum value, the minimum value is used.
val - The preferred value is the expression used when it is within the range defined by the minimum and maximum values.
max - If the preferred value exceeds this upper limit, the maximum value is assigned to the property, i.e., the largest (most positive) allowed expression value.
Expressions in clamp() can include math functions, literal values, or other valid argument types. Direct addition, subtraction, multiplication and division are allowed.
Parentheses can be used to specify the order of calculations.
Points to remember:
Percentages in table dimensions (widths and heights) are interpreted as auto for columns, column groups, rows, row groups, and cells in both auto and fixed layout tables.
Nesting of max() and min() functions is allowed in CSS expressions, where inner functions are treated like parentheses.
Full math expressions, including addition, subtraction, multiplication, and division, can be used without requiring the calc() function in nested max() and min() functions.
The expression may contain addition (+), subtraction (-), multiplication (*), and division (/) operators, following the usual precedence rules.
Spaces should be added on either side of + and - operands. The operands can be any syntax <length> values and use different units. Parentheses may be used to control the order of calculations.
Frequently it may be necessary to use min() and max() functions within a clamp() function.
Return Value
The clamp(MIN, VAL, MAX) function is resolved as max(MIN, min(VAL, MAX)).
The function returns a value of type <length>, <frequency>, <angle>, <time>, <percent>, <number> or <integer>, , depending on the given parameters.
Syntax
clamp( <calc-sum>#{3} )
The hash (#) mark multiplier indicates that the entity may be repeated one or more times (here 3 times), but each occurrence is separated by a comma (',').
CSS clamp() - Basic Example
The following example demonstrates the use of CSS clamp() function to set responsive box widths, ensuring they scale between specified minimum and maximum values while adapting to the parent container's width.
Resize the browser window to see how width of boxes is adjusted according to viewport with the help of the clamp function.
<html> <head> <style> .container { display: flex; flex-wrap: wrap; justify-content: center; } .box { width: clamp(200px, 40%, 300px); height: 150px; margin: 20px; background-color: lightblue; display: flex; justify-content: center; align-items: center; font-size: 18px; font-weight: bold; } </style> </head> <body> <div class="container"> <div class="box">Box 1</div> <div class="box">Box 2</div> <div class="box">Box 3</div> <div class="box">Box 4</div> </div> </body> </html>
To Continue Learning Please Login
Login with Google