Easy Dark Mode Using CSS

Easy Dark Mode Using CSS
Code Snippet:Light / Dark Switch, Pure CSS, No JS
Author: Agil Kerimov
Published: January 21, 2024
Last Updated: January 22, 2024
Downloads: 616
License: MIT
Edit Code online: View on CodePen
Read More

This code provides an easy way to implement a dark mode using CSS. It utilizes a checkbox to toggle between light and dark themes, enhancing user experience by adjusting the website’s color scheme with a smooth transition.

You can use this code in your web projects to quickly add a user-friendly dark mode feature. It improves accessibility by allowing users to switch between light and dark themes, enhancing readability in different lighting conditions.

How to Create Easy Dark Mode Using CSS

1. Begin by setting up the HTML structure. Add an input element of type "checkbox" with the id "switcher" within your HTML document. This checkbox will serve as the switch for toggling between light and dark modes. Make sure to wrap your website content inside a container, such as a <div>, with a class name, like “body,” as shown in the provided code.

<input type="checkbox" id="switcher">
<div class="body">
	<article>

		<h1>Hello World</h1>
		<p>Your content goes here.</p>
	</article>
</div>

2. Next, let’s style our website using CSS. Ensure your website takes up the full viewport height and remove any default margin by applying the styles to the “body” tag. Customize the padding for your content within the “article” element to suit your design preferences.

body {
  min-height: 100vh;
  margin: 0;
}

article {
  padding: 5em;
}

.body {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-height: 100vh;
  background: white;
  background-color: var(--bg-color, #fff);
  color: var(--color, #000);
  transition: 250ms;
}

#switcher {
  position: absolute;
  -webkit-appearance: none;
     -moz-appearance: none;
          appearance: none;
  top: 1rem;
  right: 1rem;
  width: 4em;
  height: 2em;
  background-color: var(--color, #000);
  border-radius: 2em;
  font-size: 20px;
}
#switcher::after {
  position: absolute;
  top: 0.25em;
  left: 0.25em;
  width: 1.5em;
  height: 1.5em;
  content: "";
  background-color: var(--bg-color, #fff);
  border-radius: 2em;
  transform: translateX(var(--translate, 0));
  transition: 250ms;
}
#switcher:checked {
  --translate: 2em;
  --bg-color: #424242;
  --color: #81d4fa;
}
#switcher:checked ~ * {
  --bg-color: #424242;
  --color: #81d4fa;
}

That’s all! hopefully, you have successfully integrated this easy Dark Mode using CSS. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X