Understanding CSS min() max() clamp() and minmax() functions

Home » CSS » Understanding CSS min() max() clamp() and minmax() functions

Responsive web design has been around for a while. It’s constantly evolving and providing its authors to stay DRY and dynamic. CSS offers many functions for calculating widths within CSS. Most underutilized CSS functions are min(), max(), minmax(), and clamp().

To use CSS min(), max(), and minmax(), we have to specify at least 2 values in the function. For the clamp function, we have to pass exactly 3 values. Min min function will select the minimum from the given values. The max function will select the maximum of the given values. Minmax will select a value range greater than or equal to min and less than or equal to max. The clamp will select an intermediate value between the min and max range.

To put it in simple terms:

  • Min() is like getting the lowest score in a test.
  • Max() is like getting the highest score in a test.
  • Clamp() is like getting a score somewhere in the middle of the two extremes.

min(), max(), and clamp() functions can be used to create responsive layouts as well as responsive fonts, the minmax() function is used for setting up values for the grid templates. Let’s understand these CSS min, max, and clamp functions with examples.

The CSS min() function:

It calculates and applies the smallest value from the specified value range. The inversion of min in this application is max, which sets the minimum value for all things. A sometimes confusing point with min and max is that when you use max to have a minimum value on a property, such as min-width and then use max instead of min because min imposes a maximum value, it is easy to get the opposite function when you try to use min to add a minimum size.

If you see the example below, You’d find a solution to a real-world problem when you have a static-size ad on the sidebar and you want it visible most of the time. You can do that using the max-width property or by using the min function.

Syntax:

property: min(value1, value2, value3);

  • The min function will take any valid length as an argument.
.layout{
   display: grid;
   grid-template-columns: 3fr auto;
}
main{
   width:100%;
}
aside{
   width: min(100%,300px);
}

Observe the sidebar in the example. It will take the minimum value from 300px and 100%. It will take the smallest value available to the property. In this case, it’s width. Media queries can be used to change the min values of the element.

Our technical experts can help fix any issue you are having with your website, regardless of its complexity.

FIND OUT MORE

The CSS max() function:

It is calculated by applying the largest value to the specified range of values. The max () function contains one or more comma-separated values and represents the greater value of each of these calculations. You can also use different units of value in your expression, or different units for the mathematical functions that make up the arguments.

Syntax:

property: max(value1,value2,value3);

  • The max function will take any valid length as an argument.

We’ll use the same example, but this time, instead of min, we’ll use max for the sidebar.

.layout{
   display: grid;
   grid-template-columns: auto auto;
}
main{
   width:100%;
}
aside{
   width: max(100%,300px);
}

Observe the sidebar. The layout size shrinks to full width to 200px. But we’re using the max function in the example. So the sidebar will take 300px as a max when the layout goes below 300px. This can be seen when the layout shrinks. We can also use a media query to add breakpoints and use the max function.

The CSS clamp() function:

It can be used for length, frequency, angle, time, percentage, or any number of integers allowed. The clamp function takes three comma-separated expressions as parameters, in order of minimum, preferred, and maximum value.

Mathematical CSS functions use most of the properties that allow numerical values. There are four supported mathematical functions in CSS that can be used in unexpected ways, such as gradients, colors, functions, or in combination with custom CSS properties.

These functions bring native CSS to a simpler format. For example, while setting the width property, the max() function behaves like setting values for the width and min-width properties, min() function behaves like setting values for max-width and width properties individually. It saves lines for code for the width property, but not all properties have the min and max ranges.

Syntax:

property: clamp(minimum, preferred, maximum);

  • The clamp function will take any valid length as an argument.

For the creation of responsive fonts, it could be difficult to get a variety of fonts using relative units. The clamp() function can just solve that problem to some extent. We can clamp viewport units vw and vh using the clamp function to get perfect responsive fonts.

Observe the following example. See how the font size stays responsive even after scaling the viewport? That is because the font size is locked between the minimum and maximum size.

The CSS minmax() function:

It can be used to specify values from minimum to maximum to specify the width and height of a CSS grid column or row. It opens the door for us to write more powerful and concise CSS by allowing us to set values in the grid that follow the function, including minimum and maximum values.

We can use various types of values for the Minmax () function, and it all depends on what kind of custom grid we want to create. We can use it for the values of the grid template columns and grid template row properties.

Syntax:

grid-template-columns: minmax(min, max)

  • Length
  • Percentage
  • Flexible Length
  • max-content
  • min-content
  • auto

The minmax property only works with grid systems. Unlike min, max, and clamp, it does not work with other properties. This function can really be helpful in case you’re using grid systems to define your layouts.

We’ll use the same example as MDN. We’ll tweak this example to see it shrinking and growing. Click on the radio buttons to try the different grid column structures in the example.

Observe: We have defined 3 columns for the demo. The first element has a bit more content than the others.

The first option will make the first column vary between 20px and auto.

The second option will make it vary between 0px and 1 fragment.

