Manipulating fake elements in CSS to get additional control over CSS styling. Pseudo-selectors select elements in a particular state. Pseudo-elements are the Real but inaccessible elements.
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 functions are min(), max(), minmax() and clamp().
To use CSS min(), max(), 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 minimum from the given values. 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.
min(), max(), 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 functions with examples.
The 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 max-width property or by using the min function.
property: min(value1, value2, value3);
- min function will take any valid length as an argument.
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.
The 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.
- 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 the max for the sidebar.
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.
The 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 width property, the max() function behaves like setting values for width and min-width properties, min() function behaves like setting values for max-width and width properties individually. It saves line for code for width property but all the properties don't have the min- and max- ranges.
property: clamp(minimum, preferred, maximum);
- 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. 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 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.
grid-template-columns: minmax(min, max)
- Flexible Length
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's 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 others.
The first option will make the first column vary between 20px and auto.
The second option will make it vary between 0px and 1fragment.
The third option allows the column to vary between 2 to 10 character width.
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 can we 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 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 if 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.
So How good are min, max, and clamp functions are?
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.