Can anyone explain why my media queries break when converting them FROM px TO ems
In the body of my document, I have included the following rule font-size: 62.5%, Therefore I would assume that I could convert my media query to FROM 650px to 65em? Changing my media queries to ems breaks the layout
As an alternative, can I convert the media query to REMS with a pixel fallback ?? that said, I have no idea how to do this
@media screen and (min-width: 650px) {
body
{
font-size: 62%;
background-color: #364759;
background-repeat: repeat-x;
background: url("bg.gif");
}
#wrapper,
footer
{
width: 80%;
max-width: 1050px;
margin: 0 auto;
}
} // end media query
many thanks, P
In addition to the other answers, please note that a length specified in
em
for another property thanfont-size
is actually relative to thefont-size
of the element itself, i.e. not necessarily its parent. This makes a difference if you specify a padding, margin etc inem
and also afont-size
for one and the same element.Section 1.3 of the media queries spec says:
Similarly, the section 2 says:
So your
font-size: 62.5%
rule has no effect with regards to media queries, and1em
is still16px
, not10px
.The reason things are this way is that doing otherwise would cause loops, which is something CSS cannot deal with. Try to think of what this example would do if we didn't have this rule:
1em would be gigantic, so the media query would match, so 1em would be small, so the media query wouldn't match, so 1em would be gigantic, so...
Here is a great article that will help explain the differences between px/em/rem
https://www.futurehosting.com/blog/web-design-basics-rem-vs-em-vs-px-sizing-elements-in-css/
Also this may be useful: https://css-tricks.com/rems-ems/
EDIT
As @Jesse Kernaghan commented that the above links could be broken easily I will outline. The differences between PX vs EM vs REM.
px
A pixel is an absolute measurement, which means that they are the same size regardless of the size of anything else. In practice, they are not the same length everywhere because specific devices treat them differently, however on each device a pixel is always the same.
16px
on your laptop monitor is not the same as16px
on your iPad. Pixels are absolute but not consistent.em
Where
px
is an absolute measure of lengthem
's are relative to the font-size of the parent element. Take the following example:Now if you wanted to half the size of the
p
element you would just need to change the font-size of the surrounding<div>
to11px
.This unit of measure can be very useful as it pertain's to responsive layouts.
However, there are problems with
em
's. Because everything is sized relative to it parent, the meaning of an em changes as elements are nested. If you take the above example and expand on it and nest a<blockquote>
inside the<p>
with afont-size
of0.5em
, the result will probably not be what is expected. The result will be that thefont-size
of the<p>
will be equal to11px
however thefont-size
of the<blockquote>
will be half of that again, becauseem
is relative to the immediate ancestor (<p>
), not the<div>
.rem
Rems (root ems), are like
em
's but they are always relative to thefont-size
of the<html>
element. It does not matter how deeply nested an element is.Conclusion
So with a brief explanation out of the way there is a basic concept that Chris Coyier has in his post https://css-tricks.com/rems-ems/. It basically outlines to set a base
html
font-size and then do media query adjustments usingpx
(remember this is an absolute measurement). And inside these media query resize all you do is scale the font-size down. Then you set your main elements using therem
and the elements that need to scale with the main elements use theem
. Chris Coyier code example is below: