Writing CSS Algorithms
notlaura.com
notlaura.com
What’s next? CSS data structures?
I think the issue is that an algorithm is typically an unambiguously defined, imperative solution to a problem. It involves step by step instructions that happen in a certain order. Even though you can argue that these CSS patterns are clearly defined solutions to specific problems, the fact that there are no steps involved, no procedure, nothing imperative, makes it odd to call it an algorithm. There are other established words to use, like pattern, utility, hack.
There’s added confusion since CSS obviously involves actual algorithms, like how the engine calculates selector matching, specificity, layout, filters, etc.
I still think it's fair to include the person's activity in the broader idea of what constitutes an "algorithm" here, and I believe the article's language does support my interpretation.
First, the author explicitly acknowledges that "algorithm" is being somewhat misused compared to it's conventional programmer usage, and gives a bunch of reasons why she thinks it's still "useful" to co-opt this word. Some of the point behind this article is precisely to explain and defend her use of the world "algorithm" in this context. Language is fluid, and that's okay & sometimes fun & useful & interesting, right?
Also, there are steps listed in the article, such as defining boxes for layout, then naming them, then listing changes, then determining dependencies. That counts as step-by-step instructions, I think.
The author also said: "A CSS algorithm is well-defined declaration or set of declarations that produces a specific styling output." and "Phrased another way, a CSS algorithm is a very intentional and well thought-out utility pattern."
That's pretty explicitly including the design & thinking in the "algorithm" as I read it, don't you agree?
> an algorithm is typically an unambiguously defined, imperative solution to a problem
The English definition of an algorithm is closer to "a process or set of rules to be followed", and that's basically the extent of it. We programmers can use the word to mean more specific things if we want, but can't claim that the word always means the same thing to others as it does in our expert domain, or even that it always means the same thing in our domain.
I'd have to disagree with the qualification of "imperative", math algorithms and functional algorithms are widely accepted as algorithms by programmers. Unambiguous is always desirable, but can't be the definition. The words "process" and "rules" imply minimal ambiguity.
FWIW, I've been trying to find examples of things conventionally called algorithms, but which are not in essence imperative (in the sense that there are multiple operations that must happen in a certain order - "procedural" is perhaps a more precise word than "imperative"), but I can't really find anything, so I would like to insist that it's one of the defining characteristics of an algorithm.
I've been in my low-level C++ world for a long time and am pretty out of touch with GUI/Web things. I was writing some JavaFX code and saw it can be extended with CSS, but I don't know where to start. I'd also like to tweak the CSS on my webpage, but also don't know what's what. What I've found online is targeted towards people starting out coding with Javascript so it's very slow and hands on and doesn't step back and explain the big picture
A little unrelated but as someone who also loves their C++ and low-level programming I really enjoyed Lin Clark's articles on Firefox's Stylo [2] and WebRender [3], I think they do a great job of intuitively explaining the back-end behind CSS rendering, if that's of any interest to you.
[0] https://developer.mozilla.org/en-US/
[1] https://developer.mozilla.org/en-US/docs/Learn/CSS
[2] https://hacks.mozilla.org/2017/08/inside-a-super-fast-css-en...
[3] https://hacks.mozilla.org/2017/10/the-whole-web-at-maximum-f...
I have watched the talk that goes with this post and I think that this is useful to better understand where Laura is coming from. She has gone deep reading the code for implementing the 'cascade' and you have to give her credit for getting people talking.
However, in the article there is some idea of solving responsiveness. There is a better starting point and it is this - CSS variables.
In the olden days people would have one chunk of common CSS and then media queries that would have other chunks of CSS in them. The better way is to only have CSS variables in media queries and then to have all the style declarations use the CSS variables. In this way you can have maintainable CSS where it is clear to see what changes.
An example, if you have an image in a figure then you might want it to appear on the right hand side in a desktop layout and full width on mobile. So in the root element of the CSS you can define --my-figure-grid-column as 1 and then in the media query for desktop you can then set the variable to 2. Then in the style rule for the image you can have grid-column: var(--my-figure-grid-column). If you review the code a few months later and decide you want the image on the left you can see what is going on and what you need to change. With old CSS you would probably decide to just bolt yet more style rules to the bottom with !important to hack it good.
CSS Grid is a good place to start, this is the new layout engine and it works in 97% of used browsers. If you start from content with sensible HTML5 elements instead of 'div soup' and make it responsive with CSS Grid and CSS variables then you will have as much fun as a kid with LEGO.
There are a few other things on starting out, one being to forget about pixels and percentages. Go with ems and 'vh/vw/vmin/vmax' instead. Document structure matters too, if you are using CSS Grid then you don't need the div tags or the spans. You also don't need the clutter of many classes added to every div. So maybe strip your webpage back to bare content, burn the existing CSS and any 'reset.css' type of files that are there for Internet Explorer 6 related reasons. Get the page structured with the HTML tags of header, main, footer, article, section, aside, nav, figure and the ones you know already for doing lists. Then get it looking good on all browsers with CSS Grid with layout problems solved with CSS variables. You can then move on to the more decorative things.
To quote: «You call throwing dynamite around martial arts?»
I'm mostly joking, this is neat.
I stand by my statement. JavaScript has progressed, and styling should too.
When I do have to do any web gui work, I'm starting to lean toward Parcel because I can import SCSS/SASS, Less, or CSS right into the javascript/typescript file, and it will spit out web-usable CSS without configuration. Of course this only helps if you're including JS anyway. I don't know enough about the system to know if you can configure for just SASS. But that just returns to your problem, anyway.
Can we just agree that current CSS is a mistake and bring more of SCSS into it?
The pain points usually stem from inconsistencies in browser implementations. Except for missing functionality like not beeing able to select a element who has no child of a given type I never really felt that it took longer than it had to (most of my time goes into deciding the design and not into wrangling with fine details of css).
But maybe I should check out SCSS?
I don't think CSS Nesting Level 3 is implemented anywhere (it only really got added last year [1]). And there are still ongoing discussions around it (e.g. media-queries [2]).
> But maybe I should check out SCSS?
Definitely, yes. There are quite a few things that would be wonderful to see in CSS:
- better, less verbose syntax for variables
- mixins and extends
- nesting
... I forget what else, as I haven't done frontend work in a while :)
[1] https://github.com/w3c/csswg-drafts/issues/2701#issuecomment...
Do you have a realtime workflow for scss?