wp_get_computed_fluid_typography_value()
Calculates a fluid typography value as a CSS clamp() function.
To calculate it, you need to specify the minimum and maximum viewport width, as well as the minimum and maximum font size.
The function normalizes the provided sizes and, based on linear interpolation, assembles a formula of the form clamp(min, preferred, max) for use in the CSS property font-size.
The function is internal and is used by the core when calculating fluid values in theme.json.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
String|null.
-
string- the font size using clamp(). null- returned if the parameters are specified incorrectly, or if the calculation results in division by zero.
Usage
wp_get_computed_fluid_typography_value( $args );
- $args(array)
Parameters for calculating the clamp() value:
-
minimum_viewport_width(string)
Minimum viewport— from which the size should start increasing. -
maximum_viewport_width(string)
Maximum viewport— up to which the size should increase. -
minimum_font_size(string)
Minimum font size. For example, 16px, 1rem. -
maximum_font_size(string)
Maximum font size. - scale_factor(integer)
A multiplier that controls how quickly the font grows between the specified limits. Default: 1 — smooth change from boundary to boundary.
-
Examples
#1 Using a fallback static size
This example shows how you can hedge in case the function returns null, and display a static font size instead of a fluid formula.
$args = [ 'minimum_viewport_width' => '360px', 'maximum_viewport_width' => '1440px', 'minimum_font_size' => '1.2rem', 'maximum_font_size' => '2rem', 'scale_factor' => 0.9, ]; $fluid_font_size = wp_get_computed_fluid_typography_value( $args ); $font_size_css = $fluid_font_size ? $fluid_font_size : '1.6rem'; ?> <p style="font-size: <?php echo esc_attr( $font_size_css ); ?>"> Paragraph text with a fluid or static font size. </p>
#2 Demo
$args = [ 'minimum_viewport_width' => '320px', 'maximum_viewport_width' => '1200px', 'minimum_font_size' => '1rem', 'maximum_font_size' => '3rem', 'scale_factor' => 1, ]; echo wp_get_computed_fluid_typography_value( $args ); // clamp(1rem, 1rem + ((1vw - 0.2rem) * 3.636), 3rem) $args['scale_factor'] = 2; echo wp_get_computed_fluid_typography_value( $args ); // clamp(1rem, 1rem + ((1vw - 0.2rem) * 7.273), 3rem)
Changelog
| Since 6.1.0 | Introduced. |
| Since 6.3.0 | Checks for unsupported min/max viewport values that cause invalid clamp values. |
| Since 6.5.0 | Returns early when min and max viewport subtraction is zero to avoid division by zero. |