DEV Community

Cover image for Adding Dark / Light Theme To The Website Using Only CSS
Bipin Rajbhar
Bipin Rajbhar

Posted on β€’ Edited on

39 9

Adding Dark / Light Theme To The Website Using Only CSS

Hello everyone πŸ‘‹, I hope you are doing great.

So, Today you are going to learn how to Add dark/light theme to your website using only CSS 😎.

HTML

Let's first set up our HTML.

<input id="toggle-theme" class="toggle-theme" type="checkbox" checked/>

<main >
  <label for="toggle-theme" class="toggle-theme-label">
    <span>πŸ’‘</span>
</label>
  <article>
  <header>Dark / Light Mode Using Only CSS</header>
  <p>...</p>
</article>
</main>
Enter fullscreen mode Exit fullscreen mode

CSS

Now lets set the background-colour and text colour of our HTML using CSS variables.


:root {
   --bg-color: #edf2f4;
   --text-color: #011627;
}

main {
  padding: 24px;
  background-color: var(--bg-color);
  color: var(--text-color);
}
Enter fullscreen mode Exit fullscreen mode

Now, lets set up the checkbox functionality when the checkbox is checked.

/* all the magic happing here ✨*/
input[type="checkbox"]:checked ~ main {
  --bg-color: #011627;
  --text-color: #edf2f4;
}
Enter fullscreen mode Exit fullscreen mode

That's It 😎.

Example

πŸ“š Further Reading:

Thanks for reading! My name is Bipin Rajbhar; I love helping people to learn new skills 😊. You can follow me on Twitter if you’d like to be notified about new articles and resources.

Image of Stellar post

How a Hackathon Project became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Watch the video

Top comments (5)

Collapse
 
cbartlett profile image
Colin Bartlett β€’

I found an interesting tool to help with this the other day on ProductHunt: nighteye.app/dark-css-generator/

It worked pretty well! Not sure how maintainable it is to have a separate stylesheet. But I was very impressed with the results they created.

Collapse
 
ameedjamous profile image
Ameed Jamous β€’

I wish all websites and docs had this light bulb :) Good stuff!

Collapse
 
adgower profile image
Alex Gower β€’

Awesome

Collapse
 
obasekietinosa profile image
Etinosa Obaseki β€’

🀯🀯🀯 Are there any drawbacks to using this as opposed to more involved solutions?

Collapse
 
bipinrajbhar profile image
Bipin Rajbhar β€’

maybe or may not πŸ€”

If you have answer let me know in the comment πŸ˜„

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay