by PaulB_GD · 10y ago · 0 comments
The point of variables is to allow the setting of one style property and applying it to others, right? So why not just to that at the top level element and inherit it down? Typically you only want to select a handful of colors for a web site / application so simply set those at the highest level possible then allow CSS inheritance to work.
Out of all of the big projects I've been on only one has used CSS preprocessors and I've never run into an issue with needing to change a value in too many places. It doesn't seem all that hard to avoid if I'm being honest here it just really, really depends on the structure of your CSS and HTML.
So first off if you want it to literally be the same then you could do something like:
h1, button {
color: #343434;
}
That would make them the same. You could also do .primaryColor {
color: #343434;
}
Then set the elements which should have a primary color with that class.But if you could break my first example apart and set it in two places just as easily. But that's setting it at a high level element itself. You could even go crazier and set EVERYTHING to that color but ultimately many of these decisions have to be context based to decide.
The way I see it, CSS preprocessors allow for code reuse via composition - vanilla CSS restricts you to inheritance.
But now your markup is littered with styling classes, and you could end up with a .secondary button with a .primaryColor class.
In vanilla css, the only way to really define something like a primary color just once and have it apply correctly to all elements is to define it at a high level in a deeply nested cascade. This of course all breaks as soon as you add a container element and have to go back and add another level of nesting to your css.
Variables in css should be standard.
I hate, hate, hate having to alter my HTML to apply styles. The idea of
<h1 class="primaryColor">
totally creeps me out. I do it because other people refuse to use preprocessors and think that altering the HTML to include presentational details is fine (see Bootstrap.)
But it still feels gross to me.
Have you never A/B tested a site and found that varying colours made a difference?
Have you never developed a site with some colour scheme and later found you needed to extend the colour scheme, perhaps to incorporate a new product or service being added to the available range?
Having a colour scheme that is set in stone early on sounds like one of those idealised things that sometimes just doesn't work in the face of reality.
I'm currently building something that has mild themes for fonts, colors and some margins (and is build on top of a SCSS framework, but that wasn't my choice), without color variables it would be a pain...
It can be a copy-paste tool on the internet or whatever, no preprocessor or build tools required. You work directly with styles.css3 in the browser and when you're done you transform it to old style without variables, so it works in older browsers.
That's what I wanted to do last week but as I couldn't find such a tool, at the end I did a search/replace. It was a small project obviously.
And the OP uses PostCSS. Apparently, a lot of people here didn't read that part...
So...a preprocessor? :)