Use this Clamp() Calculation Generator by modifying the Widths, and Select the Font Size Units. Can be used for other areas such as Margins, and more (just modify/add Classes where required).
Using the fluid typography calculator is simple:
Fluid typography ensures your text scales seamlessly across different screen sizes, enhancing readability and user experience. By using relative units like REM and VW, fluid typography adapts to both small and large screens, creating a more responsive design. This eliminates the need for multiple media queries, making your design more efficient while maintaining consistent proportions. In short, it provides flexibility and saves time, giving your website a more polished, adaptable feel.
To use Font Clamp in Elementor, follow these simple steps:
For best results, apply the clamp to every global font in your Site Settings tab.
// Function to add the calculator to the admin menu
function ft_calculator_admin_menu()
{
add_menu_page(
"Fluid Typography Calculator", // Page title
"Typography Calculator", // Menu title
"manage_options", // Capability
"fluid-typography-calculator", // Menu slug
"ft_calculator_page" // Function that displays the page content
);
}
add_action("admin_menu", "ft_calculator_admin_menu");
// Function that generates the calculator page
function ft_calculator_page() {
// Initialize the $useRem variable at the start of the function
$useRem = false;
echo 'Fluid Typography Calculator
';
// Basic styles for the form and output
echo '';
// Initialize variables
$rootFontSize = isset($_POST["rootFontSize"]) ? $_POST["rootFontSize"] : 16;
$defaultValues = [
"hero-h1" => [
"MinWidth" => 380,
"MinFontSize" => 32,
"MaxWidth" => 1600,
"MaxFontSize" => 80,
],
"h1" => [
"MinWidth" => 380,
"MinFontSize" => 29,
"MaxWidth" => 1600,
"MaxFontSize" => 68,
],
"h2" => [
"MinWidth" => 380,
"MinFontSize" => 24,
"MaxWidth" => 1600,
"MaxFontSize" => 51,
],
"h3" => [
"MinWidth" => 380,
"MinFontSize" => 20,
"MaxWidth" => 1600,
"MaxFontSize" => 38,
],
"h4" => [
"MinWidth" => 380,
"MinFontSize" => 17,
"MaxWidth" => 1600,
"MaxFontSize" => 29,
],
"h5" => [
"MinWidth" => 380,
"MinFontSize" => 14,
"MaxWidth" => 1600,
"MaxFontSize" => 22,
],
"h6" => [
"MinWidth" => 380,
"MinFontSize" => 12,
"MaxWidth" => 1600,
"MaxFontSize" => 16,
],
"body" => [
"MinWidth" => 380,
"MinFontSize" => 16,
"MaxWidth" => 1600,
"MaxFontSize" => 24,
],
"p" => [
"MinWidth" => 380,
"MinFontSize" => 14,
"MaxWidth" => 1600,
"MaxFontSize" => 28,
],
"p-accent" => [
"MinWidth" => 380,
"MinFontSize" => 16,
"MaxWidth" => 1600,
"MaxFontSize" => 24,
],
];
$cssOutput = ""; // Initialize traditional CSS output string
$cssVariablesOutput = ""; // Initialize CSS variables output string
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$useRem = isset($_POST["unitToggle"]) && $_POST["unitToggle"] === 'on';
// Security check using nonce
if (!isset($_POST["ft_calculator_nonce_field"]) || !wp_verify_nonce($_POST["ft_calculator_nonce_field"], "ft_calculator_action")) {
echo "Security check failed. Please try again.
";
return;
}
foreach (["hero-h1", "h1", "h2", "h3", "h4", "h5", "h6", "body", "p", "p-accent"] as $tag) {
$minWidth = isset($_POST[$tag . "MinWidth"]) ? $_POST[$tag . "MinWidth"] : $defaultValues[$tag]["MinWidth"];
$minFontSizePx = isset($_POST[$tag . "MinFontSize"]) ? $_POST[$tag . "MinFontSize"] : $defaultValues[$tag]["MinFontSize"];
$maxWidth = isset($_POST[$tag . "MaxWidth"]) ? $_POST[$tag . "MaxWidth"] : $defaultValues[$tag]["MaxWidth"];
$maxFontSizePx = isset($_POST[$tag . "MaxFontSize"]) ? $_POST[$tag . "MaxFontSize"] : $defaultValues[$tag]["MaxFontSize"];
// Capture the original input values for the comments
$originalMinFontSizePx = $minFontSizePx;
$originalMaxFontSizePx = $maxFontSizePx;
// Handle unit conversion
if ($useRem) {
// Convert REM to Pixels for internal calculation
$minFontSizePx *= $rootFontSize;
$maxFontSizePx *= $rootFontSize;
}
// Convert pixel values to REM for CSS generation
$minFontSize = $minFontSizePx / $rootFontSize; // In rem
$maxFontSize = $maxFontSizePx / $rootFontSize; // In rem
// Convert widths from px to vw (assuming 1rem = rootFontSize px)
$minWidthVW = $minWidth / $rootFontSize; // In vw
$maxWidthVW = $maxWidth / $rootFontSize; // In vw
// Calculate the CSS
$vwUnit = (($maxFontSize - $minFontSize) / ($maxWidthVW - $minWidthVW)) * 100;
$constant = $minFontSize - ($vwUnit * $minWidthVW) / 100;
// Format to a maximum of 5 decimal places
$vwUnitFormatted = number_format($vwUnit, 5, ".", "");
$constantFormatted = number_format($constant, 5, ".", "");
// Add to traditional CSS output
if ($tag === "hero-h1") {
$cssOutput .=
".hero-h1 h1 {font-size: clamp(" .
$minFontSize .
"rem, " .
$constantFormatted .
"rem + " .
$vwUnitFormatted .
"vw, " .
$maxFontSize .
"rem); /* " .
number_format($originalMinFontSizePx, 0) .
"px to " .
number_format($originalMaxFontSizePx, 0) .
"px */ /* .hero-h1 CSS class for homepage hero section */}\n";
} elseif ($tag === "p-accent") {
$cssOutput .=
".p-accent {font-size: clamp(" .
$minFontSize .
"rem, " .
$constantFormatted .
"rem + " .
$vwUnitFormatted .
"vw, " .
$maxFontSize .
"rem); /* " .
number_format($originalMinFontSizePx, 0) .
"px to " .
number_format($originalMaxFontSizePx, 0) .
"px */ /* .p-accent CSS class for Subheadings and accents */}\n";
} else {
$cssOutput .=
"{$tag} {font-size: clamp(" .
$minFontSize .
"rem, " .
$constantFormatted .
"rem + " .
$vwUnitFormatted .
"vw, " .
$maxFontSize .
"rem); /* " .
number_format($originalMinFontSizePx, 0) .
"px to " .
number_format($originalMaxFontSizePx, 0) .
"px */}\n";
}
// Add to CSS variables output
$cssVariablesOutput .=
" --{$tag}-font-size: clamp(" .
$minFontSize .
"rem, " .
$constantFormatted .
"rem + " .
$vwUnitFormatted .
"vw, " .
$maxFontSize .
"rem); /* " .
number_format($originalMinFontSizePx, 0) .
"px to " .
number_format($originalMaxFontSizePx, 0) .
"px */\n";
}
// Wrap CSS variables in a :root selector
$cssVariablesOutput = ":root {\n" . $cssVariablesOutput . "}\n";
}
// Display the form
echo '';
// Display the traditional CSS output
if (!empty($cssOutput)) {
echo "Generated CSS:
";
echo '
";
echo '';
}
// Display the CSS variables output
if (!empty($cssVariablesOutput)) {
echo "Generated CSS Variables:
";
echo '
";
echo '';
}
// JavaScript for copy to clipboard functionality and unit conversion
echo '';
echo "";
}
// Define the function for the fluid typography calculator
function fluid_typography_calculator() {
// Call the existing function to display the fluid typography calculator
ft_calculator_page();
}
// Add the shortcode
add_shortcode('Fluid_calc', 'fluid_typography_calculator');
Step 1: Install the plugin “Code Snippet” or whatever you prefer.
Step 2: Add a new PHP Snippet
Step 3: copy the code from above.
Step 4: Click Save!
Step 5: Add the shortcode * [ In Brackets ] * Fluid_calc Or fluid_typography_calculator to the page you want to use it on.
Step 6: Publish changes and enjoy!
We Are A Web Design & SEO Company Who Builds Websites To Rank On Google.
© 2024 Madd X Media LLC & Savage Web Solutions - All Rights Reserved