Thursday, December 12, 2019

Understanding and Using rem Units in CSS

CSS units have been the subject of several articles here on SitePoint (such as A Look at Length Units in CSS, The New CSS3 Relative Font Sizing Units, and The Power of em Units in CSS). In this article, we increase the count by having an in-depth look at rem units, which have excellent browser support and a polyfill if you need support for old IE.

This article was updated in December, 2019 to reflect the current state of rem unit sizing with CSS. For more on CSS font and text properties, read our book, CSS Master, 2nd Edition.

What Are rem Units?

You might have encountered the term “R.E.M.” before while listening to the radio or your music player. Unlike their musical counterparts, named for the “Rapid Eye Movement” during deep sleep, in CSS rem stands for “root em”. They won’t make you lose your religion nor believe in a man on the moon. What they can do is help you achieve a harmonious and balanced design.

According to the W3C spec the definition for one rem unit is:

Equal to the computed value of font-size on the root element. When specified on the font-size property of the root element, the rem units refer to the property’s initial value.

This means that 1rem equals the font size of the html element (which for most browsers has a default value of 16px).

Rem Units vs. Em Units

The main problem with em units is that they are relative to the font size of their own element. As such they can cascade and cause unexpected results. Let’s consider the following example, where we want lists to have a font size of 12px, in the case where the root font size is the default 16px:

[code language="css"]
html {
font-size: 100%;
}

ul {
font-size: 0.75em;
}
[/code]

If we have a list nested inside another list, the font size of the inner list will be 75% of the size of its parent (in this case 9px). We can still overcome this problem by using something along these lines:

[code language="css"]
ul ul {
font-size: 1em;
}
[/code]

This does the trick, however we still have to pay a lot of attention to situations where nesting gets even deeper.

With rem units, things are a simpler:

[code language="css"]
html {
font-size: 100%;
}

ul {
font-size: 0.75rem;
}
[/code]

As all the sizes are referenced from the root font size, there is no more need to cover the nesting cases in separate declarations.

Font Sizing with Rem Units

One of the pioneers of using rem units for font sizing is Jonathan Snook with his Font sizing with REM article, back in May, 2011. Like many other CSS developers, he had to face the problems that em units bring in complex layouts.

At that time, older versions of IE still had large market shares and they were unable to zoom text that was sized with pixels. However, as we saw earlier, it is very easy to lose track of nesting and get unexpected results with em units.

The main issue with using rem for font sizing is that the values are somewhat difficult to use. Let’s see an example of some common font sizes expressed in rem units, assuming, of course, that the base size is 16px:

  • 10px = 0.625rem
  • 12px = 0.75rem
  • 14px = 0.875rem
  • 16px = 1rem (base)
  • 18px = 1.125rem
  • 20px = 1.25rem
  • 24px = 1.5rem
  • 30px = 1.875rem
  • 32px = 2rem

As we can see, these values are not very convenient for making calculations. For this reason, Snook used a trick called “62.5%“. It was not a new discovery, by any means, as it was already used with em units:

[code language="css"]
body { font-size:62.5%; } /* =10px */
h1 { font-size: 2.4em; } /* =24px */
p { font-size: 1.4em; } /* =14px */
li { font-size: 1.4em; } /* =14px? */
[/code]

As rem units are relative to the root element, Snook’s variant of the solution becomes:

[code language="css"]
html { font-size: 62.5%; } /* =10px */
body { font-size: 1.4rem; } /* =14px */
h1 { font-size: 2.4rem; } /* =24px */
[/code]

One also had to take into account the other browsers that didn’t support rem. Thus the code from above would have actually been written this way:

[code language="css"]
html {
font-size: 62.5%;
}

body {
font-size: 14px;
font-size: 1.4rem;
}

h1 {
font-size: 24px;
font-size: 2.4rem;
}
[/code]

While this solution seems to be close to the status of a “golden rule”, there are people who advise against using it blindingly. Harry Roberts writes his own take on the use of rem units. In his opinion, while the 62.5% solution makes calculation easier (as the font sizes in px are 10 times their rem values), it ends up forcing developers to explicitly rewrite all the font sizes in their website.

The post Understanding and Using rem Units in CSS appeared first on SitePoint.


by Adrian Sandu via SitePoint

Wednesday, December 11, 2019

TikTok ends the year with their list of Top 10’s in different categories

For those who are not aware, the TikTok app is deemed as the ultimate lip-syncing platform where users can make 15-second videos in various categories. And in just two years of its launch - the app has become so popular that it has reached 1.5 billion total downloads and boasts over 500 million...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Saima Salim via Digital Information World

Facebook Rolls Out New Log In Screen for Emerging Markets

You have probably been using the internet for years and years, and so you wouldn’t realize that there are a lot of people out there that have not been privileged enough to be able to use the internet in this manner. This has a lot to do with the fact that there are certain emerging markets out...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Zia Muhammad via Digital Information World

Pagination Style 66

The post Pagination Style 66 appeared first on Best jQuery.


by Admin via Best jQuery

Hover Effect Style 254

The post Hover Effect Style 254 appeared first on Best jQuery.


by Admin via Best jQuery

WhatsApp's New Loading Screen is being Bashed Left and Right; Users Tweet their Reasons!

Back in February 2014, Facebook bought one of the most popular messaging services WhatsApp for a whopping $19.3 billion. Even after owning it for nearly 6 years, the Social media giant hasn’t interfered too much with WhatsApp… until now. A few days ago, a new loading screen was unveiled by...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Ali Siddiqui via Digital Information World

Instagram to Start Categorizing Accounts You Follow

If you are an Instagram user, chances are that you have been using it for quite some time now. Over the years you have probably followed all kinds of accounts, from people that you personally know to celebrities that you wanted to follow to meme accounts that you have followed simply because of the...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Zia Muhammad via Digital Information World