Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-03-26-experimental· MetaHugging Face LMArena Human Preference 140K

Day 14: CSS Variables & Functions Topics: CSS custom properties (--variable-name)…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Mistralmistral-medium-2505’s answer.
  1. User

    Day 14: CSS Variables & Functions Topics: CSS custom properties (--variable-name) calc(), min(), max(), clamp() Theme switching concept Assignment: Use CSS variables to define a light/dark theme. Add a toggle (checkbox) to switch between themes using only CSS.

  2. Meta Assistant

    Day 14: CSS Variables & Functions

    CSS Custom Properties (Variables)

    CSS custom properties, also known as CSS variables, allow you to store values in a variable and reuse them throughout your stylesheet. They are defined using the -- prefix followed by the variable name.

    Syntax:

    :root {
      --variable-name: value;
    }
    
    • :root is a pseudo-class that targets the root element of the document (usually <html>).
    • --variable-name is the custom property name (must start with --).
    • value is the value assigned to the variable.

    Example:

    :root {
      --primary-color: #3498db;
      --font-size: 16px;
    }
    
    body {
      background-color: var(--primary-color);
      font-size: var(--font-size);
    }
    

    In the above example, --primary-color and --font-size are CSS variables. The var() function is used to access their values.

    CSS Functions: calc(), min(), max(), clamp()

    CSS functions allow you to perform calculations and operations on values.

    • calc(): Performs mathematical calculations.

    width: calc(100% - 20px); /* subtract 20px from 100% width */

    *   **`min()`**: Returns the smallest value from a list of values.
        ```css
    width: min(500px, 100%); /* width will be 500px or 100%, whichever is smaller */
    
    • max(): Returns the largest value from a list of values.

    width: max(300px, 50%); /* width will be 300px or 50%, whichever is larger */

    *   **`clamp()`**: Clamps a value between a minimum and maximum value.
        ```css
    font-size: clamp(1rem, 2vw, 2rem); /* font-size will be between 1rem and 2rem, and 2vw */
    

    Theme Switching Concept

    We'll create a simple theme switcher using CSS variables and a checkbox. The idea is to:

    1. Define two themes (light and dark) using CSS variables.
    2. Store the theme preference in a CSS custom property.
    3. Update the theme preference when the checkbox is toggled.
    4. Use the theme preference to apply the corresponding styles.

    Assignment Solution: Light/Dark Theme Switcher

    HTML:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Theme Switcher</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <input type="checkbox" id="theme-toggle" class="theme-toggle">
        <label for="theme-toggle" class="theme-toggle-label">Switch Theme</label>
        <h1>Welcome to the Theme Switcher!</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sit amet nulla auctor, vestibulum magna sed, convallis ex.</p>
    </body>
    </html>
    

    CSS (in style.css file):

    /* Define themes using CSS variables */
    :root {
      --light-bg: #f9f9f9;
      --light-text: #333;
      --light-primary: #3498db;
    
      --dark-bg: #2c3e50;
      --dark-text: #ecf0f1;
      --dark-primary: #9b59b6;
    
      /* Default theme (light) */
      --bg: var(--light-bg);
      --text: var(--light-text);
      --primary: var(--light-primary);
    }
    
    /* Dark theme */
    :root:has(.theme-toggle:checked) {
      --bg: var(--dark-bg);
      --text: var(--dark-text);
      --primary: var(--dark-primary);
    }
    
    body {
      background-color: var(--bg);
      color: var(--text);
      font-family: Arial, sans-serif;
      padding: 20px;
      transition: background-color 0.3s, color 0.3s;
    }
    
    .theme-toggle {
      display: none;
    }
    
    .theme-toggle-label {
      background-color: var(--primary);
      color: #fff;
      padding: 10px 20px;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s;
    }
    
    .theme-toggle-label:hover {
      opacity: 0.9;
    }
    
    h1 {
      color: var(--primary);
      margin-bottom: 10px;
    }
    
    /* Optional: Style the checkbox label based on the theme */
    .theme-toggle:checked + .theme-toggle-label {
      background-color: var(--dark-primary);
    }
    

    How it works:

    1. We define two sets of CSS variables for light and dark themes in :root.
    2. We create a default theme (light) by assigning --bg, --text, and --primary variables to their light counterparts.
    3. The :root:has(.theme-toggle:checked) pseudo-class checks if the .theme-toggle checkbox is checked. If true, it updates the --bg, --text, and --primary variables to their dark counterparts.
    4. We use the var() function to access the theme variables (--bg, --text, --primary) in our styles.
    5. When the checkbox is toggled, the theme changes seamlessly due to the CSS variable updates and the transition property.

    Tips and Variations:

    • You can add more themes by defining additional variable sets and updating the :root pseudo-class conditions.
    • Store the user's theme preference in localStorage or a cookie to maintain the theme across page reloads.
    • Use prefers-color-scheme media query to respect the user's system theme preference.
    • Experiment with different transition effects and timing functions for a more polished experience.

    Run the above HTML and CSS code in a browser to see the theme switcher in action!

    Happy coding!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.