Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rem units in media queries and as width

When using rem units in Google Chrome (or firefox), the result is unexpected. I'v set up a page with the root font-size set to 10px to make it easier the translate a pixel based design to html/css.

This is my css. My expectation would be a page with one 500px wide box and if the screen is wider than 500px the background should turn red.

html {
font-size: 10px;
}

@media screen and (min-width: 50rem){
  body{
    background-color: red;
  }
}

.test{
  width: 50rem;
  background: black;
  height:100px;
}

But, despite both values being defined as 50rem, the results is a 500px wide box with the page turning red at 800px.

https://jsfiddle.net/tstruyf/puqpwpfj/4/

What am I doing wrong or why is this?

like image 628
user63457 Avatar asked Nov 21 '17 09:11

user63457


People also ask

Can I use rem for width?

By expressing widths, margins, and padding in rem units, it becomes possible to create an interface that grows or shrinks in tune with the root font size. Let's see how this thing works using a couple of examples. In this first example, we change the root font size using media queries.

Can you use rem in media queries?

Relative units (such as rems) in media queries might not behave as you expect. The TL;DR is that 1rem in a media query condition is always equal to the font-size set in browser settings, and is not affected by a root font-size assigned in CSS.

How do you change the width of a media query?

Setting a particular "width-range" isn't any different from the way media queries are created. The only difference is the addition of more media feature expressions (that is, the screen width sizes). Take a look: @media only screen and (min-width: 360px) and (max-width: 768px) { // do something in this width range. }

What Is rem in width CSS?

Rem (short for “root-em”) units dictate an element's font size relative to the size of the root element. By default, most browsers use a font size value of 16px. So, if the root element is 16px, an element with the value 1rem will also equal 16px.


1 Answers

It's meant to work like this, it's just a little confusing if you don't know what's going on.

If you're not using rem in media query declarations then they are based off the root html base font size. If you don't declare this, then in most modern web browsers it's 16px.

As you have declared it as 10px a rem will be 10px throughout your code. Unlike em units, where it is based on the closest parent declaration size.

The confusion comes in that media queries declarations do not base themselves on the declared font-size that you apply to html and instead always use the default size - which as I said is 16px in pretty much all browsers.

That's why 50rem is coming out as 800px - 16px * 50.

Note, this is only for the declaration of the media query breakpoint, if you assign something to be 1rem tall inside the media query, then it will base itself on the base html size.

html {
  font-size: 10px;
}

@media screen and (min-width: 50rem){ // 800px (uses base font-size)
  div.somediv {
    width: 50rem; // 500px (uses the declared html font-size)
  }
}
like image 71
Brett East Avatar answered Sep 28 '22 07:09

Brett East