I just lately wrote a really fundamental Sass loop that outputs a number of padding and margin utility courses. Nothing fancy, actually, only a Sass map with 11 spacing values, looped over to create courses for each padding and margin on either side. As we’ll see, this works, but it surely finally ends up a fairly hefty quantity of CSS. We’re going to refactor it to make use of CSS customized properties and make the system rather more trim.
This very a lot works. It outputs all of the utility courses we’d like. However, it may possibly additionally get bloated shortly. In my case, they had been about 8.6kb uncompressed and underneath 1kb compressed. (Brotli was 542 bytes, and gzip got here in at 925 bytes.)
Since they’re extraordinarily repetitive, they compress properly, however I nonetheless couldn’t shake the sensation that each one these courses had been overkill. Plus, I hadn’t even completed any small/medium/massive breakpoints that are pretty typical for these sorts of helper courses.
Right here’s a contrived instance of what the responsive model would possibly appear to be with small/medium/massive courses added. We’ll re-use the $space-stops map outlined beforehand and throw our repetitious code right into a mixin
That clocks in at about 41.7kb uncompressed (and about 1kb with Brotli, and 3kb with gzip). It nonetheless compresses properly, but it surely’s a bit ridiculous.
I knew it was doable to reference data-* attributes from inside CSS utilizing the [attr() function, so I wondered if it was possible to use calc() and attr() together to create dynamically-calculated spacing utility helpers via data-* attributes — like data-m=”1″ or data-m=”1@md” — then in the CSS to do something like margin: calc(attr(data-m) * 0.25rem) (assuming I’m using a spacing scale incrementing at 0.25rem intervals). That could be very powerful.
But the end of that story is: no, you (currently) can’t use attr() with any property except the content property. Bummer. But in searching for attr() and calc() information, I found this intriguing Stack Overflow comment by Simon Rigét that suggests setting a CSS variable directly within an inline style attribute. Aha!
So it’s possible to do something like <div style=”–p: 4;”> then, in CSS:
It is a lot like the primary Sass loop above, however there’s no loop going 11 occasions — and but it’s infinite. It’s about 1.4kb uncompressed, 226 bytes with Brotli, or 284 bytes gzipped.
In case you wished to increase this for breakpoints, the unlucky information is which you can’t put the “@” character in CSS variable names (though emojis and different UTF-8 characters are unusually permitted). So you can most likely arrange variable names like p_sm or sm_p. You’d have so as to add some further CSS variables and a few media queries to deal with all this, but it surely received’t blow up exponentially the way in which conventional CSS classnames created with a Sass for-loop do.
Right here’s the equal responsive model. We’ll use a Sass mixin once more to chop down the repetition:
That’s about 6.1kb uncompressed, 428 bytes with Brotli, and 563 with gzip.
Do I feel that writing HTML like <div model=”–px:2; –my:4;”> is agreeable to the attention, or good developer ergonomics… no, not significantly. However may this strategy be viable in conditions the place you (for some cause) want extraordinarily minimal CSS, or maybe no exterior CSS file in any respect? Sure, I certain do.
It’s value declaring right here that CSS variables assigned in inline kinds don’t leak out. They’re scoped solely to the present aspect and don’t change the worth of the variable globally. Thank goodness! The one oddity I’ve discovered up to now is that DevTools (no less than in Chrome, Firefox, and Safari) don’t report the kinds utilizing this method within the “Computed” kinds tab.
Additionally value mentioning is that I’ve used good previous padding and margin properties with -top, -right, -bottom, and -left, however you can use the equal logical properties like padding-block and padding-inline. It’s even doable to shave off just some extra bytes by selectively mixing and matching logical properties with conventional properties. I managed to get it right down to 400 bytes with Brotli and 521 with gzip this fashion.
Different use circumstances
This appears most applicable for issues which might be on a (linear) incremental scale (which is why padding and margin looks like an excellent use case) however I may see this probably working for widths and heights in grid techniques (column numbers and/or widths). Perhaps for typographic scales (however perhaps not).
I’ve centered so much on file measurement, however there could also be another makes use of right here I’m not pondering of. Maybe you wouldn’t write your code on this method, however a important CSS software may probably refactor the code to make use of this strategy.
At Marketing Solution Australia we strive to deliverer elegant responsive websites for your business integrated with our personal SEO Optimization package to bring your pages on the first page of Google.