Welcome to a new era of dynamic and maintainable stylesheets! In this tutorial, you will learn two new features that will change the way you write CSS: CSS Variables (Custom Properties) and CSS Functions. We will also learn about CSS Filters, which make it easy to create effects on images and other elements in one line of code.
Now you may be wondering why these concepts will be a big benefit.
Once you are comfortable using these two principles and filters, you will write more readable, maintainable code, and build new, exciting visual effects that will set your websites apart from others. Let’s get started!
Let’s begin with our HTML foundation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Filters & Variables Demo</title>
<link rel="stylesheet" href="Variables(CustomProperties)_CSSFunctions.css">
</head>
<body>
<div class="card">
<h2>Custom Card</h2>
<p>Using CSS variables, functions, filters & shadows!</p>
</div>
<div>
<img src="../images/images.jpg" class="blur" width="90px" height="90px">
<img src="../images/images.jpg" class="brightness" width="90px" height="90px">
<img src="../images/images.jpg" class="contrast" width="90px" height="90px">
<img src="../images/images.jpg" class="img" width="90px" height="90px">
</div>
<br>
<div class="image">
<img src="../images/images.jpg" width="100%">
<div class="container">
<h1>Hello</h1>
</div>
</div>
</body>
</html>
What are we doing here?
Pro Tip: Notice that we used the same image multiple times with different classes? This is a great way to demonstrate how CSS can change the same element in different ways!
Here’s where the magic begins! Let’s declare our CSS variables:
:root {
--primary-color: #6a11cb;
--secondary-color: #2575fc;
--text-color: #333;
--bg-gradient: linear-gradient(135deg, #a8edea, #fed6e3);
--card-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);
--radius: 15px;
--transition: all 0.3s ease;
}
Understanding CSS Variables:
Why do we use :root?
Because once declared there, we will have global access to that variable – any element in your document can access that variable!
Beginner Note: Think of CSS variables like a “settings panel” at the top of your stylesheet. Do you want to change all of the colors? You will simply update those variables!
Let’s add a universal reset:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
What this does:
Note: box-sizing: border-box is a best practice to help prevent unexpected sizing surprises. Always include!
Now let’s apply our variables using the var() function:
body {
background: var(--bg-gradient);
font-family: "Poppins", sans-serif;
color: var(--text-color);
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
padding: 40px 20px;
}
The var() function explained:
Why this is powerful:
Want to change the background gradient on your whole site? Just change –bg-gradient in :root and you’re done. No find-and-replace!
Pro Tip: CSS variables are live! If you update them via JavaScript your entire page will update in an instant. Perfect for theme switchers!
Let’s build a card that uses multiple variables:
.card {
background: #fff;
border-radius: var(--radius);
padding: 40px 50px;
text-align: center;
box-shadow: var(--card-shadow);
margin-bottom: 40px;
transition: var(--transition);
max-width: 350px;
}
.card:hover {
transform: translateY(-10px) scale(1.05);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.25);
}
.card h2 {
color: var(--primary-color);
margin-bottom: 10px;
}
.card p {
font-size: 1rem;
color: #555;
}
To summarize:
Why does this matter:
If you ever want all of the cards to look the same, you only have to change the variables once! This is DRY (Don’t Repeat Yourself) programming at its best.
Now for the fun part – CSS filters! Let’s start with blur:
.blur {
filter: blur(3px);
transition: var(--transition);
}
.blur:hover {
filter: blur(0);
}
How filters function:
Creative application: Blur effects are fun for giving a sense of depth, drawing attention, or creating “reveal” interactions!
Let’s make images darker and lighter:
.brightness {
filter: brightness(0.5);
transition: var(--transition);
}
.brightness:hover {
filter: brightness(1.2);
}
Brightness overview:
Pro Tip: Use multiple filters! filter: brightness(0.8) contrast(1.2); applies both filters!
Now let’s adjust image contrast:
.contrast {
filter: contrast(200%);
transition: var(--transition);
}
.contrast:hover {
filter: contrast(100%);
}
In-depth contrast:
Note: high contrast can help images appear dramatic, attention grabbing and great for artistic look/feel!
Let’s create a grayscale-to-color effect:
.img {
filter: grayscale(100%);
border-radius: 10px;
transition: var(--transition);
}
.img:hover {
filter: grayscale(0);
transform: scale(1.1);
}
This is what is going on:
Creative application:
This for portfolio galleries – show images in grayscale and reveal color on hover for an eye-catching effect!
Let’s build a sophisticated image overlay:
.image {
position: relative;
width: 80%;
max-width: 700px;
border-radius: var(--radius);
overflow: hidden;
box-shadow: var(--card-shadow);
margin-top: 30px;
}
.image img {
display: block;
width: 100%;
height: auto;
filter: brightness(70%);
transition: var(--transition);
}
.image:hover img {
filter: brightness(100%);
transform: scale(1.03);
}
Understanding how this works:
As a design principle, darkening a background image will help readability of overlaid text!
Now let’s add text on top of the image:
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
text-align: center;
text-shadow: 2px 2px 10px rgba(0, 0, 0, 0.6);
}
.container h1 {
font-size: 3rem;
font-weight: bold;
letter-spacing: 2px;
animation: fadeIn 1.5s ease-in-out;
}
The trick of positioning:
The text effects:
Pro tip: The translate(-50%, -50%) centering technique works no matter the size of the centered element!
Let’s animate the text when it appears:
@keyframes fadeIn {
from {
opacity: 0;
transform: translate(-50%, -60%);
}
to {
opacity: 1;
transform: translate(-50%, -50%);
}
}
Animation breakdown:
Note: We use the translate(-50%, …) centering in both keyframes to keep the text center aligned horizontally!
Finally, let’s add mobile-friendly styles:
@media (max-width: 600px) {
.card {
width: 90%;
padding: 25px;
}
.image {
width: 95%;
}
.container h1 {
font-size: 2rem;
}
}
Responsive design:
Pro Tip: Make sure to always check your design on a mobile device! Over 50% of web traffic is mobile.
The Power of CSS Variables
This project showcases why CSS variables are important: we defined variables once in :root and referenced them several times:
What’s the advantage? Do you want softer shadows everywhere on the site? Change one line. You want a different border-radius? Change one variable! This is the definition of maintainability!
CSS Functions in Action
This project employs several CSS functions:
Filter Effects Pipeline
CSS filters can be combined together:
css
filter: brightness(0.8) contrast(1.2) blur(2px);
They work in order, and create complex visual effects from simple code!
Variable Scope
CSS variables respect cascade and inheritance:
A. Components / Keywords Used
Name | Description |
:root | Pseudo-class representing the document root, used to define global CSS variables |
–variable-name | Syntax for declaring custom CSS properties (variables) |
var() | Function to retrieve and use CSS variable values |
filter | Property that applies visual effects to elements |
blur() | Filter function that applies blur effect (in pixels) |
brightness() | Filter function that adjusts brightness (0-1+ or 0%-200%+) |
contrast() | Filter function that adjusts contrast (0-200%+) |
grayscale() | Filter function that converts image to grayscale (0-100%) |
rgba() | Function to create colors with alpha (transparency) channel |
linear-gradient() | Function that creates smooth color transitions |
translate() | Transform function that moves elements in 2D space |
translateY() | Transform function that moves elements vertically |
scale() | Transform function that resizes elements |
@keyframes | Defines custom animation sequences |
@media | Creates responsive styles based on screen size |
box-shadow | Creates shadow effects around elements |
text-shadow | Creates shadow effects behind text |
B. Key Properties / Parameters / Features
Name | Description |
position: relative | Creates positioning context for absolutely positioned children |
position: absolute | Removes element from normal flow for precise positioning |
overflow: hidden | Clips content that extends beyond element boundaries |
transform | Applies 2D or 3D transformations to elements |
transition | Creates smooth animations between property changes |
opacity | Controls transparency (0 = invisible, 1 = fully visible) |
letter-spacing | Controls spacing between characters |
border-radius | Rounds corners of elements |
box-sizing: border-box | Includes padding and border in element width calculation |
max-width | Sets maximum width constraint for responsive design |
top: 50%; left: 50% | Combined with translate(-50%, -50%) for perfect centering |
flex-direction: column | Stacks flex items vertically |
align-items: center | Centers items along cross axis |
justify-content | Aligns items along main axis |
ease-in-out | Timing function that starts slow, speeds up, then slows down |
Common Mistakes to Be Aware Of:
Remember to consider what CSS variables and filters can do! These are amazing time-saving tools that can help clean up your code! The only way to get great at these techniques is to practice!
Do you want to learn more about CSS variables, functions, and filters? Refer to these great resources:
Official Documentation:
Interactive Learning:
Tools and Generators:
Video Tutorials:
Practice and Inspiration:
Browser Compatibility:
Source code:
Today you learned that CSS Variables (Custom Properties) are incredibly helpful for creating reusable dynamic values for things like colors, spacing, fonts, and layouts that make your CSS easier to maintain.
You also learned about CSS Functions, such as calc(), var(), min(), max(), clamp(), rgba(), url(), and others that allow you to be responsive and flexible with cleaner code.
By the end of the session, you will also be able to write more scalable and customizable CSS using powerful variable-based styling.
CSS Variables can be utilized to store reusable values (such as colors, fonts, and spacing) that can change globally from one location.
You would declare a CSS Variable within a selector, using --name: value; and you would call the variable with the var(--name) function.
CSS Variables function in real time in the browser, are dynamic, can change with JavaScript, and provide a means to switch themes.
The most commonly used functions are calc(), var(), clamp(), min(), max(), rgba(), and url().
Yes. You can use a variable with the functions calc() or clamp() to provide reusable, scaleable, and responsive spacing, fonts, or layouts.
Copyright © 2025 Niotechone Software Solution Pvt. Ltd. All Rights Reserved.