Unlocking the Power of Custom CSS Properties (Variables) for Dynamic and Reusable Styles

Techie     May 2024

Definition

CSS (Cascading Style Sheets) is an essential tool for web developers to control the presentation and layout of web pages. It allows developers to style HTML elements, making websites visually appealing and user-friendly. While CSS provides a powerful set of predefined properties, custom CSS properties, also known as CSS variables, take this power to the next level by offering dynamic and reusable styles. In this section, we’ll delve into the benefits and use cases of custom CSS properties, and we’ll explore examples of theming and dynamic UI updates.

Benefits of Custom CSS Properties (Variables)


Using Custom CSS Properties

Let’s dive into some practical examples to demonstrate how to use custom CSS properties for theming and dynamic UI updates.

Theming with Custom CSS Properties

/* Define your theme variables */
:root {
  --primary-color: #3498db;
  --secondary-color: #e74c3c;
  --font-size: 16px;
}

/* Apply the theme to elements */
.button {
  background-color: var(--primary-color);
  color: white;
  font-size: var(--font-size);
  padding: 10px 20px;
}

/* Switching to a different theme */
.dark-mode {
  --primary-color: #2c3e50;
  --secondary-color: #f39c12;
  /* ... additional theme changes ... */
}

In this example, we define theme variables using the :root pseudo-class. These variables are then used to style the .button element. To switch to a different theme, simply add the dark-mode class to the root element and update the variable values accordingly.


Dynamic UI Updates with Custom CSS Properties

<!DOCTYPE html>
<html>
<head>
<style>
/* Define a dynamic property */
:root {
  --bg-color: #f1f1f1;
}

/* Apply the property to an element */
.section {
  background-color: var(--bg-color);
  padding: 20px;
}
</style>
</head>
<body>

<div class="section">
  <p>This is a section with a dynamic background color.</p>
</div>

<input type="color" id="color-picker">
<script>
// Update the dynamic property based on user input
document.getElementById("color-picker").addEventListener("input", function(event) {
  document.documentElement.style.setProperty("--bg-color", event.target.value);
});
</script>

</body>
</html>

In this example, we define a custom CSS property –bg-color that controls the background color of the .section element. We also include an input type=”color” element, which allows users to pick a color. The JavaScript code listens for changes to the color picker and dynamically updates the –bg-color property, resulting in a live preview of the color change in the UI.


Conclusion

Custom CSS properties (variables) are a game-changer in modern web development. They promote reusability, enable dynamic theming, ease maintenance, and allow for dynamic UI updates. By incorporating custom CSS properties into your projects, you’ll enhance your styling workflow and create more flexible and interactive web experiences. Embrace the power of custom CSS properties and take your web design skills to the next level.


Thanks for reading, see you in the next one!