Favorites

max() CSS Function

max()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The max() function returns the largest of a list of values, such as max(50%, 300px), which never gets narrower than 300px.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The max() CSS function allows you to calculate the largest value from a list of two or more expressions. It is particularly useful when you want to define responsive sizes that adapt to different screen widths or conditions, ensuring a property never falls below a certain minimum. Unlike simple fixed values, max() can take relative units like percentages, viewport units, or other functions such as clamp() to create flexible layouts.

This function is often used with font-size, width, height, and margin to combine fixed and relative values. For example, you could ensure that a paragraph text never shrinks below 16px but can scale with the viewport width:

p {
  font-size: max(16px, 2vw);
}

Here, the browser will choose the larger value between 16px and 2vw, meaning on very small screens the font stays at 16px, but on larger screens it can grow proportionally.

Another practical use is for layout elements that should maintain a minimum size:

div.container {
  width: max(300px, 50%);
}

In this case, the container will be at least 300px wide, but can expand to 50% of its parent if that is larger.

The max() function can accept any number of comma-separated expressions and supports calculations using calc(). It helps create more robust, responsive designs without resorting to media queries for simple minimum thresholds.

All values inside max() must resolve to the same type, but they can use different units of that type: lengths such as px, em, rem, vw and percentages can be freely mixed, because the browser converts them all to pixels before comparing. Mixing different types, such as a length and a time, is invalid.

Interactive Demo

050px100px150px200px250px
Demo Code
HTML Code
<div class="demo-track">
	<div class="demo-element"></div>
</div>
<div class="demo-scale"><span>0</span><span>50px</span><span>100px</span><span>150px</span><span>200px</span><span>250px</span></div>
CSS Code
.demo-track {
	width: 300px;
	height: 36px;
	background: #f4f4f4 repeating-linear-gradient(90deg, #b0b0b0 0 1px, transparent 1px 50px);
	outline: 1px solid #c5c5c5;
}
.demo-scale {
	display: grid;
	grid-template-columns: repeat(6, 50px);
	width: 300px;
	font-size: 11px;
	color: #666;
}
.demo-element {
	height: 100%;
	background: linear-gradient(90deg, #f72585, #b5179e);
	width: max(50%, 100px);
}

Syntax

max() = max( value1, value2, ... )

Values

  • value1, value2, ...A list of comma-separated values - where the largest value is chosen. Required.

Example

<div class="container">
<div class="responsive-card">
<h2>Dynamic Scaling</h2>
<p>This card uses the max() function for its width.</p>
</div>
</div>
.container {
width: 100%;
padding: 20px;
background-color: #f0f0f0;
}

.responsive-card {
/* The width will never be smaller than 300px */
/* If 50% of the screen is more than 300px, it will take up 50% */
width: max(300px, 50%);

background-color: #007bff;
color: white;
padding: 20px;
border-radius: 8px;
margin: 0 auto;
text-align: center;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS max() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.