Menu
Coddy logo textTech

Fondamenti della modalità scura/chiara

Fa parte della sezione Frontend pratico del percorso HTML di Coddy. Lezione 13 di 35.

La modalità scura/chiara è una funzionalità molto apprezzata che permette agli utenti di scegliere la combinazione di colori che preferiscono. Implementiamo un semplice interruttore per la modalità scura/chiara usando variabili CSS.

Per prima cosa, definisci le variabili dei colori della modalità chiara nell’elemento root e applicale ai diversi elementi:

:root {
  --background-color: #ffffff;
  --text-color: #333333;
  --heading-color: #000000;
}
body {
  background-color: var(--background-color);
  color: var(--text-color);
}
h1, h2, h3 {
  color: var(--heading-color);
}

Ora crea una classe per la modalità scura che sovrascriverà queste variabili:

.dark-mode {
  --background-color: #333333;
  --text-color: #f5f5f5;
  --heading-color: #ffffff;
}

Ora, la classe .dark-mode controlla il tema: quando questa classe viene aggiunta a <body>, le variabili cambiano e tutti gli elementi che le utilizzano si aggiornano automaticamente.

<body class="dark-mode">
<!-- il contenuto della tua pagina qui -->
</body>

Per cambiare modalità dinamicamente, abbiamo bisogno di un piccolo aiuto da un altro linguaggio chiamato JavaScript (JS) per aggiungere o rimuovere la classe .dark-mode. In questa lezione, puoi provarlo manualmente aggiungendo o rimuovendo la classe sul tag <body>.

challenge icon

Sfida

Facile

Il tuo compito:

  1. Aggiungi una classe .dark-mode nella sezione <style>.
  2. Sovrascrivi le variabili CSS per impostare colori scuri per lo sfondo, il testo, le intestazioni e i pulsanti.
  3. Provala aggiungendo class="dark-mode" al tag <body>.

Provalo tu

<!DOCTYPE html>
<html>
<head>
  <title>Dark/Light Mode Basics</title>
  <style>
    /* Default light theme */
    :root {
      --background-color: #f0f4f8;
      --text-color: #222222;
      --heading-color: #003366;
      --button-bg: #ffcc00;
      --button-text: #000000;
    }

    body {
      background-color: var(--background-color);
      color: var(--text-color);
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }

    header {
      background-color: var(--heading-color);
      color: white;
      padding: 2rem;
      text-align: center;
    }

    header h1 {
      margin: 0;
      font-size: 2rem;
    }

    main {
      padding: 2rem;
    }

    h2 {
      color: var(--heading-color);
      margin-top: 1.5rem;
    }

    p {
      line-height: 1.6;
    }

    .button {
      display: inline-block;
      margin-top: 1rem;
      padding: 0.75rem 1.5rem;
      background-color: var(--button-bg);
      color: var(--button-text);
      border: none;
      border-radius: 5px;
      text-decoration: none;
      font-weight: bold;
      cursor: pointer;
    }

    footer {
      background-color: var(--heading-color);
      color: white;
      text-align: center;
      padding: 1rem;
      margin-top: 2rem;
    }
  </style>
</head>
<body>
  <header>
    <h1>Olympic Games 2025</h1>
    <p>Celebrating sports, unity, and excellence</p>
  </header>

  <main>
    <h2>Top Sports</h2>
    <p>From track and field to swimming and gymnastics, the Olympics showcase the best athletes in the world.</p>
    <a href="#" class="button">See Medal Winners</a>

    <h2>Host Cities</h2>
    <p>Explore the exciting host cities where athletes come together to compete at the highest level.</p>
    <a href="#" class="button">Learn More</a>

    <h2>History</h2>
    <p>The Olympic Games have a rich history dating back to ancient Greece, promoting peace, friendship, and fair competition.</p>
  </main>

  <footer>
    &copy; 2025 Olympic Games. All rights reserved.
  </footer>
</body>
</html>
quiz iconMettiti alla prova

Questa lezione include un breve quiz. Inizia la lezione per rispondere e tenere traccia dei tuoi progressi.

Tutte le lezioni di Frontend pratico

Esercitati da solo: Playground Web