<?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: Azin Asili</title>
    <description>The latest articles on DEV Community by Azin Asili (@azinasili).</description>
    <link>https://dev.to/azinasili</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%2F2914%2Ff0b5312c1ea96f74ae7acb015201c73b.jpeg</url>
      <title>DEV Community: Azin Asili</title>
      <link>https://dev.to/azinasili</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/azinasili"/>
    <language>en</language>
    <item>
      <title>Utilizing the “C in CSS</title>
      <dc:creator>Azin Asili</dc:creator>
      <pubDate>Wed, 08 Feb 2017 20:02:56 +0000</pubDate>
      <link>https://dev.to/azinasili/utilizing-the-c-in-css</link>
      <guid>https://dev.to/azinasili/utilizing-the-c-in-css</guid>
      <description>

&lt;p&gt;&lt;em&gt;This article was originally posted on the &lt;a href="https://blog.graphitegtc.com/"&gt;Graphite GTC blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For a lot of developers CSS is a tricky beast to handle. Developers always tack on rules to the end of their styles sheets, causing some unintended consequences. After some raging and cursing at why CSS exists, an &lt;code&gt;!important&lt;/code&gt; is added and voilÃ , it all works. Most of the time, these issues are caused by the &lt;em&gt;cascading&lt;/em&gt; nature of CSS. Because of the deep seeded hatred for the “C in CSS, many smart people developed methodologies to help reduce this feature or to completely remove it. These methodologies include &lt;a href="https://www.smashingmagazine.com/2011/12/an-introduction-to-object-oriented-css-oocss/"&gt;OOCSS&lt;/a&gt;, &lt;a href="https://smacss.com/"&gt;SMACSS&lt;/a&gt;, and, &lt;a href="http://getbem.com/"&gt;BEM&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The point of this post is not to argue against these methodologies; they’re really useful in managing your style sheets and also to keep your CSS uniform. What if we were able to use the cascading feature of CSS to our benefit? Imagine if we could write one CSS rule and have it propagate to the descending elements.&lt;/p&gt;

&lt;p&gt;To truly utilize the cascading nature of style sheets we will need to use some lesser known CSS units and keywords. We will employ the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EM (and sometimes REM) units&lt;/li&gt;
&lt;li&gt;inherit&lt;/li&gt;
&lt;li&gt;currentColor&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  EMs and REMs
&lt;/h2&gt;

&lt;p&gt;You’re probably familiar with using &lt;code&gt;PX&lt;/code&gt; units for sizing elements, margin, padding, borders, etc. We can do the same with &lt;code&gt;EMs&lt;/code&gt; and &lt;code&gt;REMs&lt;/code&gt; but with the added benefit that these units are relative rather than absolute values. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/length#Relative_length_units"&gt;MDN&lt;/a&gt; defines &lt;code&gt;EMs&lt;/code&gt; as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This unit represents the calculated font-size of the element. If used on the font-size property itself, it represents the inherited font-size of the element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;MDN also gives this tidbit about &lt;code&gt;EMs&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This unit is often used to create scalable layouts, which keep the vertical rhythm of the page, even when the user changes the size of the fonts. The CSS properties line-height, font-size, margin-bottom and margin-top often have values expressed in em.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note that MDN says that &lt;code&gt;EMs&lt;/code&gt; &lt;em&gt;“represents the inherited font-size of the element”&lt;/em&gt; and are &lt;em&gt;“used to create scalable layouts”&lt;/em&gt;. This is the exact thing we are looking for! Here is what &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/length#Relative_length_units"&gt;MDN&lt;/a&gt; says about &lt;code&gt;REMs&lt;/code&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This unit represents the font-size of the root element (e.g. the font-size of the  element). When used on the font-size on this root element, it represents its initial value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The biggest difference between the two is &lt;code&gt;REMs&lt;/code&gt; always references it’s value based on the root element’s &lt;code&gt;font-size&lt;/code&gt;, while &lt;code&gt;EMs&lt;/code&gt; inherits it’s unit from the element’s &lt;code&gt;font-size&lt;/code&gt;. If this seems confusing, don’t worry, take a look at the following CSS to get a better understanding:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.element&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;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;.5em&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 the example above the element’s padding would equate to 10px. Remember &lt;code&gt;EM&lt;/code&gt; units are relative to the element’s &lt;code&gt;font-size&lt;/code&gt;, this means our padding equation is 20 * .5 = 10.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.element&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;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;.5em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.element-child&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;.25em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&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;Imagine this scenario: we know that &lt;code&gt;.element&lt;/code&gt; padding is equal to 10px, but what about the values for &lt;code&gt;.element-child&lt;/code&gt;? Well because &lt;code&gt;EMs&lt;/code&gt; compound our math, things can get a little tricky. The &lt;code&gt;font-size&lt;/code&gt; for &lt;code&gt;.element-child&lt;/code&gt; will equate to 5px (20 * .25 = 5). Since the parent element has a &lt;code&gt;font-size&lt;/code&gt; of 20px and we defined the child element as .25em of the parent’s, our compounded value is equal to 5px. The padding is a lot easier in this case, because &lt;code&gt;EMs&lt;/code&gt; look at the &lt;code&gt;font-size&lt;/code&gt; value, our padding equates to 5px.&lt;/p&gt;

