<?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: Kiambuthi Kinuthia</title>
    <description>The latest articles on DEV Community by Kiambuthi Kinuthia (@iamkiambuthi).</description>
    <link>https://dev.to/iamkiambuthi</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%2F653368%2Ffdf2ef71-ca8a-4b69-98a0-a16bc876269f.jpg</url>
      <title>DEV Community: Kiambuthi Kinuthia</title>
      <link>https://dev.to/iamkiambuthi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iamkiambuthi"/>
    <language>en</language>
    <item>
      <title>CSS Variables | Learning CSS Best Practices - #01</title>
      <dc:creator>Kiambuthi Kinuthia</dc:creator>
      <pubDate>Fri, 24 Sep 2021 16:17:28 +0000</pubDate>
      <link>https://dev.to/iamkiambuthi/css-variables-learning-css-best-practices-01-3h3g</link>
      <guid>https://dev.to/iamkiambuthi/css-variables-learning-css-best-practices-01-3h3g</guid>
      <description>&lt;p&gt;to declare a variable prefix your variable name with two dashes then to use it, use the &lt;code&gt;var()&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* -- declaring the variable. -- */

--variable_name: variable_value

/* -- accessing the variable. -- */

html {
    font-family: var(--variable_name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Variable Scope | Global or Local ?
&lt;/h3&gt;

&lt;p&gt;Variables can be either scoped globally or locally. declaring your variables inside the &lt;code&gt;:root&lt;/code&gt; pseudo-class makes them available in the global scope and available for use throughout ythe stylesheet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;header {
    --bg-color: red;

    /* bg-color is only visisble to all children of the header element */
}

main {
    background: var(--bg-color);

    /* bg-color is undefined. */
}

/* to declare global variables place all variable inside the :root pseudo-class */

:root {
    bg-color: red;
}

header {
    background: var(--bg-color);
}

main {
    background: var(--bg-color);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Common Use Cases
&lt;/h3&gt;

&lt;p&gt;One way to use css variables is to save common colors or an entire color pallete which you can then use to style elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:root {
   /* -- Use css variables to declare site colors -- */
   --primary-color: black;
   --secondary-color: gray;
   --tetiary-color: white;

   /* -- Color Palettes -- */
   /* -- black color palette */
   --black-clr-plt-1: rgb(0, 0, 0);
   --black-clr-plt-2: rgb(30, 30, 30);
   --black-clr-plt-3: rgb(60, 60, 60);
   --black-clr-plt-4: rgb(90, 90, 90);

   /* -- brown color palette -- */
   --brown-clr-plt-1: rgb(60, 20, 20);
   --brown-clr-plt-2: rgb(90, 30, 30);
   --brown-clr-plt-3: rgb(120, 40, 40);
   --brown-clr-plt-4: rgb(150, 50, 50);
}

/* -- styling -- */
body {
   background: var(--brown-clr-plt-3);
   color: var(--black-color-plt-1);
   border: 1px solid var(--primary-color);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Providing Fallback Values
&lt;/h3&gt;

&lt;p&gt;To provide a fallback value in case the variable is undefined, pass the fallback value as a second argument into the &lt;code&gt;var()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:root {
   --site-font: 'Raleway';
}

html {
   font-family: var('Raleway', monospace);
}

/* In this example if site-font is undefined, the monospace font is used. */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that's it, you're welcome to follow the journey onwards on my &lt;a href="https://github.com/iamkiambuthi/css-sandbox"&gt;github&lt;/a&gt;&lt;/p&gt;

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