{"id":5971,"date":"2020-10-25T20:02:00","date_gmt":"2020-10-25T20:02:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=5971"},"modified":"2020-11-22T20:12:31","modified_gmt":"2020-11-22T20:12:31","slug":"taking-a-look-at-css-units-pixels-em-and-percentage","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/code\/taking-a-look-at-css-units-pixels-em-and-percentage\/","title":{"rendered":"Taking a look at CSS Units: Pixels, EM, and Percentage"},"content":{"rendered":"\n<p>Unit takes an important role for measuring and building things like a house, a bridge or a tower, and building a website is not an exception. There are a number of methods of measurements used on the Web, specifically in CSS, namely&nbsp;<strong>Pixel, EM and Percentage<\/strong>.<\/p>\n\n\n\n<p>In this post, we are going to run through these units to get further insight into these units of measurements.<\/p>\n\n\n\n<h2>PX<\/h2>\n\n\n\n<p>Pixel is a fixed unit measurement and the smallest unit on screen measurement but don\u2019t confuse this with the Pixel that defines screen resolution. The Pixel in CSS has nothing to do with the screen resolution.<\/p>\n\n\n\n<p>When we try viewing a web page fixed at&nbsp;<strong>1024px<\/strong>&nbsp;width say on a tablet with&nbsp;<strong>1024px by 480px<\/strong>&nbsp;screen resolution, the web page will not fit in the screen.<\/p>\n\n\n\n<p>I\u2019ve been experiencing this issue in the past and found out that the Pixel in CSS is actually not a linear unit \u2013 it is in fact a radial measurement.<\/p>\n\n\n\n<p>Basically, the Pixel in CSS measures the length of the display area instead of the screen resolution, so the&nbsp;<strong>1024px screen resolution does not mean that the screen is also 1024px<\/strong>&nbsp;of the length.<\/p>\n\n\n\n<h2>More On PX<\/h2>\n\n\n\n<p>I\u2019m not an expert on the technicalities in this matter but if you know&nbsp;<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Trigonometry\">Trigonometry<\/a>, and want to find out more, check out the following explanation from&nbsp;<strong>Sean B. Plamer<\/strong>:&nbsp;<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"http:\/\/inamidst.com\/stuff\/notes\/csspx\">CSS px is an Angular Measurement<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5973\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/taking-a-look-at-css-units-pixels-em-and-percentage\/attachment\/rad-px\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/rad-px.jpg\" data-orig-size=\"500,280\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"rad-px\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/rad-px-300x168.jpg\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/rad-px.jpg\" loading=\"lazy\" width=\"500\" height=\"280\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/rad-px.jpg\" alt=\"\" class=\"wp-image-5973\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/rad-px.jpg 500w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/rad-px-300x168.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<h2>Finding the Display Area<\/h2>\n\n\n\n<p>So, how do we find the actual display area of a device? Luckily, there is a handy special calculator to\u00a0<strong>roughly estimate<\/strong>\u00a0the display area in format of Pixel, <a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/www.unitconverters.net\/typography\/centimeter-to-pixel-x.htm\">CM to PX<\/a>.<\/p>\n\n\n\n<p>We simply need the length of the device in&nbsp;<code>cm<\/code>&nbsp;and the PPI (Point Per Inch) \/ DPI (Dot Per Inch) which you can get these from the device box. With this, it turns out that the tablet I used is merely&nbsp;<code>953px<\/code>in length.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5974\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/taking-a-look-at-css-units-pixels-em-and-percentage\/attachment\/cm-to-px\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/cm-to-px.jpg\" data-orig-size=\"500,280\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"cm-to-px\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/cm-to-px-300x168.jpg\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/cm-to-px.jpg\" loading=\"lazy\" width=\"500\" height=\"280\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/cm-to-px.jpg\" alt=\"\" class=\"wp-image-5974\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/cm-to-px.jpg 500w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/cm-to-px-300x168.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<h2>Pixels Font Size in Photoshop<\/h2>\n\n\n\n<p>When working on Photoshop, we will find that the font size is by default set to&nbsp;<strong>Pt (Points)<\/strong>. The Pt unit is ideal for print styles.<\/p>\n\n\n\n<p>To not confuse this with&nbsp;<code>px<\/code>&nbsp;when we are translating it to Web documents, we can change the units from the following menu:&nbsp;<strong>Edit &gt; Preferences &gt; Units and Rulers<\/strong>.<\/p>\n\n\n\n<p>There, you get a window option as shown in this screenshot below. Choose \u2018pixels\u2019 for the Type unit.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5975\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/taking-a-look-at-css-units-pixels-em-and-percentage\/attachment\/units-rulers\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/units-rulers.jpg\" data-orig-size=\"500,280\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"units-rulers\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/units-rulers-300x168.jpg\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/units-rulers.jpg\" loading=\"lazy\" width=\"500\" height=\"280\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/units-rulers.jpg\" alt=\"\" class=\"wp-image-5975\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/units-rulers.jpg 500w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/units-rulers-300x168.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>And, we have px for the font size.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5976\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/taking-a-look-at-css-units-pixels-em-and-percentage\/attachment\/px-font-menu\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-font-menu.jpg\" data-orig-size=\"500,69\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"px-font-menu\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-font-menu-300x41.jpg\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-font-menu.jpg\" loading=\"lazy\" width=\"500\" height=\"69\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-font-menu.jpg\" alt=\"\" class=\"wp-image-5976\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-font-menu.jpg 500w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-font-menu-300x41.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<h2>EM<\/h2>\n\n\n\n<p>EM is a relative measurement. In CSS, EM refers to the multiplication of the default font size from the device or the document itself, that is why I personally word EM as&nbsp;<strong>EMphasize<\/strong>, but of course don\u2019t take my word for it.<\/p>\n\n\n\n<p>Here is an example; let\u2019s say in a document, the font size is assigned for is&nbsp;<code>16px<\/code>.&nbsp;<code>1em<\/code>&nbsp;is equal to&nbsp;<code>16px<\/code>,&nbsp;<code>2em<\/code>&nbsp;is equal to&nbsp;<code>32px<\/code>&nbsp;and so on.<\/p>\n\n\n\n<p>Although EM is traditionally used for font sizing and in fact it is the standard unit used in browser styles for measuring font size, we can also use EM to define an element\u2019s length.<\/p>\n\n\n\n<p>The only constraint when working with EM unit is it is not quite intuitive, but there are two practical ways we can handle this. First, we can use this calculator,&nbsp;<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"http:\/\/pxtoem.com\/\">PX to EM<\/a>; it\u2019s a handy calculator that I use often.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5977\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/taking-a-look-at-css-units-pixels-em-and-percentage\/attachment\/px-to-em\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-to-em.jpg\" data-orig-size=\"500,350\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"px-to-em\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-to-em-300x210.jpg\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-to-em.jpg\" loading=\"lazy\" width=\"500\" height=\"350\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-to-em.jpg\" alt=\"\" class=\"wp-image-5977\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-to-em.jpg 500w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/px-to-em-300x210.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>Another way is to set the document pixel base to\u00a0<code>10px<\/code>, so we can calculate it more easily; for instance, setting\u00a0<code>15px<\/code>\u00a0at\u00a0<code>1.5em<\/code>. Well, I hope you get the basic idea with this example.<\/p>\n\n\n\n<h2>Percentage<\/h2>\n\n\n\n<p>It is more straightforward with percentage, which measures relatively to the parent length. When the parent width is\u00a0<code>800px<\/code>\u00a0then\u00a0<code>50%<\/code>\u00a0would become\u00a0<code>400px<\/code>. In recent years, as<strong>\u00a0Responsive Design<\/strong>\u00a0starts becoming the <strong>web design standard<\/strong>, percentage unit is adapted more frequently in the wild.<\/p>\n\n\n\n<p>Alright, let\u2019s take a look at the following example; this code below defines&nbsp;<code>.container<\/code>&nbsp;and&nbsp;<code>.main<\/code>&nbsp;class selector with&nbsp;<code>60%<\/code>&nbsp;for the width, but these classes refer to different parents thus they generate different lengths.<\/p>\n\n\n\n<p>The&nbsp;<code>.container<\/code>&nbsp;will take&nbsp;<code>60%<\/code>&nbsp;of the browser viewport width, whilst the&nbsp;<code>.main<\/code>&nbsp;will take&nbsp;<code>.container<\/code>&nbsp;width as its direct parent.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>.container {<\/code><code>width<\/code><code>: <\/code><code>60%<\/code><code>;<\/code><code>margin<\/code><code>: <\/code><code>100px<\/code> <code>auto<\/code><code>;<\/code><code>background-color<\/code><code>: <\/code><code>#eaeaea<\/code><code>;<\/code><code>padding<\/code><code>: <\/code><code>1%<\/code><code>;<\/code><code>height<\/code><code>: <\/code><code>250px<\/code><code>;<\/code><code>font-family<\/code><code>: <\/code><code>Arial<\/code><code>;<\/code><code>text-align<\/code><code>: <\/code><code>center<\/code><code>;<\/code><code>}<\/code><code>.main, .aside {<\/code><code>line-height<\/code><code>: <\/code><code>253px<\/code><code>;<\/code><code>}<\/code><code>.main {<\/code><code>width<\/code><code>: <\/code><code>60%<\/code><code>;<\/code><code>height<\/code><code>: <\/code><code>100%<\/code><code>;<\/code><code>float<\/code><code>: <\/code><code>left<\/code><code>;<\/code><code>background-color<\/code><code>: <\/code><code>#ccc<\/code><code>;<\/code><code>}<\/code><code>.aside {<\/code><code>width<\/code><code>: <\/code><code>40%<\/code><code>;<\/code><code>height<\/code><code>: <\/code><code>100%<\/code><code>;<\/code><code>background-color<\/code><code>: <\/code><code>#aaa<\/code><code>;<\/code><code>float<\/code><code>: <\/code><code>left<\/code><code>;<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Thus, it will give us the following nice result:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5972\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/taking-a-look-at-css-units-pixels-em-and-percentage\/attachment\/percentage-demo\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/percentage-demo.jpg\" data-orig-size=\"500,201\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"percentage-demo\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/percentage-demo-300x121.jpg\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/percentage-demo.jpg\" loading=\"lazy\" width=\"500\" height=\"201\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/percentage-demo.jpg\" alt=\"\" class=\"wp-image-5972\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/percentage-demo.jpg 500w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/percentage-demo-300x121.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>As percentage is also a relative unit, so it has a similar constraint like the EM unit as well. It is not obvious how much a percentage&nbsp;<code>15px<\/code>&nbsp;is of&nbsp;<code>500px<\/code>? This generally happens when we translate&nbsp;<code>px<\/code>&nbsp;unit from the design stage to web document in many cases.<\/p>\n\n\n\n<p>There are two ways we can take to this matter, either we do it traditionally with a calculator or if you are comfortable with using CSS Pre-processor, you can utilize the&nbsp;<code>percentage()<\/code>&nbsp;function from&nbsp;<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/sass-lang.com\/\">Sass<\/a>.<\/p>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>There are many discussions on the best practices of using units in web design. But ideally the PX unit should be used when the properties generally need to be precise e.g. the&nbsp;<code>border-radius<\/code>&nbsp;and&nbsp;<code>box-shadow<\/code>&nbsp;horizontal or vertical offset, whilst for the EM unit&nbsp;<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/www.w3.org\/Style\/Examples\/007\/units\">as suggested by W3C is better used for font sizing<\/a>. Percentage is the ideal unit to use on responsive layouts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit takes an important role for measuring and building things like a house, a bridge or a tower, and building a website is not an exception. There are a number of methods of measurements used on the Web, specifically in CSS, namely&nbsp;Pixel, EM and Percentage. In this post, we are going to run through these [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":5978,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,145,77],"tags":[25,56,94,146,116,73,217],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/11\/css-units.jpg","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-1yj","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5971"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=5971"}],"version-history":[{"count":1,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5971\/revisions"}],"predecessor-version":[{"id":5979,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5971\/revisions\/5979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/5978"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=5971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=5971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=5971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}