Dark theme with CSS variable
Published: 2019-06-12 15:21:05

<!-- wp:paragraph -->
<p>Nowadays, more and more applications and OS are going to adapt to the new trend: dark theme and web applications are not exceptions. Dark theme is not the necessary feature for the web application but it's nice to have. Giving your users more choice base on their preference always help your application more attractive. Today I gonna show you some way to apply the dark theme in your application.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Solution 1: Use css nested</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let imagine that your current web application only supports the light theme, and now you want to add a new feature that allows users to change the application's theme in runtime. So the easiest way to we defined a new class for dark theme, apply that class to the root element (<code>&lt;html&gt;</code> or <code>&lt;body&gt;</code>). Because the application's theme must be consistent for the whole application so to make sure every element of our application follow the selected theme, we must set that class to root element.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After we have a class for the dark theme, we will jump to customize the look &amp; feel for every child inside. 2 most important CSS properties we need bear in mind is <code>color</code>and <code>background-color</code>. Beside that, we also need to take care of other CSS properties as well depend on your application's implementation. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Something like this</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code {"language":"css"} -->
<pre class="wp-block-syntaxhighlighter-code">/* Default css for light theme */
body {
  background-color: white;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  transition: all linear 1s;
}
.panel {
  background-color: lightblue;
}
label{
  color:#0d47a1;
}
/* Custom css for dark theme */
[data-theme=dark-theme] {
  background-color:gray ;
}
[data-theme=dark-theme] .panel {
  background-color: darkgray;
}
[data-theme=dark-theme] label {
  color: rgb(245,245,245);
}</pre>
<!-- /wp:syntaxhighlighter/code -->
Live demo
{% codepen https://codepen.io/phuchieuct93abc/pen/QXwYzK/ default-tab=css,result%}

<!-- wp:paragraph -->
<p>This way works perfectly however, it still has one big drawback is we need to duplicate CSS code for every component we want to customize between light theme and dark theme. In the above sample, you can see that we need to redefined CSS for label and panel twice, even worse we also need to repeat <code>[data-theme=dark-theme]</code> many times. To get rid of this disadvantage, I want to introduce you another approach to apply dark theme is using css variable.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Solution 2: CSS variable</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The idea behind this solution is we will define some CSS values as a variable, and we define every element will reuse CSS variable instead of defined the new one. It means the value of that variable is flexible during runtime. By this way, we only need to defined customize CSS once (these CSS properties should depend on CSS variable), besides that we also need to define 2 sets of variables: one for dark theme and one for the light theme. This way is much simpler than the solution 1 with nested CSS.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let take a look on the CSS variable version of above example:</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code {"language":"css"} -->
<pre class="wp-block-syntaxhighlighter-code">/* Default css for light theme */
body {
  --backgroundColor: white;
  --panelBackgroundColor: lightblue;
  --labelColor: #0d47a1;
}
body[data-theme="dark-theme"] {
  --backgroundColor: gray;
  --panelBackgroundColor: darkgray;
  --labelColor: rgb(245, 245, 245);
}
body {
  background-color: var(--backgroundColor);
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  transition: all linear 1s;
}
.panel {
  background-color: var(--panelBackgroundColor);
}
label {
  color: var(--labelColor);
}
</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>Live demo:</p>
<!-- /wp:paragraph -->
Live demo
{% codepen https://codepen.io/phuchieuct93abc/pen/ZdYZpd/ default-tab=css,result %}


<!-- wp:heading -->
<h2> Final words</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Hope that you learned something about CSS variable and willing to apply it in your next project, not only for theme changing but also for flexible web application. Let let us know your feeling in the comment section below and see you in the next post.</p>
<!-- /wp:paragraph -->
You can also visit my blog to read more interesting topics: https://frontend.cloudaccess.host/dark-theme-with-css-variable/