The other questions explained custom properties and CSS variables but what if we make it simple and say that custom properties are simply here to allow us to add more properties in addition to the CSS default ones. Of course, a custom property alone is useless as the browser will do nothing with it and that's why in 99% of the cases they are used with CSS variables.
The other fact is that when doing CSS we will intuitively think about using default CSS properties in order to do what we want and when we face a complex situation we will try to combine a lot of properties, pseudo-element, etc in order to achieve our needs. But what if we think differently and we start using custom properties for such purpose? What if instead of complex CSS code we simply write a custom property and implement a JS/jQuery code that will do the job for us.
Let's take a simple and common example. You have an element that you want to make absolute position in order to use it as an overlay of his parent element. With CSS we will do something like this:
.block {
height:100px;
width:100px;
margin:20px;
border:1px solid red;
position:relative; /* we usually forget this !*/
}
.overlay {
position:absolute;
top:0;
right:0;
left:0; /*sometimes we use width:100% but it won't work with padding!*/
bottom:0;
padding:5px;
z-index:1; /* we can forget this sometimes*/
background:rgba(0,0,0,0.5);
}
<div class="block">
<span class="overlay"></span>
some text here
</div>
This is pretty easy and we can use the same class everywhere if we want to have the same thing. But we can consider a custom property that will reduce the CSS code and make it easier:
$('*').each(function() {
if ($.trim($(this).css('--overlay')) === "top") {
$(this).css({
'position': 'absolute',
'top': 0,
'bottom': 0,
'left': 0,
'right': 0,
'z-index':2
});
$(this).parent().css('position','relative');
} else {
//we test the other values and we do the necessary changes
}
})
.block {
height: 100px;
width: 100px;
margin: 20px;
border: 1px solid red;
}
.overlay {
--overlay: top;
background: rgba(0, 0, 0, 0.5);
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<div class="block">
<span class="overlay"></span>
some text here
</div>
I know that the first thought is "Why bother using all this code for such simple thing that we can do with CSS?" and "What if the property changes? What if the parent has a position already set?" It's true, but this is a very simplified example. Imagine you build a JS or jQuery library where your provide many custom properties for complex stuffs (create common CSS shapes, do complex transformation where it's hard to calculate the values, etc) and you provide a documentation for each one with their respective values.
It's like a JS/jQuery library that tells you to add a class to your element and call one function and then you have a responsive slider, an interactive map, a twitter widget, etc. Why not doing the same with custom properties? We ask people to include the library and simply write CSS, then see the magic.
We can also consider this like an upgrade by adding more useful properties in order to make things easier. Exactly like the CSS makers do BUT we do it by our self and don't wait until there is a new specification and until it's approved.
Let's take the example of flexbox. Before flexbox, if we want to have N numbers of div inside a container having exactly the same size and filling all the container we are obliged to write some complex code that we have to adjust each time we add a new element. Now with flexbox, we simply set flex:1
and that's it! The browser will do all the complicated stuff for us.
We can do exactly the same. We create our custom properties to make things easier and we share it to the community. People will start using them and may find them useful then they can become a reference for common problems. Maybe some browser will even think about an implementation to integrate them.
So custom properties can also be a way to contribute to the enhancement of CSS.