The Battle of the Units: REM vs PX vs EM

#REM #PX #EM

Cloud Service

One of the most critical decisions website developers must make is deciding what unit of measurement to use when sizing elements, fonts, and other design properties. The three most popular units: px, em, and rem, are most commonly used, but other units such as pt, pc, ex, etc., may also be suitable.


In this article, I`m going to try to explain these differences clearly for developers who don`t understand what they mean. 


CSS units have two basic types: 

  1. Absolute units 
  2. Relative units 

 

Absolute units 

Absolute units are fixed and do not depend on the size of the parent element or the viewport. Examples of absolute units are pixels (px), points (pt), and centimeters (cm). 


Relative units 

Relative units, on the other hand, are flexible – and just as the name implies, they are relative to the parent element’s size, the viewport’s size, or the root element’s font size. 


What is the difference between REM, PX and EM? 


REM (Root EM): 

This unit is relative to the root element (usually the <html> tag) and is useful for constructing responsive designs. The font size of the root element is one rem. For example, if the root element`s font-size is 16px, then 1rem is the same as 16px. 

The root font-size is the default font-size specified either by the user in their browser settings or by you, the developer. 

For example: 



Advantages: 

  • The Rem values adapt automatically to changes in device viewport and user preferences based on the root font size. 
  • It is recommended for elements that require scalability and responsiveness, such as font sizes. - The use of Rem units should result in consistency in scaling across the entire document. 


 Disadvantages: 

  • The root element must have a base font size, which may cause inconsistencies if not handled correctly. 



Pixel (Px): 

The unit of measurement is absolute and cannot be compared to anything else. It is commonly used for elements with fixed sizes or elements that require precise measurements. 

The container doesn`t scale according to the viewport, nor does the text scale according to the viewport when you resize the browser. 

Its fixed nature is one of the main reasons why px is not always recommended for responsive design. px is useful when you want to specify a fixed size of an element, such as a border size or an image size. 

For example: 



In this example, the font size of the element is set to 14 pixels and the top margin is set to 20 pixels. 


Advantages:

  • A pixel value can be used to control the size and position of an element precisely. 
  • A good choice for non-scaling elements such as borders, shadows, and fixed-size images. 


Disadvantages: 

  • Pixels cannot be scalable and do not adjust according to the user`s preferences, for example, font size. 
  • Having fixed pixel values can reduce the responsiveness and adaptability of layouts across various devices. 


Em (Relative EM): 

The em is another relative unit of measurement, like the rem. Unlike rem, however, em is related to the font size of the parent element or the nearest parent with a defined font size. 

Let`s say you have a parent div with a font-size of 16px. The paragraph font-size will be 16px if you create a paragraph element in that div with a font-size of 1em. 

For example: 



In this example, the font size of the child element is 0.8 times the font size of the parent element. 


Advantages: 

  • Em values are scalable and adjust to the parent element`s font size, providing design flexibility. 
  • A small adjustment can be made within a specific section of a document without affecting the rest. 


 Disadvantages: 

  • Compounding effects can make it difficult to predict element sizes when Em units are nested. 



 The choice of unit depends on the project`s specific requirements. A rem design can be scalable, an em design can be responsive, and a px design can be fixed.


Connect with Revaalo labs your one stop solution for Digital Transformation needs.

The Battle of the Units: REM vs PX vs EM

One of the most critical decisions website developers must make is deciding what unit of measurement to use when sizing elements, fonts, and other design properties.

Read more

Top AI Tools for Developers in 2024

It`s an amazing technology-one that will help us solve society`s toughest problems and reshape the world.

Read more

Chrome Extension Development Company in Bengaluru

Today, web browsers play a significant role in our lives, providing us with access to a world of information and possibilities.

Read more

Top 10 Alternatives to Power BI

Power BI is a popular business intelligence tool developed by Microsoft for data visualization and analysis. While Power BI is a robust solution, there are several alternatives available that cater to different needs and preferences.

Read more

Tenant Billing System/Utilities Billing software

Tenant billing systems are software solutions used by property owners, managers, and landlords to accurately bill tenants for their usage of utilities and services.

Read more