<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shakirul Hasan</title>
    <description>The latest articles on Forem by Shakirul Hasan (@_khanshaheb).</description>
    <link>https://forem.com/_khanshaheb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F181136%2Fb757384c-50d3-462a-bdcb-26f07b183529.png</url>
      <title>Forem: Shakirul Hasan</title>
      <link>https://forem.com/_khanshaheb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/_khanshaheb"/>
    <language>en</language>
    <item>
      <title>Why Should You Not Use px?</title>
      <dc:creator>Shakirul Hasan</dc:creator>
      <pubDate>Wed, 10 Jan 2024 15:09:35 +0000</pubDate>
      <link>https://forem.com/_khanshaheb/why-should-you-not-use-px-1b2m</link>
      <guid>https://forem.com/_khanshaheb/why-should-you-not-use-px-1b2m</guid>
      <description>&lt;p&gt;Using &lt;code&gt;px&lt;/code&gt; or pixels in CSS sizing is generally not recommended. This document explores alternative units of measurement that can be employed instead of &lt;code&gt;px&lt;/code&gt;. But first, let's delve into why &lt;code&gt;px&lt;/code&gt; is often considered a poor choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem with Pixels
&lt;/h2&gt;

&lt;p&gt;Everything displayed on a screen comprises pixels. But why does using pixels for measurement pose a problem? Websites are viewed on a variety of screens, each differing in size, orientation, zoom levels, and pixel geometries. What looks perfect on one screen might be distorted on another.&lt;/p&gt;

&lt;p&gt;Consider a &lt;code&gt;div&lt;/code&gt; on your website set to 600px, occupying half of your screen. When you resize the screen, zoom in or out, or view the site on a mobile device, your website might not display as intended. Moreover, in CSS, a pixel doesn't always correspond to a physical pixel on your screen. On high-resolution displays, several device pixels can combine to form a single CSS pixel.&lt;/p&gt;

&lt;p&gt;Additionally, pixel-based sizing scales poorly. For users with poor eyesight who have increased their device's font size, a fixed font size like &lt;code&gt;14px&lt;/code&gt; on your website might render the text unreadable, as it doesn't adjust to their preferred settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternatives to Pixels
&lt;/h2&gt;

&lt;p&gt;So, what are the alternatives to using &lt;code&gt;px&lt;/code&gt;? Units like &lt;code&gt;em&lt;/code&gt;, &lt;code&gt;rem&lt;/code&gt;, &lt;code&gt;ch&lt;/code&gt;, &lt;code&gt;ex&lt;/code&gt;, &lt;code&gt;vh&lt;/code&gt;, &lt;code&gt;vw&lt;/code&gt;, and others offer more flexibility. Let's explore them in detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding &lt;code&gt;rem&lt;/code&gt; and &lt;code&gt;em&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;rem&lt;/code&gt; unit is relative to the font size of the root element of the document, which is usually the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element. This means that if the font size of the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element is set to 16 pixels, &lt;code&gt;1rem&lt;/code&gt; will be equivalent to 16 pixels. Using &lt;code&gt;rem&lt;/code&gt; for font sizes, margins, padding, and other properties ensures consistency throughout your website. For example, setting paragraph font sizes to &lt;code&gt;1rem&lt;/code&gt; means that they will scale appropriately if the user has different default font size settings in their browser. This makes &lt;code&gt;rem&lt;/code&gt; particularly useful for maintaining accessibility, as it adapts to the preferences of users with poor eyesight.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;em&lt;/code&gt; unit is relative to the font size of the element it's used on. It's useful for scaling designs within an element, such as for text, margins, and padding. If &lt;code&gt;em&lt;/code&gt; is used for an element's font size, it's relative to its own font size. For other properties, it's based on the element's text size.&lt;/p&gt;

