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" />
  <link rel="icon" type="image/svg+xml" href="favicon.svg" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <!-- Google fonts y font-awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
    integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g=="
    crossorigin="anonymous" referrerpolicy="no-referrer" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,400;1,100&display=swap" rel="stylesheet">
  <!-- FIN Google fonts y font-awesome -->
  <link rel="stylesheet" href="style.css">
  <title>Dark light app</title>
</head>

<body>
  <header>
    <div class="header-container">
      <div></div>
      <ul>
        <li><a href="">Dark</a></li>
        <li><a href="">Light</a></li>
        <li><a href="">Mode</a></li>
      </ul>
      <div><i id="btn-theme" class="fa-solid fa-circle-half-stroke btn-theme"></i></div>
    </div>
  </header>
  <main>
    <section>

      <h1>Code Dark light mode</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi odit harum pariatur! Nulla exercitationem ut
        totam dignissimos dicta excepturi, a neque porro voluptatem voluptas expedita accusantium est architecto. Sed
        dignissimos facilis maxime nemo neque ipsam laborum? Officia repellat soluta amet, beatae enim sunt suscipit
        vero distinctio praesentium libero et eaque nostrum non delectus, assumenda culpa officiis consequatur explicabo
        quasi, consectetur quisquam similique unde inventore. Deserunt, similique ea. Blanditiis quia similique
        repudiandae ducimus corrupti cumque consequuntur voluptates amet. Explicabo quae pariatur a similique deserunt
        sit voluptas, dolore ea aperiam officia error ipsa ab eveniet non facere libero ex beatae fuga quidem.</p>

    </section>
  </main>
  <footer>

  </footer>
  <script type="module" src="/main.js"></script>
</body>

</html>
              
            
!

CSS

              
                /* RESET  */
*,
*::after,
*::before {
  box-sizing: border-box;
  list-style: none;
  margin: 0;
  padding: 0;

}

/* variables globales */
:root {
  --header-color: #f5f5f5;
  --header-back-color: rgb(84, 121, 241);
  --main-back-color: #f5f5f5;
  --main-color: #333;
  --header-container-max-width: 70rem;
}

.darkmode {
  --header-color: #b2b2b2;
  --header-back-color: rgb(31, 31, 31);
  --main-back-color: #595959;
  --main-color: rgb(235, 235, 235);
  --header-container-max-width: 70rem;
}


a,
a :link,
a :visited {
  text-decoration: none;
  color: var(--header-color);
}

body {
  font-family: 'Lato', sans-serif;
  background-color: var(--main-back-color);
  color: var(--main-color);
}

header {
  background-color: var(--header-back-color);
  color: var(--header-color);
  height: 3rem;
  width: 100%;
}

/* header y menu nada relacionado a dark light  */
section,
.header-container {
  max-width: var(--header-container-max-width);
  /* center */
  margin: 0 auto;
  height: 100%;
}

.header-container,
.header-container ul {
  display: flex;
  justify-content: space-between;
  height: 100%;
  align-items: center;
  margin: 0;
  padding: 0;
}

.header-container li {
  margin-right: 1em;
}

.header-container li:last-child {
  margin-right: 0;
}

.btn-theme {
  cursor: pointer;
  margin-right: 1em;
}
              
            
!

JS

              
                /*
* Cambia el color de la aplicacion desde el boton. 
*/

// btn btn-theme
const btnTheme = document.querySelector('#btn-theme');

btnTheme.addEventListener('click', () => {
  // 1. Check if dark mode is enabled
  localStorage.getItem('theme') === 'dark' ? disableDarkMode() : enableDarkMode();

})

// cambio de dark mode a ligh mode. 
const enableDarkMode = () => {
  document.body.classList.add('darkmode');
  // 2. Update darkMode in localStorage
  localStorage.setItem('theme', 'dark');
}
// cambio de light mode a dark mode.
const disableDarkMode = () => {
  // 1. Remove the class from the body
  document.body.classList.remove('darkmode');
  // 2. Update darkMode in localStorage 
  localStorage.setItem('theme', 'light');
}

/*
* Cambia el color de la aplicacion desde el sistema o config del navegador. 
*/

// Detectar el color del sistema
const detectColorScheme = () => {
  const localTheme = localStorage.getItem('theme');
  // Verificar que theme tiene el sistama/navegador 
  if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
    // dark mode
    (localTheme === 'dark' || localTheme === null) ? enableDarkMode() : disableDarkMode();
  } else {
    (localTheme === 'dark') ? enableDarkMode() : disableDarkMode();
  }
}


// Verifica si hay cambios en el thema y lo cambia
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
  event.matches ? enableDarkMode() : disableDarkMode();
});

// Detectar el color del sistema
detectColorScheme();
              
            
!
999px

Console