6 ms·
The - -var: ; hack to toggle multiple values with one custom property
- dang 6y agoUrl changed from http://feedproxy.google.com/~r/leaverou/~3/HD-MDYAgQ8w/ http://feedproxy.google.com/~r/leaverou/~3/HD-MDYAgQ8w/, which redirects to this. That domain is banned on HN so please do the macroexpansion before submitting!
- chrismorgan 6y agoCould you please also fix the title? `– -var:;` should be `--var: ;`.
- dang 6y agoAh yes. Fixed now. Thanks!
- chrismorgan 6y agoStill needs to be --, not - -.
- moonchild 6y agoRecommend adding 'in CSS' or similar to the title; currently lacks context.
- bryanrasmussen 6y agoI had a strong suspicion it was going to be CSS, but I think you're right.
- baq 6y agofor some reason i thought this was yaml.
- Freak_NL 6y agoAlso please fix the title. Someone used a computer that automatically turns -- into – (en-dash), and the space between the colon and semi-colon got dropped. The --var: ; hack
- 0xdeadb00f 6y agoFunny now the one time a title change would be actually useful the HN mods haven't changed it.
- dang 6y agohttps://hn.algolia.com/?dateRange=all&page=0&prefix=true&sort=byDate&type=comment&query=%22work%20a%20little%22%20by:dang https://hn.algolia.com/?dateRange=all&page=0&prefix=true&sor...
- Smithalicious 6y agoIt's not a bug, it's a feature!
- Theodores 6y agoThis is a very clever hack, I had no idea that initial meant that the fallback would be used. I am all for clever hacks but more needs to be done when it comes to explaining to experienced developers and new people exactly how to use CSS variables. It has been a difficult journey and we still have people doing their own weird things with LESS/SASS precompilers and not writing maintainable CSS.
- schwartzworld 6y agoWhat resources would you recommend to teach either using CSS variables or writing maintainable CSS. I'd love to learn about either of those
- wa1987 6y agohttps://every-layout.dev/ https://every-layout.dev/ is downright amazing for learning how to write robust layouts
- chrisweekly 6y agoCame here to post same. https://every-layout.dev https://every-layout.dev is a game-changer. The free content is a goldmine, and having spent $100 for the complete set of reated materials (book, component generator, etc) I can say I've gotten easily 10x that value out of it, maybe more. Hands-down the best CSS-related resource I've encountered since starting my career in web dev in 1998 (ie, before CSS was created).
- taf2 6y agohttps://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_c...
- jibbit 6y agoi'm thinking i don't understand css variables very well
- rriepe 6y ago
- enhdless 6y agoThere was a good HN thread about this a while ago too! https://news.ycombinator.com/item?id=23865900 https://news.ycombinator.com/item?id=23865900
- chrismorgan 6y agoI observed this whitespace behaviour by accident maybe a year and a half ago (I think it was related to either CSS minification experiments or how Firefox’s dev tools didn’t cope at all well with empty or only-whitespace property declarations), and after a bit of poking and prodding figured out how to achieve exactly what this article describes, poor man’s if(). Since then I’ve seen a couple of others discover and report this independently as well, and I’m sure plenty of others have found it too. It’s always fun when you have a complex system and people stumble upon interesting emergent behaviour. I doubt that the CSS Custom Property spec writers had this sort of application in mind when they specified that it was whitespace-sensitive.
- rendall 6y agoThe comments from James0x57 led me to a github repo wherein he discusses this hack https://github.com/propjockey/css-sweeper https://github.com/propjockey/css-sweeper > Space Toggle has been independently discovered a number of times > @anatudor probably the first to discover it - about 3 years ago! > @JamesOx57 in April 2020 which kickstarted development of augmented-ui v2 because it allows augs to be equipped without extra class names. > @DavidKPiano in June 2020 and instead of just space, he includes an empty comment and calls it prop-and-lock And now Lea Verou
- parhamn 6y ago"...and that would set its border, background image, box and text shadows in one fell swoop?" Like a class?
- deleted 6y ago[deleted]
- kroltan 6y agoYes, like a class, but which can be set through CSS and not needing an interactive JS or only be used statically.
- heladakilioohug 6y agocheap advertisement. do you really need to toggle multiple values with one "custom" property?
- swyx 6y agofor those interested this "hack" is in extensive use in Tailwind css to control opacity for a host of color utilities https://tailwindcss.com/docs/opacity https://tailwindcss.com/docs/opacity
- lights0123 6y agoPure opacity, like your link, doesn't use variables at all—just regular CSS opacity. Background opacity, however, does: https://tailwindcss.com/docs/background-opacity https://tailwindcss.com/docs/background-opacity
- chrismorgan 6y agoThis doesn’t look to be true. Opacity is straight `opacity: 0.5` and the likes (no custom properties involved) and its background and text opacity things use CSS Custom Properties (`--bg-opacity: 1` and `background: rgb(…, …, …, var(--bg-opacity))`), but not this technique which is all about using custom property values containing only whitespace.
- junon 6y ago> This is a declaration where the value of --foo is literally one space character. I don't know if this is technically accurate though it leads to the same result. Whitespace in such languages is generally ignored - I believe this would just result in an empty property under the hood. EDIT: also why the off-kilter background shapes for the code blocks. It made me dizzy reading the article.
- chrismorgan 6y agoSpeaking bluntly: you don’t know what you’re talking about here (you’re talking in generalities without specific knowledge of how CSS is implemented), and Lea does (she’s very expert in this area).
- junon 6y agoYou don't know me or my background; please do not act like you do. I write parsers and compilers for fun, often. https://www.w3.org/TR/css-syntax-3/#parse-rule https://www.w3.org/TR/css-syntax-3/#parse-rule Whitespace tokens are skipped, as I suspected. There is no whitespace value. As I mentioned before, the result of what the author describes is the same, but the explanation isn't entirely accurate. It's harmless errata. Perhaps it is /you/ that doesn't know what they're talking about.