Instead of looking at the DOM holistically, we began instead to think in terms of componentization. Now, everything is a component and a component really is a subtree of the DOM. Now, this is not looking at the whole page presented to the user. Instead, we're thinking about small sub trees and then being able to move those sub trees around within different DOM structures, components, component that component is fundamentally different from looking at the DOM holistically.
So the thing is that JavaScript developers working in JavaScript frameworks that were building the DOM for them, the things they didn't like were the very things that people fell in love with CSS for that separation of structure and presentation. JavaScript developers would say, well, we don't want to spend our time sometimes in JavaScript and sometimes in CSS and sometimes in HTML component cascade. Well, that was a problem because of my components. If I move my component from one container to another container, the cascade rules override and I have to think about what might happen with the CSS on that page. Selectors that required the DOM structure to be consistent. And if I'm making a component and moving it around, then who knows what the final DOM will actually look like?
As a result of the sea change, we went from HTML that might look like this, where we're defining our classes and using them along with our structure, the things becoming popular now like this, where we're just focused on a particular element and just the styles for that element, in some ways doing the opposite of what CSS was intended for, and a negative downside being becoming more focused on what the CSS needs to be. And just adding a div so that I end up getting a component that looks like the prototype. But CSS has continued to grow and to add some really extraordinary features. It's time for the modern JavaScript developer, the modern front end developer to fall in love with CSS. Again, what are some features of modern CSS that works so nicely now? With the componentization of the DOM. Well, let's just look at a few examples. One is CSS nesting. Previously, we might say, well, I have a notification class for my notification component. And then if it's actually a success notification, then I add a secondary class on that. And those are all separated in your CSS rules. But now, with natively supported CSS nesting that is in the browser, in the CSS specification itself, we can show in the way that we write our CSS that all of these rules are connected to each other, that they're part of the same set of rules. Now, it's really just syntactic sugar, meaning it doesn't add anything that couldn't be done before, but it's a much nicer way of writing your CSS, especially when you're writing CSS for a component, when you're writing another feature, cascade layers, the at layer keyword. This allows me to group my different CSS rules and then order them of their own specificity, but not really specificity, but ordering which layer should have higher precedence in the cascade.
Comments