{"id":3208,"date":"2019-11-08T08:46:00","date_gmt":"2019-11-08T08:46:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=3208"},"modified":"2019-11-12T09:55:04","modified_gmt":"2019-11-12T09:55:04","slug":"7-css-units-you-might-not-know-about","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/development\/7-css-units-you-might-not-know-about\/","title":{"rendered":"7 CSS Units You Might Not Know About"},"content":{"rendered":"\n<h2>New CSS Techniques<\/h2>\n\n\n\n<p>It\u2019s easy to get stuck working with the CSS techniques we know well, but doing so puts us at a disadvantage when new problems surface.<\/p>\n\n\n\n<p>As the web continues to grow, the demand for new solutions will also continue to grow. Therefore, as web designers and front end developers, we have no choice but to know our toolset, and know it well.<\/p>\n\n\n\n<p>That means knowing even the specialty tools &#8211; the ones that aren\u2019t used as often, but when they are needed, are exactly the right tool for the job.<\/p>\n\n\n\n<p>Today, I&#8217;m going to introduce you to some CSS tools you might not have known about before. These tools are each units of measurement, like pixels or ems, but it\u2019s quite possible that you\u2019ve never heard of them! Let\u2019s start.<\/p>\n\n\n\n<h2>rem<\/h2>\n\n\n\n<p>We\u2019ll start with something that\u2019s similar to something you are probably already familiar with. The&nbsp;<code>em<\/code>&nbsp;unit is defined as the current&nbsp;<code>font-size<\/code>. So, for instance, if you set a font size on the body element, the&nbsp;<code>em<\/code>&nbsp;value of any child element within the body will be equal to that font size.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><code>&lt;body&gt;<\/code><br>    <code>&lt;div class=\"test\"&gt;Test&lt;\/div&gt;<\/code><br><code>&lt;\/body&gt;<\/code><\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><code>body {<\/code><br>    <code>font-size: 14px;<\/code><br><code>}<\/code><br><code>div {<\/code><br>    <code>font-size: 1.2em; \/\/ calculated at 14px* 1.2, or 16.8px<\/code><br><code>}<\/code><\/p><\/blockquote>\n\n\n\n<p>Here, we\u2019ve said that the div will have a&nbsp;<code>font-size<\/code>&nbsp;of&nbsp;<code>1.2em<\/code>. That\u2019s 1.2 times whatever the font-size it has inherited, in this case 14px. The result is&nbsp;<code>16.8px<\/code>.<\/p>\n\n\n\n<p>However, what happens when you cascade em-defined font sizes inside each other? In the following snippet we apply exactly the same CSS as above. Each div inherits its font-size from its parent, giving us gradually increasing font-sizes.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p> &lt;body&gt;<br>    &lt;div&gt;Test &lt;!&#8211; 14 * 1.2 = 16.8px &#8211;&gt;<br>        &lt;div&gt;Test &lt;!&#8211; 16.8 * 1.2 = 20.16px &#8211;&gt;<br>            &lt;div&gt;Test &lt;!&#8211; 20.16 * 1.2 = 24.192px &#8211;&gt;<br>            &lt;\/div&gt;<br>        &lt;\/div&gt;<br>    &lt;\/div&gt;<br>&lt;\/body&gt;<\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-codepen\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Cascading em Values\" id=\"cp_embed_xbZQRQ\" src=\"https:\/\/codepen.io\/tutsplus\/embed\/preview\/xbZQRQ?height=300&amp;slug-hash=xbZQRQ&amp;default-tabs=html,result&amp;host=https:\/\/codepen.io\" scrolling=\"no\" frameborder=\"0\" height=\"300\" allowtransparency=\"true\" class=\"cp_embed_iframe\" style=\"width: 100%; overflow: hidden;\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>While this may be desired in some cases, often you might want to simply rely on a single metric to scale against. In this case, you should use&nbsp;<code>rem<\/code>. The \u201cr\u201d in&nbsp;<code>rem<\/code>&nbsp;stands for \u201croot\u201d; this is equal to the font-size set at the root element; in most cases that being the&nbsp;<code>html<\/code>&nbsp;element.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>html {<br>    font-size: 14px;<br>}<br>div {<br>    font-size: 1.2rem;<br>} <\/p><\/blockquote>\n\n\n\n<p>In all three of the nested divs in the previous example, the font would evaluate to&nbsp;<code>16.8px<\/code>.<\/p>\n\n\n\n<h3>Good for Grids<\/h3>\n\n\n\n<p>Rems aren\u2019t only useful for font sizing. For example, you could base an&nbsp;entire grid system&nbsp;or UI style library on the root HTML font-size using&nbsp;<code>rem<\/code>, and utilize scaling of&nbsp;<code>em<\/code>&nbsp;in specific places. This would give you more predictable font sizing and scaling.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><code>.container {<\/code><br>    <code>width: 70rem; \/\/ 70* 14px= 980px<\/code><br><code>}<\/code><\/p><\/blockquote>\n\n\n\n<p>Conceptually, the idea behind a strategy like this is to allow your interface to scale with the size of your content. However, it may not necessarily make the most sense for every case.<\/p>\n\n\n\n<h4>Can I use it?<\/h4>\n\n\n\n<p><a href=\"https:\/\/caniuse.com\/#feat=rem\">Feature: rem (root em) units<\/a>&nbsp;on caniuse.com<\/p>\n\n\n\n<h2>vh and vw<\/h2>\n\n\n\n<p>Responsive web design techniques rely heavily on percentage rules. However, CSS percentage isn\u2019t always the best solution for every problem. CSS width is relative to the nearest containing parent element. What if you wanted to use the width or height of the viewport instead of the width of the parent element? That\u2019s exactly what the&nbsp;<code>vh<\/code>&nbsp;and&nbsp;<code>vw<\/code>&nbsp;units provide.&nbsp;<\/p>\n\n\n\n<p>The&nbsp;<code>vh<\/code>&nbsp;element is equal to 1\/100 of the height of the viewport. For example, if the browser\u2019s height is&nbsp;<code>900px<\/code>,&nbsp;<code>1vh<\/code>&nbsp;would evaluate to 9px. Similarly, if the viewport width is&nbsp;<code>750px<\/code>,&nbsp;<code>1vw<\/code>&nbsp;would evaluate to&nbsp;<code>7.5px<\/code>.<\/p>\n\n\n\n<p>There are seemingly endless uses for these rules. For example, a very simple way of doing full-height or near full-height slides can be achieved with a single line of CSS:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p> .slide {<br>    height: 100vh;<br>} <\/p><\/blockquote>\n\n\n\n<p>Imagine you wanted a headline that was set to fill the&nbsp;<em>width<\/em>&nbsp;of the screen. To accomplish this, you would set a font-size in&nbsp;<code>vw<\/code>. That size will scale with the browser\u2019s width.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-codepen\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Demo of vw Unit\" id=\"cp_embed_gbPQga\" src=\"https:\/\/codepen.io\/tutsplus\/embed\/preview\/gbPQga?height=300&amp;slug-hash=gbPQga&amp;default-tabs=css,result&amp;host=https:\/\/codepen.io\" scrolling=\"no\" frameborder=\"0\" height=\"300\" allowtransparency=\"true\" class=\"cp_embed_iframe\" style=\"width: 100%; overflow: hidden;\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4>Can I use it?<\/h4>\n\n\n\n<p><a href=\"http:\/\/caniuse.com\/#feat=viewport-units\">Feature: Viewport units: vw, vh<\/a>&nbsp;on caniuse.com<\/p>\n\n\n\n<h2>vmin and vmax<\/h2>\n\n\n\n<p>While&nbsp;<code>vh<\/code>&nbsp;and&nbsp;<code>vm<\/code>&nbsp;are always related to the viewport height and width, respectively,&nbsp;<code>vmin<\/code>&nbsp;and&nbsp;<code>vmax<\/code>&nbsp;are related to the&nbsp;<em>maximum<\/em>&nbsp;or&nbsp;<em>minimum<\/em>&nbsp;of those widths and heights, depending on which is smaller and larger. For example, if the browser was set to 1100px wide and the 700px tall,&nbsp;<code>1vmin<\/code>&nbsp;would be 7px and&nbsp;<code>1vmax<\/code>&nbsp;would be 11px. However, if the width was set to 800px and the height set to 1080px,&nbsp;<code>vmin<\/code>&nbsp;would be equal to 8px while&nbsp;<code>vmax<\/code>&nbsp;would be set to&nbsp;<code>10.8px<\/code>.<\/p>\n\n\n\n<p>So, when might you use these values?<\/p>\n\n\n\n<p>Imagine you need an element that is always visible on screen. Using a height and width set to a&nbsp;<code>vmin<\/code>&nbsp;value below 100 would enable this. For example, a square element that always touches at least two sides of the screen might be defined like this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>.box {<br>    height: 100vmin;<br>    width: 100vmin;<br>}<\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"3211\" data-permalink=\"https:\/\/azoora.com\/blog\/development\/7-css-units-you-might-not-know-about\/attachment\/vmin-1\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmin-1.png\" data-orig-size=\"850,629\" 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=\"vmin-1\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmin-1-300x222.png\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmin-1.png\" loading=\"lazy\" width=\"850\" height=\"629\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmin-1.png\" alt=\"\" class=\"wp-image-3211\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmin-1.png 850w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmin-1-300x222.png 300w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmin-1-768x568.png 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>If you needed a square box that always&nbsp;<em>covers<\/em>&nbsp;the visible viewport (touching all four sides of the screen at all times), use the same rules except with&nbsp;<code>vmax<\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>.box {<br>    height: 100vmax;<br>    width: 100vmax;<br>}<\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"3210\" data-permalink=\"https:\/\/azoora.com\/blog\/development\/7-css-units-you-might-not-know-about\/attachment\/vmax-1\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmax-1.png\" data-orig-size=\"850,629\" 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=\"vmax-1\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmax-1-300x222.png\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmax-1.png\" loading=\"lazy\" width=\"850\" height=\"629\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmax-1.png\" alt=\"\" class=\"wp-image-3210\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmax-1.png 850w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmax-1-300x222.png 300w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/vmax-1-768x568.png 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>Combinations of these rules provide a very flexible way of utilizing the size of your viewport in new and exciting ways.<\/p>\n\n\n\n<h4>Can I use it?<\/h4>\n\n\n\n<p><strong>Feature<\/strong>: <a href=\"http:\/\/caniuse.com\/#feat=viewport-units\">Viewport units: vmin, vmax&nbsp;on caniuse.com<\/a><\/p>\n\n\n\n<h2>ex and ch<\/h2>\n\n\n\n<p>The units&nbsp;<code>ex<\/code>&nbsp;and&nbsp;<code>ch<\/code>, similar to&nbsp;<code>em<\/code>&nbsp;and&nbsp;<code>rem<\/code>, rely on the current font and font size. However, unlike&nbsp;<code>em<\/code>&nbsp;and&nbsp;<code>rem<\/code>, these units also rely on the&nbsp;<code>font-family<\/code>, as they are determined based on font-specific measures.<\/p>\n\n\n\n<p>The&nbsp;<code>ch<\/code>&nbsp;unit, or the&nbsp;<em>character<\/em>&nbsp;unit is defined as being the \u201cadvanced measure\u201d of the width of the zero character,&nbsp;<code>0<\/code>. Some very interesting discussion about what this means&nbsp;<a href=\"http:\/\/meyerweb.com\/eric\/thoughts\/2012\/05\/15\/defining-ch\/\">can be found on Eric Meyers&#8217;s blog<\/a>, but the basic concept is that, given a monospace font, a box with a width of&nbsp;<code>N<\/code>&nbsp;character units, such as&nbsp;<code>width: 40ch;<\/code>, can always contain a string with 40 characters in that particular font. While conventional uses of this particular rule relate to laying out braille, the possibilities for creativity here certainly extend beyond these simple applications.<\/p>\n\n\n\n<p>The&nbsp;<code>ex<\/code>&nbsp;unit is defined as the \u201cx-height of the current font OR one-half of one&nbsp;<code>em<\/code>\u201d. The <code>x-height<\/code>&nbsp;of a given font is the height of the lower-case x of that font. Often times, this is about at the middle mark of the font.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"3209\" data-permalink=\"https:\/\/azoora.com\/blog\/development\/7-css-units-you-might-not-know-about\/attachment\/x2-1\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/x2-1.png\" data-orig-size=\"850,400\" 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=\"x2-1\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/x2-1-300x141.png\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/x2-1.png\" loading=\"lazy\" width=\"850\" height=\"400\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/x2-1.png\" alt=\"\" class=\"wp-image-3209\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/x2-1.png 850w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/x2-1-300x141.png 300w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/x2-1-768x361.png 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>There are&nbsp;<em>many<\/em>&nbsp;uses for this kind of unit, most of them being for typographic micro-adjustments. For example, the&nbsp;<code>sup<\/code>&nbsp;element, which stands for&nbsp;<em>superscript<\/em>, can be pushed up in the line using position relative and a bottom value of 1ex. Similarly, you can pull a subscript element down. The browser defaults for these utilize superscript- and subscript-specific&nbsp;<code>vertical-align<\/code>&nbsp;rules, but if you wanted more granular control, you could handle the type more explicitly like this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><code>sup {<\/code><br>    <code>position: relative;<\/code><br>    <code>bottom: 1ex;<\/code><br><code>}<\/code><br><code>sub<\/code> <code>{<\/code><br>    <code>position: relative;<\/code><br>    <code>bottom: -1ex;<\/code><br><code>}<\/code><\/p><\/blockquote>\n\n\n\n<h4>Can I Use it?<\/h4>\n\n\n\n<p>The&nbsp;<code>ex<\/code>&nbsp;unit has&nbsp;<a href=\"http:\/\/www.w3.org\/TR\/REC-CSS1\/#length-units\">been around since CSS1<\/a>, though you won\u2019t find such solid support for the&nbsp;<code>ch<\/code>&nbsp;unit. For specifics on support, check out&nbsp;<a href=\"http:\/\/www.quirksmode.org\/css\/units-values\/\">CSS units and values<\/a>&nbsp;on quirksmode.org.<\/p>\n\n\n\n<h2>Conclusion:<\/h2>\n\n\n\n<p>Keeping an eye on the continued development and expansion of CSS is incredibly important so that you know all of the tools in your toolset. Perhaps you will encounter a particular problem that requires an unexpected solution utilizing one of these more obscure measurement units. Take time to read over&nbsp;<a href=\"http:\/\/www.w3.org\/TR\/css3-values\/\">new specifications<\/a>. Sign up for news updates from free resources like&nbsp;our <a href=\"https:\/\/azoora.com\/blog\">blog<\/a>. And don\u2019t forget to <a href=\"https:\/\/azoora.com\/blog\/#blog_subscription-2\">subscribe<\/a>&nbsp;for daily updates on latest trends, news, design &amp; development resources and free tutorials like this one from Web Design on Azoora, Inc.!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>New CSS Techniques It\u2019s easy to get stuck working with the CSS techniques we know well, but doing so puts us at a disadvantage when new problems surface. As the web continues to grow, the demand for new solutions will also continue to grow. Therefore, as web designers and front end developers, we have no [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3212,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[145,168],"tags":[56,94,146,116,106],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/CSSUnits.jpg","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-PK","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/3208"}],"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=3208"}],"version-history":[{"count":3,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/3208\/revisions"}],"predecessor-version":[{"id":3217,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/3208\/revisions\/3217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/3212"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=3208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=3208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=3208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}