Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font-size and device-pixel-ratio

Let's say that I have default font size set to 16px (this should be redundant since it's default in most browsers):

For brevity, my site contains some box with size 10em x 10em.

body, html 
{
    font-size: 16px;
}
.box {
    border: 1px green solid;
    background-color:green;
    width:10em;
    height:10em;
}

My site is already responsive with min-width media queries for specific breakpoints targetted at em values (36, 62 and 85em).

The only thing my site does is change width and color of the box.

@media (min-width:36em) {
  .box {
    width: 36em;
    border:1px red solid;
    background-color:red;
  }
}
@media (min-width:62em) {
    .box {
        width: 62em;
        border:1px blue solid;
        background-color:blue;
    }
}
@media (min-width:85em) {
    .box {
        width: 85em;
        border:1px orange solid;
        background-color:orange;
    }
}

Now let's say that I have some device with resolution 1440x900 where browsers decide that the device's pixel density is 2dppx by default. As result, everything looks much bigger than it should be. And I don't like it.

But the solution should be easy, right? Just add a media query at start for 2dppx pixel density and since it's twice as much pixels, I just reduce the font size by half...that should work, right?

And since I use "em" on everything then by changing the font-size by half I automatically reduce size of everything "em"-sized by half...right?

@media ( min-resolution: 2dppx) {
    body, html {
        font-size: 50%;
    }
}

relevant jsFiddle

Well I found it does not work as I thought it would...I tried to google answers but did not find anything relevant, I suspect I misunderstand how font-sizes, ems and pixel density works...

  • Reducing font-size by 50% reduces the font-size by much more than by half. The original size is 16px but font-size: 50%; results in a font much smaller than font-size:8px ... Tested in Firefox and Chrome. Does anyone know why is that?

  • When I set font-size:8px; in the media query for 2dppx then media queries for min-width:36em, 62em and 85em do not trigger as I would have expected, at least when I change layout.css.devPixelsPerPx in Firefox. The min-width in media query behaves like the font-size is still at 16px eventhough it's changed with the dppx media query. Tested in Firefox and Chrome. Does anyone know why is that?

UPDATE

Looks like media queries are loaded in 1dppx context, it does not matter if you use "em", it seems they are cached on load and values such as min-width are fixed to the first value and eventhough you change the base font-size in different media query, other queries don't refresh.

The only solution I can think of right now is to multiply all media queries. One media query for px value in 1 dppx context and another media query for px value in 2 dppx context (which is 50% of the first value).

like image 723
Mirek Avatar asked Nov 07 '22 17:11

Mirek


1 Answers

Thanks to Mr Lister in comments, I found out that this:

@media ( min-resolution: 2dppx) {
    body, html {
        font-size: 50%;
    }
}

...is not what I intended to do. The 50% rule works for <html> tag AND for <body> tag. The resulting font size is not 8px in body, but 4px, because 50% in html is 8px and another 50% in nested body tag results in 4px.

What I intended to do was this:

@media ( min-resolution: 2dppx) {
    html {
        font-size: 50%;
    }
}

Font-size issue is solved.

For the media query: I found out that sizes in @media queries ignore pixel density but sizes in the CSS rule body do not. Since I use "em" sizes and SASS, I can simply replace all @media queries with this mixin (example with 2dppx):

@mixin media-min-width($min-width) {
    @media(min-width:$min-width), (min-width: $min-width / 2) and (min-resolution: 2dppx) {
        @content;
    }
}
like image 119
Mirek Avatar answered Nov 15 '22 06:11

Mirek