Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Jakobud opened this issue Sep 18, 2017 · 8 comments
Closed

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

Jakobud opened this issue Sep 18, 2017 · 8 comments
Labels

Comments

@Jakobud
Copy link

Jakobud commented Sep 18, 2017

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.

@andresgalante
Copy link
Collaborator

andresgalante commented 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!

@Jakobud
Copy link
Author

Jakobud commented Sep 18, 2017

Can you explain why setting a base font size in px is bad for accessibility?

@andresgalante
Copy link
Collaborator

Sure, there are many users with sight problems that set the default font size of the browser larger.

Since rem units are relative to the root font size, your page will follow that change. If you set them in px, and pixels are absolute units and not relative, you would be blocking that font size bump.

Some people argue that users can always do a zoom and they should be ok too. That's wrong for 2 things:

1- you don't want to ask people to do that if they set it by default in their browsers
1- By doing zoom you'll be also increaing the size of the images, therefore there will be less room for text making it almost impossible to read.

Thisi s not a bootstrap issue or bug, Bootstrap is doing calculations with rems, and that's why setting them in pixels fails in your codebase.

There are many other benefits to using rem and you can probably google it.

@Jakobud
Copy link
Author

Jakobud commented Sep 19, 2017

Okay, but what I was proposing was setting the base font size to pixels. Setting the base font size in pixels is fine for accessibility. It will resize properly with default browser font size changes and with zoom changes. Bootstraps code is this:

$font-size-base: 1rem; and that is exactly the same thing as $font-size-base: 16px;.

The only difference is that the calculations used elsewhere won't work if the value is in pixels. Does that make sense?

@patrickhlauke
Copy link
Member

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

@patrickhlauke
Copy link
Member

it's not necessarily a complete deal breaker in terms of accessibility, but it's not really "the done thing" to just override user/platform prefs

@jaesung2061
Copy link

I need to somehow override the root rem because I'm creating a plugin that has it's own UI which will be used across multiple websites (it is scoped if you're wondering). Because you guys use REM for everything, if a website decides to use 20px root font, everything will be massively oversized. What can i do to avoid this?

@mdo
Copy link
Member

mdo commented Jan 4, 2018

You can force the <html> element to have a specific font-size as needed. Dunno how @patrickhlauke feels about that though. Beyond that option, is this a won't fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants