question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v4: Can't set $font-size-base to a pixel value

See original GitHub issue

Currently, $font-size-base is defined as:

$font-size-base: 1rem !default;

If you attempt to define your base font size using pixels, for example:

$font-size-base: 14px;

Then SASS will not compile because of Incompatible units: 'rem' and 'px'. in various calculations. It seems like you should be able to define a font size using pixels. If you are working with a designer who is using Sketch or something, you can sure bet that they have their font’s defined in pixels. A designer is not going to have any clue what a rem is.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
andresgalantecommented, Sep 18, 2017

Hi @Jakobud you should always avoid doing fonts in px, it’s not good for accessibility.

Let me help you with your designers using sketch issues 😃

You can set up a function that transform px to rem like this:

$font-size-root: 16; // no units, you assume the font size base of most browsers is 16px.

@function strip-unit($num) {
    @return $num / ($num * 0 + 1);
}

// prem stands for  px to rem.
@function prem($pxval, $base: $font-size-root) {
    @return strip-unit($pxval) / $base * 1rem;
}

Then if the designers set a 36px font size, you use it like this:

.class-name {
    font-size: pem(32); // Returns 2rem (or 32px)
}

This is a flexible solution that will let you use rems and your designers px. We are using it at our project with successful results. In fact we use this for all our measurements, the only thing we do in px are border-with. Designers use px we do prem 😃

hope it helps!

1reaction
patrickhlaukecommented, Sep 19, 2017

setting the base font size in pixels overrides any minimum font size the user may have set in their browser, or any minimum default rem size a browser on a specific platform has been set to. not all use 16px default - for instance, from memory, browser on some kindle devices uses 20px or more

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change Bootstrap's global default font size?
Bootstrap uses the variable: $font-size-base: 1rem; // Assumes the browser default, typically 16px. I don't recommend mucking with this, but you can.
Read more >
Why you should never use px to set font-size in CSS
If the current font size is 20px , then 1em = 20px (though again: you shouldn't set font sizes in pixels—this is just...
Read more >
Use Rems for Font Size to Respect User Preferences
Using pixels is a particularly bad practice for font sizing because it can create some accessibility problems for users with vision impairments.
Read more >
Font Size Idea: px at the Root, rem for Components, em for ...
This way you can adjust font-size at a module level, which is pretty easy. The chances the type within a single module have...
Read more >
font-size - CSS: Cascading Style Sheets - MDN Web Docs
Setting the font size in pixel values ( px ) is a good choice when you need pixel accuracy. A px value is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found