Day 14: CSS Variables & Functions Topics: CSS custom properties (--variable-name)…
- 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.
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; }:rootis a pseudo-class that targets the root element of the document (usually<html>).--variable-nameis the custom property name (must start with--).valueis 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-colorand--font-sizeare CSS variables. Thevar()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:
- Define two themes (light and dark) using CSS variables.
- Store the theme preference in a CSS custom property.
- Update the theme preference when the checkbox is toggled.
- 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:
- We define two sets of CSS variables for light and dark themes in
:root. - We create a default theme (light) by assigning
--bg,--text, and--primaryvariables to their light counterparts. - The
:root:has(.theme-toggle:checked)pseudo-class checks if the.theme-togglecheckbox is checked. If true, it updates the--bg,--text, and--primaryvariables to their dark counterparts. - We use the
var()function to access the theme variables (--bg,--text,--primary) in our styles. - When the checkbox is toggled, the theme changes seamlessly due to the CSS variable updates and the
transitionproperty.
Tips and Variations:
- You can add more themes by defining additional variable sets and updating the
:rootpseudo-class conditions. - Store the user's theme preference in
localStorageor a cookie to maintain the theme across page reloads. - Use
prefers-color-schememedia 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!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.