What is the optimal baseline font size for responsive typography using rem units: percentage or pixels?

Currently, I am exploring the implementation of a responsive typography solution using SCSS and rems. The goal is to utilize media queries solely on the baseline font-size in html rather than on each individual element.

After researching different approaches to setting the font size in html:

html { font-size: 100%;  }  // default browser font-size
html { font-size: 62.5%; }  // 62.5% = 10px for rem calculation ease
html { font-size: 16px;  }  // fixed 16px

If we were to adopt the 62.5%-approach recommended by Jonathan Snook, managing headings and paragraphs could be streamlined with a mixin for px-fallbacks:

@mixin font-size($sizeValue: 1.6) {
    font-size: ($sizeValue * 10) + px;
    font-size: $sizeValue + rem;
}

h1 { @include font-size(3.2); }         // 32px
h2 { @include font-size(2.6); }         // 26px
h3 { @include font-size(2.2); }         // 22px
h4 { @include font-size(1.8); }         // 18px
h5 { @include font-size(1.6); }         // 16px
h6 { @include font-size(1.4); }         // 14px

Subsequently, applying media queries to the html font-sizes can help scale typography at various resolutions, like so:

@media (min-width: 768px) { html { font-size: 56.3%; } } // 56.3% = 9px
@media (min-width: 992px) { html { font-size: 62.5%; } } // 62.5% = 10px
@media (min-width: 1200px) { html { font-size: 68.8%; } } // 68.8% = 11px

Questions:

1. What is the most effective approach to setting the font-size baseline (px vs 100% vs 62.5%)?

2. What is the optimal strategy for implementing responsive typography with SASS / SCSS?

Answer №1

While I wouldn't claim it as the ultimate solution, here's my recent approach:

  1. Establish a base size using pixels for consistency across browsers and easier comprehension.
  2. Use pixel baselines, set type sizes for elements in rems, and adjust baseline size on media queries (in pixels).

I came across an insightful article by Trent Walton focusing more on best practices than implementation, which I found quite beneficial:

Although I may not have fully addressed your question, it's certainly a complex subject worth exploring further!

Answer №2

Implementing SCSS fluid typography effortlessly:

html { 
    @include fluid-property(font-size, 1rem, 1.5rem, 320px, 1600px);
}


