variables, also called custom properties, allow you to store reusable values in a CSS file. They act like placeholders for values (e.g., colors, fonts, sizes) that you can reference throughout your stylesheet.
Key Features:
- Begin with a double hyphen (
--), e.g.,--main-color. - Can be defined globally (in the
:rootselector) or locally within specific selectors. - Accessed using the
var()function.
Advantages of CSS Variables**
- Reusability: Define a value once and reuse it across the stylesheet.
- Maintainability: Update the variable to instantly reflect changes wherever it is used.
- Dynamic Updates: CSS variables can be manipulated via JavaScript to create dynamic themes.
Example: CSS Variables in Action
HTML File (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Variables Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
<main>
<p>This is an example of using CSS variables to style a web page.</p>
<button>Click Me</button>
</main>
</body>
</html>
CSS File (styles.css)
/* Global variables defined in :root */
:root {
--primary-color: #4CAF50;
--secondary-color: white;
--text-color: #333;
--font-size: 16px;
--button-radius: 8px;
}
/* Apply variables */
body {
font-family: Arial, sans-serif;
font-size: var(--font-size);
background-color: var(--secondary-color);
color: var(--text-color);
margin: 0;
padding: 0;
}
header {
background-color: var(--primary-color);
color: var(--secondary-color);
text-align: center;
padding: 1em;
}
button {
background-color: var(--primary-color);
color: var(--secondary-color);
border: none;
padding: 10px 20px;
border-radius: var(--button-radius);
cursor: pointer;
font-size: var(--font-size);
}
button:hover {
background-color: darkgreen;
}
Explanation of the Example
-
Defining Variables:
- Global variables like
--primary-colorand--font-sizeare defined in the:rootselector, making them available throughout the stylesheet.
- Global variables like
-
Using Variables:
- The
var()function is used to insert variable values into CSS properties. For example:color: var(--primary-color);
- The
-
Result:
- The
buttonandheadershare the same--primary-color, ensuring consistency. Any changes to--primary-colorautomatically apply to both.
- The