Explain Codes LogoExplain Codes Logo

How to set min-font-size in CSS

html
responsive-design
css-functions
accessibility
Anton ShumikhinbyAnton Shumikhin·Oct 29, 2024
TLDR

To establish minimum font size in CSS, use calc() along with vw (view width):

.text { /* Even a small screen has no chance to shrink the text size below 16px */ font-size: calc(16px + 1vw); }

Here, the font size has a defined minimum of 16px, allowing for proportionate scaling with the screen size.

Instructions for setting up minimum font size

When focusing on responsive typography, the goal is to ensure readability across multiple devices. Let's get started with the most basic and common approach using calc() and gradually move into more advanced strategies:

1. The power of calc()

The calc() function, a handy CSS feature, combines fixed and relative units yielding dynamic font size adaptation:

.text { /* Old McDonald had a font, E-I-E-I-O, with a calc-calc here... */ font-size: calc(16px + 1vw); }

2. The magic of clamp()

The CSS clamp() function offers a robust method to define a responsive font size range:

.text { /* Clamps! Not just for plumbing anymore. */ font-size: clamp(12px, 2vw, 1.5em); }

This communicates to the browser to use 12px as the minimum font size, sizes preferentially at 2vw, but to limit the scale at 1.5em.

3. Setting a font size floor with max()

The max() function lets you set a floor on the minimum font size, preventing underscaling:

.text { /* Like a hard floor, but for fonts. No falling below 12px! */ font-size: max(12px, 1em); }

4. Accommodating screen widths using media queries

Consider the use of media queries to respectively alter font sizes based on screen dimension:

/* When the phone's too small, make the font stand tall! */ @media (max-width: 600px) { .text { font-size: 12px; } }

5. Scaling with viewport units

Viewport units such as vw provide scalability, allowing for continuous font size adaptation depending on screen size.

6. Overriding with !important

You can override smaller font sizes with an !important declaration:

.text { /* Small font size? Not on my watch! */ font-size: 14px !important; }

7. Considering browser support

Please note, function support varies across browsers; always stay updated with the recent compatibility information.

Supplementing your toolkit

Expand your reservoir of techniques with additional CSS functions and media queries for managing font size.

1. Tackling extreme viewports

The vmax and vmin units can handle scalable extremes while imposing boundaries on font size:

.text { /* Changing with the times, just like your favorite music genre! */ font-size: 3vmax; }

2. Precision with media queries

max-width in media queries allows precision control over when your font size adjustments take effect:

/* I love the 768px, sweet spot for your text! */ @media (max-width: 768px) { .text { font-size: 12px; } }

The accessibility factor

Bear in mind, maintaining a reasonable minimum font size is not just about the appearance—it's elemental for accessibility. Users with visual impairments could find small font sizes challenging, underlining that a balanced minimum size is a cornerstone of inclusive design.