&lt;p&gt;Take a look at the example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c"&gt;/* 40px if root font size is 16px */&lt;/span&gt;
  &lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="nt"&gt;span&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c"&gt;/* Twice the font size of h2 */&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&amp;lt;span&amp;gt;&lt;/span&gt;Larger&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt; Text&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, changing &lt;code&gt;h2&lt;/code&gt;'s font size automatically adjusts the &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; size, showcasing &lt;code&gt;em&lt;/code&gt;'s adaptability.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Are &lt;code&gt;ch&lt;/code&gt; and &lt;code&gt;ex&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Ch&lt;/code&gt; and &lt;code&gt;ex&lt;/code&gt; units refer to the width and height of a character, respectively. &lt;code&gt;1ch&lt;/code&gt; is the width of the character &lt;code&gt;0&lt;/code&gt;, and &lt;code&gt;1ex&lt;/code&gt; is the height of the &lt;code&gt;x&lt;/code&gt; character in your font. For instance, if you want a paragraph with a consistent character count per line regardless of font size, you can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30ch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Long text...&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Viewport Units
&lt;/h3&gt;

&lt;p&gt;Viewport units are responsive to the size of the user's browser window. &lt;code&gt;1vw&lt;/code&gt; represents 1% of the viewport's width, and &lt;code&gt;1vh&lt;/code&gt; is 1% of its height. To create a &lt;code&gt;div&lt;/code&gt; that takes half of the viewport's width, set its width to &lt;code&gt;50vw&lt;/code&gt;. The same concept applies to &lt;code&gt;vh&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Additionally, &lt;code&gt;vmin&lt;/code&gt; and &lt;code&gt;vmax&lt;/code&gt; are based on the smaller and larger dimensions of the viewport. A div with &lt;code&gt;width: 50vmin&lt;/code&gt; and &lt;code&gt;height: 50vmax&lt;/code&gt; occupies 50% of the viewport's smallest width and largest height.&lt;/p&gt;

&lt;h3&gt;
  
  
  Percentages (%)
&lt;/h3&gt;

&lt;p&gt;Percentages are relative to the size of the parent element. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;600px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the &lt;code&gt;p&lt;/code&gt; tag inside the &lt;code&gt;div&lt;/code&gt; will be 300px wide.&lt;/p&gt;

&lt;h3&gt;
  
  
  Absolute Lengths
&lt;/h3&gt;

&lt;p&gt;CSS also offers units like &lt;code&gt;cm&lt;/code&gt;, &lt;code&gt;mm&lt;/code&gt;, &lt;code&gt;in&lt;/code&gt;, &lt;code&gt;pt&lt;/code&gt;, etc., based on physical measurements. However, like &lt;code&gt;px&lt;/code&gt;, these units can vary depending on the screen and are typically not recommended.&lt;/p&gt;

&lt;p&gt;In conclusion, while pixels may seem straightforward, they often lead to less flexible and accessible designs. Embracing alternative units like &lt;code&gt;em&lt;/code&gt;, &lt;code&gt;rem&lt;/code&gt;, &lt;code&gt;ch&lt;/code&gt;, &lt;code&gt;ex&lt;/code&gt;, &lt;code&gt;vw&lt;/code&gt;, &lt;code&gt;vh&lt;/code&gt;, &lt;code&gt;vmin&lt;/code&gt;, &lt;code&gt;vmax&lt;/code&gt;, and percentages can make your web designs more adaptable and user-friendly.&lt;/p&gt;

&lt;p&gt;Read the post on &lt;a href="https://medium.com/@shakirulhkhan/why-should-you-not-use-px-3021aa0b5958"&gt;Medium&lt;/a&gt;, &lt;a href="https://open.substack.com/pub/shakirulhasan/p/why-should-you-not-use-px?r=30rvns&amp;amp;utm_campaign=post&amp;amp;utm_medium=web"&gt;Substack&lt;/a&gt; or &lt;a href="https://www.linkedin.com/pulse/why-should-you-use-px-shakirul-hasan-khan-0oa8c"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>design</category>
      <category>html</category>
    </item>
  </channel>
</rss>