/// Fluid properties example
///      html           { @include fluid-property(font-size, 14px, 20px, 320px, 1366px); }                          
///      html           { @include fluid-property(font-size, 1em, 2em, 320px, 1366px); }                            
///      h1,h2,h3,h4,h5 { @include fluid-property((padding-left, padding-right), 10rem, 20rem, 48em, 64em); }       
/// Units accepted: px / em / rem.
@mixin fluid-property($properties, $min-value, $max-value, $min-screen, $max-screen) {
    @each $property in $properties {
        #{$property}: $min-value;
    }
    @include media-min($min-screen) {
        @each $property in $properties {
            #{$property}: calc-interpolation($min-value, $max-value, $min-screen, $max-screen); 
        }
    }
    @include media-min($max-screen) {
        @each $property in $properties {
            #{$property}: $max-value;
        }
    }
}
@function calc-interpolation($min-value, $max-value, $min-screen, $max-screen) {
    $u-min-value: unit($min-value);
    $a: 0; $b: 0;
    @if ($u-min-value == em) {
        $a: em($max-value - $min-value) / em($max-screen - $min-screen);
        $b: $min-value - $a * em($min-screen); 
    } @else if ($u-min-value == rem) {
        $a: rem($max-value - $min-value) / rem($max-screen - $min-screen);
        $b: $min-value - $a * rem($min-screen); 
    } @else {
        $a: px($max-value - $min-value) / px($max-screen - $min-screen);
        $b: $min-value - $a * px($min-screen); 
    }    
    $sign: "+";
    @if ($b < 0) {
        $sign: "-";
        $b: abs($b);
    }
    @return calc(#{$a*100}vw #{$sign} #{$b});
}

The generated CSS output:

html { font-size: 1rem; }

@media only screen and (min-width: 320px) { html { font-size: calc(0.625vw + 0.875rem); } }

@media only screen and (min-width: 1600px) { html { font-size: 1.5rem; } }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Just discovered a background change triggered by the time, but unable to replicate it

I find it odd that everything works perfectly fine with document.body.style.background, yet I can't seem to change the style of this particular div. Here is the code snippet: var container = document.getElementById("container"); var updateTime = fu ...

CSS GRID: The Default row and column gaps are automatically included

I'm trying to position the images side by side without any gaps using CSS Grid. However, I noticed that there is a default gap between the columns and rows. Here is the code I used: <div class="grid-container"> <figure class="grid_item gr ...

Troubleshooting Bootstrap 4 Scrollspy CSS Selector Problem

As I work on my website, I am attempting to implement a scrollspy with Bootstrap 4. Unfortunately, it seems that the functionality is not working as expected. I suspect that the issue lies within the classes and selectors I have utilized. Interestingly, wh ...

Dynamic horizontal scrolling

I need help implementing a site using React that scrolls horizontally. I'm unsure how to implement certain aspects, so I am looking for some assistance. Within a wrapper, I have multiple container Divs. <div class="wrapper"> <div class=" ...

Unable to locate any division element by using document.getElementById

I'm encountering an unusual issue in my code. I am attempting to change the background color of certain divs using Javascript, but for some reason, when I use "document.getElementById", it is unable to find the div and returns NULL. Here is the probl ...

Encountering a source mapping error while trying to start the grunt server

I'm facing an issue accessing my sass files while running grunt server. It's unclear whether this is a file path/permission problem or something more complicated. Even when I run sudo grunt server, I encounter the same problem. grunt server Run ...

Why does the font-awesome CSS file fail to work while the font-awesome CDN is successful?

I've noticed that when using the font-awesome CSS file, the icons do not display properly. However, when I use the CDN and keep all the code the same, it works perfectly. Can someone explain why this happens? Here is my code: <!DOCTYPE html> < ...

Creating a dynamic canvas with a three-dimensional model using three.js: A step-by-step guide

I'm currently developing a website where I have integrated a canvas element to display a 3D object (specifically, a cube) using three.js. The canvas is housed within a div, following the guidelines found in various documentation. The issue arises wh ...

Basic color scheme

I'm attempting to change the background color behind a partially transparent .png div. The CSS class that modifies the div is ".alert". Manually editing the .alert class background color works perfectly, and now I want to automate this on a 1-second c ...

What is the best way to break down a <table> into several <div> elements?

Currently, I am dealing with a vast amount of data presented in a table format with numerous rows and columns. To manage this, I have opted to enclose the table within an overflow:scroll div. However, I am seeking a solution that allows me to keep the firs ...

Using Previous and Next Buttons in Bootstrap Tabs for Form Navigation

I am currently facing a challenge in splitting a form into multiple tabs and encountering issues with the next and previous buttons on the second and third tabs. My goal is for the tabs at the top to change state as you cycle through them by clicking the ...

Tips for overriding CSS rules marked as !important by a script

I recently implemented a JavaScript plugin from an external service that comes with its own CSS rules. However, these rules are not compatible with my layout as the width needs to be changed from 540 to 700px: #chat-container.bbhorizontal .main .booking { ...

The expandable card header in Bootstrap 4 is experiencing problems with spacing and wrapping, as well as issues with the rotating

I'm currently in the process of developing a Bootstrap4 card that consists of a title, body, and close button that can expand and collapse. I have been closely following this tutorial from Card collapse tricks! While I appreciate the example provided ...

Are there any design techniques for websites that can help make CSS more stable and less unpredictable?

What inspires me to write this question is my passion for creating visually appealing websites, although I struggle with CSS. I believe that the key is trial and error. It's all about experimenting, testing in different browsers, and refining the des ...

Why did the bootstrap installation in my project fail?

Previously, everything on my website was functioning correctly. However, upon launching it now, I noticed that the carousel, buttons, and other elements are broken. It seems like there might be an issue with the Bootstrap CDN as the buttons and sliders are ...

Clearing previous animation and implementing interval in jQuery for optimal performance

On my HTML page, I have implemented a feature with 6 tabs that are initially hidden using CSS display:hidden. When clicking on a tab, the content becomes visible by changing the display property to block. The first tab includes an animated loop created us ...

The proper method for incorporating a hover effect using CSS

Currently, I am facing an issue with integrating a hover effect to an image using CSS. The problem arises when the hover area is not aligned properly and the effect triggers even when the mouse is not directly over the image. <body> <div id=&apos ...

Tips for dynamically displaying images in both horizontal and vertical orientations

I would like to showcase images in the imageList. Here is what I want: AB CD How can this be achieved? It's not a matter of being even or odd Perhaps the list could look something like this: ABCDE FG I simply want a new row or display:block when ...

Eliminate jQuery's delayed blinking effect with the use of an event

Utilizing the mouseenter and mouseleave events, I have implemented a functionality to add a button (not actually a button) to an <li>. However, there seems to be a problem with my code. The button appears and disappears on mouseleave and mouseenter, ...

How can we show pictures when a user clicks?

After creating a model for displaying images in a modal when clicked by the user, I encountered an issue. Whenever I try to access the images from the gallery within the modal content, it only displays a blank popup. I want the pictures from the image gall ...