Rem units are based off the font size of the html
element, not the body
.
Your css would change to this:
html { // changed from body to html
font-size: 14px;
}
div {
margin: 10px;
padding: 20px;
text-align: center;
background: #efefef;
}
#em h1 { font-size: 2em; }
#em p { font-size: 1em; }
#rem h1 { font-size: 2rem; }
#rem p { font-size: 1rem; }
@media screen and (max-width: 500px) {
html { // changed from body to html
font-size: 6px;
background: red;
}
}
Notice we target the html element in the first line and in the media query.
Here is your jsfiddle modified to work with rems.