<?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>DEV Community: Tanay J</title>
    <description>The latest articles on DEV Community by Tanay J (@tanayj).</description>
    <link>https://dev.to/tanayj</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%2F541515%2F25ea02da-83c4-4d22-895a-d9d44a1fb8d1.jpg</url>
      <title>DEV Community: Tanay J</title>
      <link>https://dev.to/tanayj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tanayj"/>
    <language>en</language>
    <item>
      <title>Understanding CSS Units</title>
      <dc:creator>Tanay J</dc:creator>
      <pubDate>Wed, 16 Dec 2020 13:00:00 +0000</pubDate>
      <link>https://dev.to/tanayj/understanding-css-units-38e4</link>
      <guid>https://dev.to/tanayj/understanding-css-units-38e4</guid>
      <description>&lt;p&gt;As a coding newbie, my first encounter with CSS units was not very pleasant. There are enough of them to confuse someone who has just started their coding journey. So, if you are using &lt;code&gt;rem&lt;/code&gt; all the time because you are clueless about what &lt;code&gt;em&lt;/code&gt;,&lt;code&gt;vh&lt;/code&gt;,&lt;code&gt;vw&lt;/code&gt; and others are, here's an explainer for you.&lt;/p&gt;

&lt;p&gt;CSS units can be divided into two types -  Absolute Units and Relative Units&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Absolute units&lt;/strong&gt;: These are straight forward units with fixed size. One centimetre is one centimetre irrespective of the size of the screen. Hence, these are suitable for environment where output screen size is fixed and known. Given below are the absolute units we can use in CSS-&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;px&lt;/code&gt;: One device pixel of the display&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mm&lt;/code&gt;: One millimetre&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cm&lt;/code&gt;: One centimetre&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;in&lt;/code&gt;: One inch&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pt&lt;/code&gt;: One point (which is 1/72 of an inch).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pc&lt;/code&gt;: One pica (which is 12 points).&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Relative Units&lt;/strong&gt;: As the name suggests, these are relative to something, say, font-size. This ensures the scalability of the webpage across screen-sizes. Given below are the relative units in CSS- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;rem&lt;/code&gt; and &lt;code&gt;em&lt;/code&gt;: These units are relative to the font sizes. &lt;code&gt;rem&lt;/code&gt; corresponds to the font-size of root element and &lt;code&gt;em&lt;/code&gt; corresponds to the font size of parent element.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
font-size: 10px;
}
div h2 {
font-size: 3em;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;font-size&lt;/code&gt; of h2 will be 30px&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
font-size: 10px;
}
div h2 {
font-size: 2rem;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;font-size&lt;/code&gt; of h2 will be 20px &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;vh&lt;/code&gt; and &lt;code&gt;vw&lt;/code&gt;: While making a design responsive, percentage isn't the best solution all the time and this is where &lt;code&gt;vh&lt;/code&gt; and &lt;code&gt;vw&lt;/code&gt; come to our rescue. &lt;code&gt;vh&lt;/code&gt; equals 1/100 of the height of the viewport (visible area of a web page). So, if the viewport height is 500px, 1vh equals 5px. Thus, unlike percentage, &lt;code&gt;vh&lt;/code&gt; &lt;code&gt;vw&lt;/code&gt; allow sizing based on viewport size.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
height: 100vh;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This will fill the entire height of the viewport.&lt;/em&gt;&lt;br&gt;
Similarly, &lt;code&gt;vw&lt;/code&gt; corresponds to width of the viewport i.e. if the viewport width is 700px, 1vw equals 7px.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;vmin&lt;/code&gt; and &lt;code&gt;vmax&lt;/code&gt;: These units correspond to minimum and maximum values of &lt;code&gt;vh&lt;/code&gt; and &lt;code&gt;vw&lt;/code&gt;. &lt;br&gt;
Eg. Let's say, the viewport has a height of 900px and width of 500px. Here, &lt;code&gt;vmin&lt;/code&gt; will be 5px and &lt;code&gt;vmax&lt;/code&gt; will be 9px. Thus, &lt;code&gt;vmin&lt;/code&gt; is related to minimum of viewport's height and width and &lt;code&gt;vmax&lt;/code&gt; is related to maximum of viewport's height and width.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ex&lt;/code&gt; and &lt;code&gt;ch&lt;/code&gt;: To understand these units, think of rem/em and add &lt;code&gt;font-family&lt;/code&gt; to the relation. These units consider the &lt;code&gt;font-family&lt;/code&gt; as they rely on dimensions of different fonts. &lt;code&gt;ch&lt;/code&gt; corresponds to width of character 0 in the particular font. &lt;code&gt;ex&lt;/code&gt; is defined by height of lower case x in the given font, which for many fonts is equal to 0.5em.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we can see, the absolute units (&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; and &lt;code&gt;pc&lt;/code&gt;) will have fixed size irrespective of the output environment. Since screen sizes vary too much, these units are not recommended. For this reason, the relative units are best suited for the web developers.&lt;/p&gt;

</description>
      <category>css</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
