CSS theme using variables allows for easy customization and maintainability

Here's a simple example of how you can set up a theme using CSS variables:

22

Arun Kr.
07-Feb-25
/* Define CSS Variables for the theme */
:root {
    --primary-color: #3498db;
    --secondary-color: #2ecc71;
    --background-color: #f4f4f4;
    --text-color: #333;
    --font-family: 'Arial', sans-serif;
    --font-size: 16px;
}

/* Apply the theme to your page */
body {
    background-color: var(--background-color);
    color: var(--text-color);
    font-family: var(--font-family);
    font-size: var(--font-size);
    margin: 0;
    padding: 0;
}

header {
    background-color: var(--primary-color);
    color: white;
    padding: 10px 0;
    text-align: center;
}

button {
    background-color: var(--secondary-color);
    color: white;
    border: none;
    padding: 10px 20px;
    cursor: pointer;
}

button:hover {
    background-color: darken(var(--secondary-color), 10%);
}

h1 {
    color: var(--primary-color);
    text-align: center;
    margin-top: 20px;
}

footer {
    background-color: var(--primary-color);
    color: white;
    text-align: center;
    padding: 10px;
    position: fixed;
    bottom: 0;
    width: 100%;
}

 

Dynamic Theme Changes (Optional):

To switch themes dynamically using JavaScript, you can update the root variables like this:

// JavaScript to change the theme dynamically
function changeTheme() {
    document.documentElement.style.setProperty('--primary-color', '#e74c3c');
    document.documentElement.style.setProperty('--secondary-color', '#f39c12');
}

 

@Since 2024 Arun'Log Powered by Arun Git