Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!--==================== CSS ==========-->
    <link
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css"
      rel="stylesheet"
    />

    <link rel="stylesheet" href="assets/css/styles.css" />

    <title>Implementing Dark Theme</title>
  </head>
  <body>
    <!--==================== HEADER ====================-->
    <header class="header" id="header">
      <nav class="nav container">
        <a href="#" id="mode-text">Light Mode</a>

        <div class="nav__menu" id="nav-menu">
          <ul class="nav__list grid">
            <li class="nav__item">
              <a href="#introduction" class="nav__link"> Introduction </a>
            </li>

            <li class="nav__item">
              <a href="#feature-one" class="nav__link"> Feature 1 </a>
            </li>

            <li class="nav__item">
              <a href="#feature-two" class="nav__link"> Feature 2 </a>
            </li>
          </ul>
        </div>
        <!--==================== Dark Mode Toggle Button ====================-->
        <div>
          <input type="checkbox" class="checkbox" id="toggleBtn" />
          <label class="switch" for="toggleBtn">
            <i class="fas fa-moon"></i>
            <i class="fas fa-sun"></i>
            <div class="ball"></div>
          </label>
        </div>
        <!--==================== End====================-->
      </nav>
    </header>
    <main class="main">
      <section class="section" id="introduction">
        <h2 class="section__title">Introduction</h2>
        <span class="section__subtitle">Subtitle</span>

        <div class="container grid">
          <!-- <img src="assets/img/about.jpg" alt="" class="about__img" /> -->

          <div>
            <p>
              This webpage is for the demo purpose of how we can easily
              implement dark mode to impress users.
            </p>
          </div>
        </div>
      </section>
      <section class="section" id="feature-one">
        <h2 class="section__title">Feature 1</h2>
        <span class="section__subtitle">Feature 1</span>

        <div class="container grid">
          <div>
            <p>
              When the dark theme is activated, not only background color is
              changed, but also the text and hover color. Simply create a dark
              theme class for body tag.
            </p>
          </div>
        </div>
      </section>
      <section class="section" id="feature-two">
        <h2 class="section__title">Feature 1</h2>
        <span class="section__subtitle">Feature 2</span>

        <div class="container grid">
          <div>
            <p>
              We can store the user's theme preference in local storage for the
              next visit. Try activating the dark theme and refresh this page.
            </p>
          </div>
        </div>
      </section>
    </main>
    <footer class="footer">
      <div class="footer__bg">
        <div class="footer__container container grid">
          <div>
            <h1 class="footer__title">Footer</h1>
            <span class="footer__subtitle">Subtitle</span>
          </div>

          <ul class="footer__links">
            <li>
              <a href="#" class="footer__link">Item 1</a>
            </li>
            <li>
              <a href="#" class="footer__link">Item 2</a>
            </li>
            <li>
              <a href="#" class="footer__link">Item 3</a>
            </li>
          </ul>
        </div>

        <p class="footer__copy">Demo purpose</p>
      </div>
    </footer>
    <!--==================== MAIN JS ====================-->
    <script src="assets/js/main.js"></script>
  </body>
</html>

              
            
!

CSS

              
                /*========== GOOGLE FONTS ==========*/
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap");
/*==================== VARIABLES CSS ====================*/
:root {
  /*========== Colors ==========*/
  /* Change favorite color */
  --hue-color: 230;
  /* HSL color mode */
  --primary-color: hsl(var(--hue-color), 69%, 61%);
  --primary-color-second: hsl(var(--hue-color), 69%, 61%);
  --primary-color-lighter: hsl(var(--hue-color), 92%, 85%);
  --text-color: hsl(var(--hue-color), 8%, 45%);
  --text-color-light: hsl(var(--hue-color), 8%, 65%);
  --body-color: hsl(var(--hue-color), 60%, 99%);
  --header-height: 3rem;
}

/*========== Variables Dark theme ==========*/
body.dark-theme {
  /* HSL color mode */
  --primary-color-second: hsl(var(--hue-color), 30%, 8%);
  --text-color: hsl(var(--hue-color), 8%, 75%);
  --input-color: hsl(var(--hue-color), 29%, 16%);
  --body-color: hsl(var(--hue-color), 28%, 12%);
}

/*==================== BASE ====================*/
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0 0 var(--header-height) 0;
  font-family: "Poppins", sans-serif;
  font-size: 0.94rem;
  background-color: var(--body-color);
  color: var(--text-color);
}