&lt;p&gt;Hopefully that wasn’t too much to handle. The tricky part with &lt;code&gt;EMs&lt;/code&gt; is managing how to calculate the output values. Since it’s a hassle I’ve created a set of functions and mixins for Sass to make our lives easier! You can find the &lt;a href="https://gist.github.com/azinasili/8e41854f58176cbd9cff"&gt;Github Gist here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inherit Keyword
&lt;/h2&gt;

&lt;p&gt;Another tool in our arsenal is the &lt;code&gt;inherit&lt;/code&gt; keyword, this value allows us to get the style that is defined on a parent element and apply it to the child. We can do this without having the same rule defined twice. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/inherit"&gt;MDN&lt;/a&gt; defines &lt;code&gt;inherit&lt;/code&gt; as the following:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The inherit CSS-value causes the element for which it is specified to take the computed value of the property from its parent element. It is allowed on every CSS property.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine the following scenario using the &lt;code&gt;inherit&lt;/code&gt; keyword:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.element&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;coral&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.element-child&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;inherit&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;Our &lt;code&gt;.element-child&lt;/code&gt; would get the value for padding from &lt;code&gt;.element&lt;/code&gt; and apply it to itself. This is nice because if the &lt;code&gt;padding&lt;/code&gt; on &lt;code&gt;.element&lt;/code&gt; changes, so will the &lt;code&gt;padding&lt;/code&gt; for &lt;code&gt;.element-child&lt;/code&gt;! This is perfect if we are trying to avoid writing the same styles multiple times. The one downside of this technique is the keyword only looks at the parent elements value; we are unable to move up in the DOM tree and get the value of an ancestor element.&lt;/p&gt;

&lt;h2&gt;
  
  
  CurrentColor Keyword
&lt;/h2&gt;

&lt;p&gt;This keyword acts like &lt;code&gt;inherit&lt;/code&gt; with one big difference, &lt;code&gt;currentColor&lt;/code&gt; can be used to inherit the color of properties that normally do not inherit that value. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#currentColor_keyword"&gt;MDN&lt;/a&gt; defines &lt;code&gt;currentColor&lt;/code&gt; as the following:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The currentColor keyword represents the calculated value of the element's color property. It allows to make the color properties inherited by properties or child's element properties that do not inherit it by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we have some knowledge of the techniques we can use to manage how our values cascade, let’s put it into action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples of our new techniques
&lt;/h2&gt;