The third option allows the column to vary between 2 to 10 character widths.

The fourth and fifth options are using special keywords so they have different behavior depending on their usage.

If these width units don’t make sense, then let’s explore what values we can have for min and max arguments.

  • Length: It accepts a valid length value.
  • Percentage: It accepts valid percentage values as min or max.
  • Flexible Length: It accepts a valid number unit with a fr unit. The fr unit represents a fraction of the free space in the grid container.
  • max-content: This keyword represents the cell’s ideal size. This is the smallest possible size the cell can be, while still fitting around its contents unobtrusively.
  • min-content: This keyword represents the smallest possible size the cell can be, which does not lead to an overflow unless that overflow is unavoidable.
  • auto: This keyword can take different values depending on whether it is used as the maximum or minimum value in the minmax() function. If used as a max, it is equivalent to the max-content value. If used as a min, it represents the largest minimum size the cell can be.

Our technical experts can help fix any issue you are having with your website, regardless of its complexity.

FIND OUT MORE

What are the benefits of using CSS min, max, and clamp?

When it comes to web development, one of the most important aspects is CSS. These properties allow you to set the minimum, maximum, and clamped values for a given element. This can be extremely useful in a variety of situations, such as ensuring that an element is always visible or preventing it from becoming too small or large.

Overall, the benefits of using min, max, and clamp in CSS are numerous. If you’re not already using these properties in your web development projects, you should consider doing so – you’ll be glad you did!

What are the limitations of CSS min, max, and clamp?

The limitations of CSS min, max, and clamp are that these properties only work on linear scale values. Additionally, they can be tricky to use with percentage values. And finally, browser support is still somewhat limited.

Using Min Max Clamps With Media Queries

When it comes to responsive design, min and max clamps can be extremely useful. By setting a minimum and maximum width for your elements, you can ensure that they always stay within a certain size range. This can be especially helpful when using media queries to make your site more responsive.

min and max clamps can be applied to almost any element on your page. For example, you could use them to set the width of your content area or the height of your sidebar. By doing this, you can make sure that your content always looks its best, no matter what device it’s being viewed on.

If you’re not sure how to use min and max clamps, don’t worry – they’re quite easy to use once you start using them.

So how good are min, max, and clamp functions?

If you have imagined a responsive layout without media queries, well, using the clamp function, you can achieve it at some levels. You can experiment with these functions for your next project. They are like a magic trick. It could reduce much code in your CSS. Minimax function, on the other hand, can give you more control over your grid system to build complex layouts. There are tons of enhancements that you can do with your content depending on its height or width. UI designers can optimize their typography and components using this simple trick because device size matters.

Continue Reading

rhodium

Rhodium Developers

Framework: WordPress – Elementor
Requirement: New custom-made website with SEO optimization
shangri_la
development

Problem

It’s a start-up company and it did not have enough stuff to showcase its work. It faced the challenge of establishing a strong online presence to attract potential clients. They wanted a visually appealing website that would convey their professionalism and expertise.

Our Plan

Execution

1A premium website template was designed in Figma. Blueprints of the home page and other internal pages were created. The color palette has been chosen wisely to exude elegance without compromising usability – sleek beige typography against crisp black backgrounds creates an impactful visual contrast that enhances readability and attention. 

2By using strong visuals such as high-quality images and graphics showcasing their past projects, we aimed to convey their expertise and build trust with potential clients. The overall tone we maintained throughout the design process was professional yet approachable, reflecting the client’s brand image as a reliable and innovative construction company.

3 By incorporating intuitive navigation features and user-friendly interfaces, we ensured seamless browsing experiences for potential clients. By focusing on creating internal pages that offered valuable content and intuitive navigation, we were able to provide users with an immersive and informative experience. With intuitive menu options, easy-to-use contact forms, and visually appealing project showcases, visitors are certain to find exactly what they need with minimal effort.

4 One of the primary goals for this project was to showcase the exceptional quality of their work through an impressive gallery section. By leveraging the power of visually appealing images and intuitive navigation, we successfully transformed their website into a virtual gallery that entices visitors to explore more.

5 A major challenge encountered during the project was effectively showcasing Rodium’s unique approach to building construction. By incorporating captivating visuals and interactive elements, such as 2D models and virtual tours, the website successfully demonstrates the intricate processes involved in constructing high-quality buildings. Additionally, clear and concise descriptions were implemented to explain each step of the construction journey, making it easily understandable for potential clients who may not be familiar with technical jargon.

Final Testing

During the testing phase, every aspect of the website will be put under a microscope to ensure its seamless functionality and performance. From checking cross-browser compatibility to ensuring smooth navigation on different devices and screen sizes, no stone will be left unturned. The comprehensive testing process aims not only to identify any potential glitches or bugs but also to guarantee that the user experience is intuitive and flawless.

By utilizing the latest techniques in search engine optimization algorithms, this website is now primed to attract organic traffic and boost its online presence.