h1,
h2,
h3,
h4 {
  color: var(--title-color);
  font-weight: 600;
}

ul {
  list-style: none;
}

a {
  text-decoration: none;
}

img {
  max-width: 100%;
  height: auto;
}

/*==================== REUSABLE CSS CLASSES ====================*/
.section {
  padding: 6rem 0 2rem;
}

.section__title {
  font-size: 1.5rem;
  color: var(--title-color);
  color: var(--primary-color);
}

.section__subtitle {
  display: block;
  font-size: 0.8rem;
  margin-bottom: 3rem;
}

.section__title,
.section__subtitle {
  text-align: center;
}

/*==================== LAYOUT ====================*/
.container {
  max-width: 768px;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}

.grid {
  display: grid;
  gap: 1.5rem;
}

.header {
  width: 100%;
  padding: 0 1rem;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 100;
  background-color: var(--body-color);
  height: max-content;
}

/*==================== NAV ====================*/
.nav {
  max-width: 968px;
  height: calc(var(--header-height) + 1.5rem);
  column-gap: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#mode-text,
.nav__toggle {
  color: var(--title-color);
  font-weight: 500;
}

#mode-text:hover {
  color: var(--primary-color);
}

.nav__toggle {
  font-size: 1.1rem;
  cursor: pointer;
}

.nav__toggle:hover {
  color: var(--primary-color);
}

.nav__menu {
  margin-left: auto;
}

.nav__list {
  grid-template-columns: repeat(3, 1fr);
  gap: 2rem;
  display: flex;
  column-gap: 2rem;
}

.nav__link {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 0.8rem;
  color: var(--title-color);
  font-weight: 500;
}

.nav__link:hover {
  color: var(--primary-color);
}

/*==================== FOOTER ====================*/
.footer {
  position: absolute;
  bottom: 0;
  height: 100px;
  width: 100%;
  padding-top: 2rem;
}

.footer__container {
  row-gap: 3.5rem;
}

.footer__bg {
  background-color: var(--primary-color-second);
  padding: 2rem 0 3rem;
}

.footer__title {
  font-size: 1.5rem;
  margin-bottom: 0.25rem;
}

.footer__subtitle {
  font-size: 0.8rem;
}

.footer__links {
  display: flex;
  flex-direction: column;
  row-gap: 1.5rem;
}

.footer__link:hover {
  color: var(--primary-color-lighter);
}

.footer__copy {
  font-size: 0.75rem;
  text-align: center;
  color: var(--text-color-light);
  margin-top: 3rem;
}

.footer__title,
.footer__subtitle,
.footer__link,
.footer__social {
  color: #fff;
}

/*==================== Dark Light Button Implementation ====================*/
.checkbox {
  display: none;
}

.switch {
  background-color: #111;
  border-radius: 50px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px;
  position: relative;
  height: 26px;
  width: 50px;
  /* Scale the size to your need */
  transform: scale(0.7);
}

.switch .ball {
  background-color: #fff;
  border-radius: 50%;
  position: absolute;
  top: 2px;
  left: 2px;
  height: 22px;
  width: 22px;
  transform: translateX(0px);
  transition: transform 0.2s linear;
}

.checkbox:checked + .switch .ball {
  transform: translateX(24px);
}

.fa-moon {
  color: #f1c40f;
}

.fa-sun {
  color: #f39c12;
}

              
            
!

JS

              
                // CSS class name for dark theme
const darkTheme = "dark-theme";

const darkThemeSetUp = () => {
  if (getCurrentTheme() === "dark") {
    document.getElementById("toggleBtn").checked = true;
    document.getElementById("mode-text").textContent = "Dark Mode";
  } else {
    document.getElementById("toggleBtn").checked = false;
    document.getElementById("mode-text").textContent = "Light Mode";
  }
};

const getCurrentTheme = () =>
  document.body.classList.contains(darkTheme) ? "dark" : "light";

//   Get user's theme preference from local storage
const selectedTheme = localStorage.getItem("selected-theme");
if (selectedTheme === "dark") {
  document.body.classList[selectedTheme === "dark" ? "add" : "remove"](
    darkTheme
  );
  darkThemeSetUp();
}

const themeButton = document.getElementById("toggleBtn");
themeButton.addEventListener("change", () => {
  document.body.classList.toggle(darkTheme);
  localStorage.setItem("selected-theme", getCurrentTheme());
  darkThemeSetUp();
});

              
            
!
999px

Console