&lt;p&gt;See the Pen &lt;a href="http://codepen.io/azinasili/pen/GrQQmW/"&gt;Utilizing the 'C' inÂ CSS - Button Example&lt;/a&gt; by Azin Asili (&lt;a href="http://codepen.io/azinasili"&gt;@azinasili&lt;/a&gt;) on &lt;a href="http://codepen.io"&gt;CodePen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The above example is just getting our feet wet. Although it’s simple, it perfectly shows how &lt;code&gt;EMs&lt;/code&gt; can be used to scale our elements. You will notice two buttons, both look the same except &lt;code&gt;.btn--large&lt;/code&gt; is, well larger. Even though the button is larger, we never adjusted the padding for the button or even reset it’s &lt;code&gt;border-radius&lt;/code&gt;, instead we only adjusted the &lt;code&gt;font-size&lt;/code&gt;. Neat!&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;EMs&lt;/code&gt; inherit the &lt;code&gt;font-size&lt;/code&gt; value, any property that is also using &lt;code&gt;EMs&lt;/code&gt; will scale. Since our &lt;code&gt;.btn--large&lt;/code&gt; is 1.25x larger than the initial &lt;code&gt;.btn&lt;/code&gt;, that means the other values will also be 1.25x larger. This is exactly what we want with our cascade. The compounding of values may cause issues if you’re not careful, but when deliberately used it can be the best way to create alternative sized elements with minimum effort.&lt;/p&gt;

&lt;p&gt;See the Pen &lt;a href="http://codepen.io/azinasili/pen/pRLEwR/"&gt;Utilizing the 'C' inÂ CSS - Cards&lt;/a&gt; by Azin Asili (&lt;a href="http://codepen.io/azinasili"&gt;@azinasili&lt;/a&gt;) on &lt;a href="http://codepen.io"&gt;CodePen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This card example expands on some of the principals for cascading. Again, we are using &lt;code&gt;EMs&lt;/code&gt; to size our elements but now we are also using the &lt;code&gt;currentColor&lt;/code&gt; and &lt;code&gt;inherit&lt;/code&gt; keywords to style certain elements. Rather than hard-coding colors and other values, we can use our handy keywords to have the styles cascade down to the element we want.&lt;/p&gt;

&lt;p&gt;See the Pen &lt;a href="http://codepen.io/azinasili/pen/NdYdeZ/"&gt;Utilizing the 'C' inÂ CSS - Pricing Table&lt;/a&gt; by Azin Asili (&lt;a href="http://codepen.io/azinasili"&gt;@azinasili&lt;/a&gt;) on &lt;a href="http://codepen.io"&gt;CodePen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Our last example will demonstrate our new found cascading knowledge and apply it to a more real-world scenario. Much like the other examples we will continue to use &lt;code&gt;EMs&lt;/code&gt; to size our elements, but with one exception, the &lt;code&gt;margin&lt;/code&gt; on &lt;code&gt;.price&lt;/code&gt; will use &lt;code&gt;REMs&lt;/code&gt; instead. This is because we do not want our margins to scale with any changes to &lt;code&gt;font-size&lt;/code&gt;. Notice the middle table is larger than the other two? We not only use color to highlight that specific option but we also resized the whole table to be larger than the other two. Both the color and the increase in size is to draw attention to our customers. Normally with the larger sized element we would have to resize every &lt;code&gt;padding&lt;/code&gt; and &lt;code&gt;font-size&lt;/code&gt; properties. Instead we use the power of &lt;code&gt;EM&lt;/code&gt; and the &lt;em&gt;cascade&lt;/em&gt; to do this for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The above techniques are a fantastic way to take back control of your CSS, however, it does take a little extra brainpower. Using &lt;code&gt;EMs&lt;/code&gt; for unit values can cause issues when you have nested elements. This can be solved with some simple math or with &lt;a href="https://gist.github.com/azinasili/8e41854f58176cbd9cff"&gt;functions/mixins&lt;/a&gt; if you’re using Sass or any other pre/post processor. Either way, a little bit of extra effort can go a long way, making your style sheets more flexible and scalable for your projects. Let me know what you guys think!&lt;/p&gt;


</description>
      <category>css</category>
      <category>frontenddevelopment</category>
      <category>sass</category>
    </item>
  </channel>
</rss>
