189 points · by adrian_mrd · 5y ago · 143 comments
After the dot-com crash I found myself coding again, and fell into CSS with his book, and managed to carve out a spot for myself as a bit of an early CSS guru in my professional network. Doing that saved my house, quite literally.
CSS can be gnarly and frustrating. This was even MORE true back then when you basically had to create parallel implementations of the same layout to support IE's willfully wrong interpretations of the box model (oh, and its bugs). But it beat the everliving crap out of what came before.
I've long since left that part of my career -- I mostly talk and tell people what to do now -- but I'll always love CSS a little for that period of my life.
Infact, it's the best language for styling content ever created. Think about this the next time you have to create a couple of XML files just to get round corners and shadows in Android.
Try covering dynamic-sized image with an svg surface. Try outlining a hovered element so that outline/shadow covers other items around. Try to baseline align text in non-shallow div hierarchy (which is required to workaround other issues).
When there is a moderate visual requirement I’d better mess around with something like GTK+/Cairo, NSView or QPainter rather than pulling hairs at CSS trying to slap few incompatible js-y solutions into one big ball of bs.
I'd appreciate a visual example of what you are talking about. Seems like a far fetched, obscure-as-hell example to use as an argument. A strawman as they would say. CSS isn't perfect but it's pretty damn powerful for something I can teach a 9 year old.
We can certainly get a lot of work done within CSS's limits. For the auto-generated billion div soup, god speed and good luck.
You can also hit the ugly points very quickly.
E.g., suppose I want to draw diagrams of text connected by Bezier curves. Looks like SVG is the obvious choice for me. But suppose I add editable text to the mix-- well, HTML5 certainly has plenty of options for me there. And the Javascript to glue a solution together however I want.
Yet, none of those options come with any CSS that allows me to align the baseline of the text in my choice of HTML element with an SVG text element of my diagram. I have to scour Stackoverflow for some holy-shit hack of nested div bullshit just to tell the browser to position HTML stuff the way it does for an SVG text element.
These rought seams joining SVG, HTML, and CSS remain very frustrating until you start reading the specs and list archives to figure out that they were very different tech developed by very different teams with very different initial purposes and use cases in mind
Covering a image with something (whether SVG or not) is very common, though most of the time the image’s size will be known, or at least known in one axis. We’re talking things like putting captions on top of images.
Needing hover effects to render on top of elements around is even more common—if you want a row of buttons that share borders, by far the easiest way involves this very situation. Example where `position:relative` does the magic:
data:text/html,<style>button{border:2px solid red}button:is(:hover,:focus,:active){border-color:lime;position:relative}button+button{margin-left:-2px}</style><button>ⅰ</button><button>ⅱ</button><button>ⅲ</button>We can certainly get a lot of work done within CSS's limits
All I really want is a linear model over current layout model. E.g.
...your regular css...
.wrapper:relations {
div.text/left == svg.line.underliner-segment/left + 5px
div.text/baseline == svg.line.underliner-segment/bottom + 2px
#item-a/center-x == #item-b/center-x
#item-d/right >= #item-c/left + 20px
*/right <= $this/right - 10px
*/bottom <= $this/bottom - 10px
}
I don't want play a detective with css properties ever, when there is a proper mathematical model for coordinate relations. Those who are fine with current CSS could then continue to craft wands and potions and everyone'd be happy.Sounds challenging to implement that in straight CSS :) I'd love to see a demo
This isn’t difficult. Two reasonable approaches immediately occur to me on this.
① The traditional one: <div style=position:relative;display:inline-block><img><svg style=position:absolute;inset:0 /></div>
② Using Grid’s ability to put multiple things in the one cell: <div style=display:grid><img style=grid-row:1;grid-column:1><svg style=grid-row:1;grid-column:1 /></div>
> Try outlining a hovered element so that outline/shadow covers other items around.
On hover, ensure it’s on a layer of its own, and if its surrounds are also on layers bump its z-index above theirs. e.g. `:hover { position: relative; z-index: 1 }`. Definitely hacky, but not difficult, unless you want the decoration to render beyond the bounds of a parent element’s layer, such as if it’s in a scrolling area with 0.5em of padding and you want a 1em box-shadow around it—that simply can’t be done, layers are inviolate; but as consolation, other platforms almost certainly have the same limitation.
It indeed is [1].
Expected: a yellow line crossing an entire 236x236 image, in all three cases.
Explanation: svg default size is lunatic (300x150) and it doesn't care of your layout modes, so it clips all drawing beyond that, unless you specify either width/height attributes (not css properties!), or a viewBox, from which it infers missing width/height.
I lost a day to this once, when parent-filling svg clipped 3 of my 5 shapes for no fucking reason. Shapes coincidentally fell either fully into 300x150 box or fully outside of it -- no easy hint for you.
You can pre-calculate that in js, but if you specify .wrapper's dimensions in non-px, or use some aspect fit, you're screwed as well.
You could embed the dynamic-sized image within an svg, perhaps even a fully dynamic inline svg.
> Try outlining a hovered element so that outline/shadow covers other items around. Try to baseline align text in non-shallow div hierarchy
I mean at some point you need to use JS. Still, IMHO it's quite amazing how far you can go with a purely declarative language. That also means a myriad of side effects including smooth reflow being taken care of automatically as long as best-practices are followed. Is there any way to write these examples completely declaratively in GTK+/Cairo, NSView or QPainter?
It gets a bad rap because it's so easy to use for non-designers that a lot of sites that use it don't bother customizing the UI elements like buttons or expand/collapse accordions etc. Or the layout of the page, for that matter. The famous "Every Bootstrap website ever"[0] site outlines this nicely:
Only a couple months ago I decided to check what Mr Meyer was up to. I bought his book Design for Real life thinking it was about CSS. I was delightedly surprised by the breadth of new things I learned about design that I had never considered before.
Well I remember a significant difference in the early years of CSS. Netscape Navigator would frequently crash when encountering perfectly valid CSS while IE at worst would render it weirdly. Netscapes habit of crashing probably set back CSS adoption for several years, since you couldn't even introduce CSS as an optional enhancement.
I know a whole generation of designers loathe IE, but this is noting compared to the hate designers felt towards Netscape in the early years!
It was the right thing to do. CSS and javascript just slow down the computer and run malware.
I used the Wayback Machine (Thank you, Internet Archive!) to dig up some of my old designs, and boy, I used tables for all it was worth, and then some.
Unlike Mr. Meyer, it didn't feel wrong. But again, unlike Mr. Meyer, 25 years ago I was ... let's just say "young", around 13-14.
I'm planning to write a small blog post about it some day, because the amount of tables I used for a simple heading is just staggering. It must be at least a full page (in 800x600) for just a simple header. I don't think I can explain how or why without actually posting screenshots.
Especially when you wanted to do rounded corners, with tables and sliced images.
At least it was/is easy to vertically center the content of the cell. Unlike divs, for reasons that still escape me.
If you want to cash in on that table knowledge nowadays, just do email templates ;)
Every WYSIWYG HTML editor made it super easy to us...abuse tables for layouts. IIRC Dreamweaver was still rocking table layouts until the MX 2004 release. Dreamweaver 4 and MX definitely focused on table layouts. I seem to remember MX supporting CSS but the layout focused on tables.
Just different flavors of alien.
You wanted to position things at specific coordinates, and have the browser take care of padding/flowing/etc.
A table is a natural choice.
Unfortunately tables did not work like a coordinate system, since cells would grow to accommodate their content. Combined with spacer gifs, tables could be used to generate whitespace padding and aligning content, but it was very tricky to use tables for exact positioning.
But you could easily lose at a job interview if you were a proponent of this.
Now that CSS grid is finally useable both sides can agree on a proper answer, but this is 20 years later.
That's the thing about purists, they're not wrong in pointing out the problems, but they're often so obsessed with the problems they fail to critically evaluate the alternative solutions and realize they are also terrible.
Even today you might have to support old internet explorer, so you cant use display:grid.
Of course incompetence can be present on either side of the table in interviews. I have heard about people avoiding display:table in CSS because they heard tables were bad.
Flex and grid make the majority of responsive layouts a breeze and the some of the new filter stuff is super powerful, just a shame it took 20+ years to get it. Now let me transition to and from `height/width: auto` damn it.
Width/height: auto require a layout to know the size, so I don't know if we'll ever get that either.
[1] https://www.w3.org/TR/css-flexbox-1/#intrinsic-main-sizes
> Width/height: auto require a layout to know the size, so I don't know if we'll ever get that either
Surely the browser ultimately knows what the size is going to be?
Huge thank you to Eric, Zeldman, Shea, Jeremy Keith (DOM Scripting), etc. for their contributions. Their publications and answers to my emailed questions helped me set my students up for the future of front-end development.
I chuckle recalling all the tricks we had to learn/use to get our pages to work across all browsers and my insistence that all pages validate to receive full credit!
As for the general case, every website is different enough that no general-purpose GUI/WYSIWYG exists that could cater to every need, I guess.
Pretty impressive, if you ask me. I'm choosing tables too. And CSS.
Now if you add 1x1px spacer gifs, then you get more consistent control. But you also would have to be a masochist.
What exactly is stopping you from doing just that today?
So your document becomes a mix of semantic structure and then wrappers, containers, pushers, purely visual areas and so on. Also you're going to have things that are specifically redundant for your use-case, but you still add them for consistency and re-usability.
CSS would have to be quite a bit more powerful to achieve a clean separation of document structure and layout. It would need things like purely visual grouping mechanisms, reactive or backtracking selectors and HTML itself would probably need to be either a relational or graph structure instead of a tree to actually convey the semantics correctly and consistently.
E.g. let's say I have some html like <p class="warning">be very careful<p> and there is some 3rd party CSS that already has nice styling for a warning paragraph. But it uses .warn as a class, not .warning.
I don't want to change my HTML and I also don't want to copy the style. I want to tell CSS that my own .warning should inherit the style of .warn (and maybe adjust it slightly if necessary)
That is (to my knowledge) currently not possible.
What is it that not currently possible with CSS, which requires JavaScript or generated HTML? I understand there's things CSS can not do, because that not it's purpose. I'm not saying that you're wrong, just genuinely curious.
It's maybe a weird comment but HTML feels clunky and rigid and you sometimes need JS to patch over that clunkyness. (CSS just reacts on HTML structure so it is not specifically at fault.
In other words you sometimes need to break out of the HTML tree and move things around so they display and behave as intended by web design.
Another thing that comes up often is breaking out of the default UI controls. Some designs are quite playful and interactive, so you end up zooming, moving, replacing and adding stuff based on user input etc. In short: as soon as you have to listen to more or other using events you are going to use JS.
Also more classic and conservative designs (example: HN) are essentially one dimensional with a bit of two dimensionality to them, AKA just text that flows through a simple layout. There, you typically can avoid using JS for these kind of things. But if your design is more two dimensional it becomes increasingly harder to avoid JS. Flexbox was an addition that provided some notion of two dimensionality and Grid is a feature that goes further into that direction.
There are web servers written in Brainfuck and Bash. They are capable of it, you can be surprised by it, but it doesn't mean that this is useable or applicable in real life.
What CSS lacks? Take what Sass offers, go through feature by feature, and you'll find so many things lacking. Some of them are slowly making their way into CSS, with emphasis on slowly: variables are in, nesting has been debated for 6 years now.
What else CSS lacks, and will lack forever due to the core nature of HTML and CSS? Actual useful animations. Where you can animate a height: auto, or a list item being removed from a list.
Animating height:auto would be very welcome though.
Which currently is:
(1) CSS holds no mutable state
(2) CSS selectors are all based on existing DOM/device state.
"On-click" is not a state, it's an event. CSS can't select an event. And toggling classes is new mutable state. CSS can't have mutable state.
Also you may be overstating the problem with this "so many needless use of JS". It's a tiny snippet you can add once and reuse it everywhere in your document.
https://jsfiddle.net/y1qv069p/5/
document.addEventListener('click', e => {
let n = e.target;
do if (n.getAttribute?.('data-toggle') !== undefined) n.classList.toggle('on-click');
while (n = n.parentNode);
}); div {
behavior:check;
}
then any div will behave as a checkbox - will toggle :checked flag on element and so we can use: div:checked {
color:red;
}
no JS is required at all.Modern CSS is bearable with features like Flexbox and Grid, but CSS will never be able to hide how inelegant, clunky and sprawling it is. You could write an entire book on CSS 'gotchas' - from specificity to box-sizing to inheritance. The list of topics is endless.
There are also dozens of different ways of writing CSS (by design) which is why you never see two or more people write it in the same way.
Why did a "style sheet" language incorporate animation into its already bloated feature set? (Not convinced by the argument that animation = appearance and thus fits into the scope of CSS.)
It's a bit ironic that modern CSS features let you finally drop horrible CSS hacks from the past like floats. And yet, developers are not taking advantage of modern CSS features like Grid (which greatly simplifies layout). CSS frameworks often don't take advantage of these modern features either.
In fact, modern CSS makes it easier to write plain CSS without the use of a CSS framework. But is there anyone left who isn't using a framework?
It's like jQuery, it was easily 5 years between when we no longer needed it and when we finally started dropping it.