<?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: Prashant Bhat</title>
    <description>The latest articles on DEV Community by Prashant Bhat (@prashantbhat84).</description>
    <link>https://dev.to/prashantbhat84</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%2F622834%2F924c5906-2c4f-4ce7-a28a-fd9b73ab43c5.jpeg</url>
      <title>DEV Community: Prashant Bhat</title>
      <link>https://dev.to/prashantbhat84</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prashantbhat84"/>
    <language>en</language>
    <item>
      <title>CSS Variables in React.js</title>
      <dc:creator>Prashant Bhat</dc:creator>
      <pubDate>Thu, 29 Apr 2021 17:17:38 +0000</pubDate>
      <link>https://dev.to/prashantbhat84/css-variables-in-react-js-259c</link>
      <guid>https://dev.to/prashantbhat84/css-variables-in-react-js-259c</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;The purpose of this post is to show the use of CSS varibles in React.js  applications&lt;/p&gt;

&lt;h1&gt;
  
  
  CSS Variables with HTML and CSS
&lt;/h1&gt;

&lt;p&gt;In a typical app which consists of HTML ,CSS and JS the CSS file is linked to the html file  and we have the root pseudo selector as shown below.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---ErzYnYW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8sm3vdbjpdg2q0ec4qp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---ErzYnYW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8sm3vdbjpdg2q0ec4qp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now in a react app its a SPA but its component based structure. Each component  has a separate CSS file attached to it.&lt;/p&gt;

&lt;p&gt;So the question is how can we have a common set of CSS variables which can be used throughout the application?&lt;/p&gt;

&lt;h1&gt;
  
  
  CSS Variables in React applications
&lt;/h1&gt;

&lt;p&gt;This is a standard react application structure.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xjm1BiOF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8un9vrg2539e9u186np3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xjm1BiOF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8un9vrg2539e9u186np3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
We have the index.css  file where we can define the root pseudo selector. In that we can define the  CSS variables which we can use throughout the application.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H0e4_vvx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8bdklbmgzlrm0fomtk9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H0e4_vvx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8bdklbmgzlrm0fomtk9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Now we need to import the index.css file into the index.js file as shown below.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hdo214-n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05uymhina7wxn0a4yay0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hdo214-n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05uymhina7wxn0a4yay0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can use these declared CSS variables throughout the app. Because React being a SPA all the variables declared in index.css will be applicable  throughout the application as shown in the app.css file  image below.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W5eFUZVl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ndm85xcbvg2y1qmxcis.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W5eFUZVl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ndm85xcbvg2y1qmxcis.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading this article&lt;/p&gt;

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