Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 14, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 14, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 14, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 14, 2025

      I test a lot of AI coding tools, and this stunning new OpenAI release just saved me days of work

      May 14, 2025

      How to use your Android phone as a webcam when your laptop’s default won’t cut it

      May 14, 2025

      The 5 most customizable Linux desktop environments – when you want it your way

      May 14, 2025

      Gen AI use at work saps our motivation even as it boosts productivity, new research shows

      May 14, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025
      Recent

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold at the 2025 Hermes Creative Awards

      May 14, 2025

      PIM for Azure Resources

      May 14, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025
      Recent

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025

      You can now share an app/browser window with Copilot Vision to help you with different tasks

      May 14, 2025

      Microsoft will gradually retire SharePoint Alerts over the next two years

      May 14, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Fluid Superscripts and Subscripts

    Fluid Superscripts and Subscripts

    December 11, 2024

    Superscripts and subscripts are essential elements in academic and scientific content — from citation references to chemical formulas and mathematical expressions. Yet browsers handle these elements with a static approach that can create significant problems: elements become either too small on mobile devices or disproportionately large on desktop displays.

    After years of wrestling with superscript and subscript scaling in CSS, I’m proposing a modern solution using fluid calculations. In this article, I’ll show you why the static approach falls short and how we can provide better typography across all viewports while maintaining accessibility. Best of all, this solution requires nothing but clean, pure CSS.

    The problem with static scaling

    The scaling issue is particularly evident when comparing professional typography with browser defaults. Take this example (adapted from Wikipedia), where the first “2” is professionally designed and included in the glyph set, while the second uses <sub> (top) and <sup> (bottom) elements:

    Diagramming the typographic parts and spacing of subscripts and superscripts.

    Browsers have historically used font-size: smaller for <sup> and <sub> elements, which translates to roughly 0.83x scaling. While this made sense in the early days of CSS for simple documents, it can create problems in modern responsive designs where font sizes can vary dramatically. This is especially true when using fluid typography, where text sizes can scale smoothly between extremes.

    Fluid scaling: A better solution

    I’ve developed a solution that scales more naturally across different sizes by combining fixed and proportional units. This approach ensures legibility at small sizes while maintaining proper proportions at larger sizes, eliminating the need for context-specific adjustments.

    CodePen Embed Fallback

    Here’s how it works:

    sup, sub {
      font-size: calc(0.5em + 4px);
      vertical-align: baseline;
      position: relative; 
      top: calc(-0.5 * 0.83 * 2 * (1em - 4px)); 
      /* Simplified top: calc(-0.83em + 3.32px) */
    }
    
    sub {
      top: calc(0.25 * 0.83 * 2 * (1em - 4px)); 
      /* Simplified top: calc(0.42em - 1.66px) */
    }
    • Natural scaling: The degressive formula ensures that superscripts and subscripts remain proportional at all sizes
    • Baseline alignment: By using vertical-align: baseline and relative positioning, we prevent the elements from affecting line height and it gives us better control over the offset to match your specific needs. You’re probably also wondering where the heck these values come from — I’ll explain in the following.

    Breaking down the math

    Let’s look at how this works, piece by piece:

    Calculating the font size (px)

    At small sizes, the fixed 4px component has more impact. At large sizes, the 0.5em proportion becomes dominant. The result is more natural scaling across all sizes.

    sup, sub {
      font-size: calc(0.5em + 4px);
      /* ... */
    }
    
    sub { 
      /* ... */
    }

    Calculating the parent font size (em)

    Within the <sup> and <sub> elements, we can calculate the parent’s font-size:

    sup, sub {
      font-size: calc(0.5em + 4px);
      top: calc(2 * (1em - 4px));
    }
    
    sub { 
      top: calc(2 * (1em + 4px));
    }

    The fluid font size is defined as calc(0.5em + 4px). To compensate for the 0.5em, we first need to solve 0.5em * x = 1em which gives us x = 2. The 1em here represents the font size of the <sup> and <sub> elements themselves. We subtract the 4px fixed component from our current em value before multiplying.

    The vertical offset

    For the vertical offset, we start with default CSS positioning values and adjust them to work with our fluid scaling:

    sup, sub {
      font-size: calc(0.5em + 4px);
      top: calc(-0.5 * 0.83 * 2 * (1em - 4px));
    }
    
    sub { 
      top: calc(0.25 * 0.83 * 2 * (1em - 4px));
    }

    The formula is carefully calibrated to match standard browser positioning:

    • 0.5em (super) and 0.25em (sub) are the default vertical offset values (e.g. used in frameworks like Tailwind CSS and Bootstrap).
    • We multiply by 0.83 to account for the browser’s font-size: smaller scaling factor, which is used per default for superscript and subscript.

    This approach ensures that our superscripts and subscripts maintain familiar vertical positions while benefiting from improved fluid scaling. The result matches what users expect from traditional browser rendering but scales more naturally across different font sizes.

    Helpful tips

    The exact scaling factor font-size: (0.5em + 4px) is based on my analysis of superscript Unicode characters in common fonts. Feel free to adjust these values to match your specific design needs. Here are a few ways how you might want to customize this approach:

    For larger scaling:

    sup, sub {
      font-size: calc(0.6em + 3px);
      /* adjust offset calculations accordingly */
    }

    For smaller scaling:

    sup, sub {
      font-size: calc(0.4em + 5px);
      /* adjust offset calculations accordingly */
    }

    For backward compatibility, you might want to wrap all of it in a @supports block:

    @supports (font-size: calc(1em + 1px)) {
      sup, sub {
        ...
      }
    }

    Final demo

    I built this small interactive demo to show different fluid scaling options, compare them to the browser’s static scaling, and fine-tune the vertical positioning to see what works best for your use case:

    CodePen Embed Fallback
    Open Live Demo

    Give it a try in your next project and happy to hear your thoughts!


    Fluid Superscripts and Subscripts originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleHow to Install PHP 8.4-7.4 on RHEL 9
    Next Article Advanced Weather Companion GNOME Shell Extension

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 15, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-30419 – NI Circuit Design Suite SymbolEditor Out-of-Bounds Read Vulnerability

    May 15, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Top 10 Upskilling Courses to stay ahead in 2025

    Web Development

    NZXT Capsule Elite review: A standout microphone for an aspiring streamer or content creator on a budget

    News & Updates

    Guardian Analytics and Webster Bank Settle $1.4 Million Data Breach Lawsuit

    Development

    Gemini 2.0 is now available to everyone

    Artificial Intelligence
    GetResponse

    Highlights

    Development

    Top 3 MS Office Exploits Hackers Use in 2025 – Stay Alert!

    March 27, 2025

    Hackers have long used Word and Excel documents as delivery vehicles for malware, and in…

    Tips for Freelancers Looking to Maximize Passive Income Streams

    November 11, 2024

    The 50+ best Black Friday Walmart deals 2024: Early sales live now

    November 5, 2024

    Guess who’s back? Xbox is coming to Gamescom 2024—without its blue rival

    June 24, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.