I have designed and developed more than 10 sites, but I still have a doubt about the correct unit I should use. Should it be px
, em
or %
?
EDIT 1: FOR LAYOUTS (Especially for container boxes)
In order to ensure consistent use of whitespace throughout the application, given a component could be used in a variety of contexts, it may be best to use rem for margin and padding than em .
In this article, I will show you the most suitable CSS units to use for responsive design. There are two types of CSS Units which are: Absolute Length Unit and Relative Length Unit.
While PX, EM, and REM are primarily used for font sizing, %, VW, and VH are mostly used for margins, padding, spacing, and widths/heights.
CSS supports a number of measurements including absolute units such as inches, centimeters, points, and so on, as well as relative measures such as percentages and em units. You need these values while specifying various measurements in your Style rules e.g. border = "1px solid red".
Different units depending on context. If there was one that was best for every situation, then there wouldn't be so many units.
As rules of thumb go:
If you are working on screen media:
%
for font sizespx
for imagespx
, %
, or em
for box sizesIf you are working in print media:
px
(this is not a hard rule by any means) and everything else is fair game. It really depends how much control you want.There's no real right or wrong, but as a rule of thumb:
Each used to have specific advantages or disadvantages in different browsers when it came to users scaling the browser's base font-size/zooming, but more recent versions of the browsers by-and-large get around these issues by scaling everything, not just font-size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With