My First CSS
engineering.kablamo.com.au
engineering.kablamo.com.au
I would add a general principle that I think is really important: let the layout engine do as much work for you as possible. Which is to say, tell it as little as possible.
If you want that <div> to be narrower, you probably don't want to set it to a narrower `width`, you want to step back and understand where its current width is coming from, and determine how that flow can be shifted upstream to achieve the desired result. CSS is a language of constraints; use them to guide the layout like water, instead of hammering it into place like wooden planks.
When you do the latter, that's how you end up in a quagmire of competing constraints where the sum behavior is impossible to predict, and changing that behavior is infuriating (and often involves `important!`s). That's the kind of situation most people are talking about when they say they hate CSS. For some, it's all they've ever known.
Ah, well _that_ width is dictated by some image code at the bottom of the page text. The image was cropped incorrectly because the designer was on vacation and they had to use some random online crop tool during our cloud software outage. We fixed it with some additional markup and inline CSS, but the inline CSS was stripped during an upgrade to the latest inline HTML editor we use here.
Anyway, the graphic was supposed to be a flourished section break that was center-aligned, and instead it is now a white bar that is currently even longer than the anchor text that is also overlapping the background imagery.
If you remove the graphic, be careful because it reveals this...other problem, also with the CSS.
You know you can scroll to the left and right really easily if you hold down shift?
I wrote this a good few years ago and the writing style isn't great looking back, but I stand by the ideas :)
Take his example of the images being off thus screwing up your widths because you chose to inherent the image width as a constraint. Now imagine a very realistic scenario where you are reusing that style across multiple pages, and each page has a different image. Now you need to confirm this works for all existing and future possible images, good luck with that.
Point being, isolating element styles from each other where possible is a big win for maintainability and reliability.
You want CSS to solve this problem that other tools and workflows introduce? `article .content img { width: auto; display: block; max-width: 100%; }`. This is not the solution, but one of many possible.
> ... step back and understand where its current width is coming from, and determine how that flow can be shifted upstream to achieve the desired result.
Step back here is seeing that large images in certain containers push the container out and that you want to avoid this.
If you want CSS to avoid the problems you list, in the first place: that is just unconstructive: The tooling, holiday schedule, poor migrations, and broken upgrades are causing the issues, the constructive solution would be to fix those. And if you cannot, to look at how CSS can help you with them.
> Now you need to confirm this works for all existing and future possible images, good luck with that.
This is not hard at all. It is what both the C in CSS and classes and inheritance solve for you. `img.leader` vs `img.aside` vs `article p.teaser img` vs `article img` etceteras.
Going back to brundolf's point, he's arguing that you should try and take advantage of relative constraints as much as possible as CSS lets you do that. I'm arguing the opposite, and so are you in your last line, that the solution is to hardcode those images and decouple the two components.
You can say don't blame CSS as it gives you a multitude of options for how to deal with the situation, but the counterargument is that good tools lead you to a "pit of success" where the correct way is obvious, and CSS is more like a "tiny little bridge out of Moria of success" where you're just asking to fall to your doom.
And it's simple enough that even amateurs can accomplish much.
Second, to call CSS simple is insane. How many ways are there to create a positioning root? How many ways to create a stacking context. How many different things can using '%' as a unit mean in different context? CSS is incredibly complex. CSS tries to shove these important concepts implicitly away from the developers notice, only to ultimately make life worse for both amatures and pros.
On a touch interface?
That being said it's very annoying to read anything that needs to be scrolled sideways. So if I come across it I zoom out and if it's too small to read then I navigate away from the site.
On macOS, but not by default on Windows. On the other hand, Chrome has adopted this as default behaviour even on Windows, so I guess Edge has got it now too.
You can scroll left and right easily, not everyone can. Avoiding the need to scroll in two directions makes content more usable to everyone but there's also a WCAG criterion about it.
This touches on my key insight with CSS (and programming in general), in that if you have a problem with your page layout, that probably probably exists in the code you’re already written, so adding more code on top will only make things more difficult.
Step back, understand exactly why the layout is acting why it is, and address that problem rather than just monkey patching a hack on top.
I think this is the core reason why functional CSS is growing, because by separating out your layouts from each other, one can actually approach CSS refactoring this way without worrying about breaking other pages.
This way you work with the cascade, as CSS was designed.
Defining sensible defaults and then having a few utility classes for layout helps with this. You could follow it BEM for the rest.
This structure results in minimal CSS that doesn’t fight itself.
The only thing I've found to actually work is to keep things as simple as possible when it comes to your actual stylesheet. Any kind of complexity including some core CSS properties (e.g. the C for cascading) I avoid. My second box .foo is only slightly different so I will adapt it based on its parent .bar leads to misery. Eventually you will need .baz .bar .foo. Or .bar .foo:not(.shoot-me) and all of that will be burried in 100+K of code done by people that left the project 2 years ago. Hence wack-a-mole. So I try to do myself a favor and just avoid any kind of CSS logic. The complexity will be moved to the template, so not really gone, but at least you can make a change and not worry about something else breaking. And at the very least, if it gets complicated, you can write a test to check if your html code contains proper classes.
Thanks for the catharsis.
I'm on a team of 4 competent people who touch front end code, and despite our experience with it CSS is more of a recurring pain than anything else. We plan to make a slow migration to tailwind, essentially building new things with it and keeping our bespoke CSS in maintenance mode until it's replaced or retired. I'm looking forward to our ugly markup.
I think Adam's quote on the front page of tailwindcss.com is spot on "...but the truth is you’re never going to believe me until you actually try it. If you can suppress the urge to retch long enough to give it a chance, I really think you'll wonder how you ever worked with CSS any other way"
Once you are comfortable with CSS, try to dig a tad deeper into design philosophies and patterns. Work with established designers who understand that spacing is not just margins/padding of a designated number but a rhythm across screens of varying shapes and sizes.
For instance, understand the reasoning why a good designer will tell you that the typographic scale for desktops will be major-third, while for the smaller mobile phones, it will have to be minor-third.
Advance topics to research and have peace with CSS are vertical rhythm for spacings, modular scaling for typography, and the Cicada Principle, etc.
Here is an anecdote to illustrate the above;
There was a "Careers Page" redesign for one of the biggest banks in the world - after about 10+ years of their old one. When I landed in London, they already had a "Senior front-end developer" mid-way into the project. I went as a replacement for one of my team who had a family emergency. I haven't written much code in years.
They had engaged a brilliant designer and had a wall plastered with some of the most beautiful glossy print-outs. Everyone, including the client, loved it. Unfortunately, the struggle was -- the front-end output had irregular margins (pixel mismatch), fix another but breaks the other, etc.
I saw that they had screen rulers that try to match the design with what comes out on the screen.
I requested the designer to spend few hours explaining his reasoning behind the spacing and asked him the rhythms (similar to musical notes) that he has used, the scaling of the font sizes. This was when I realized the designer was brilliant, but the engineers would not understand or appreciate his effort.
I wrote out the Sass functions and mixins and steadily replaced many hacked in numbers and values. Introduced Design Tokens, a simple style guide, etc.
The website is still there and will likely last another 10+ years. I'm also sure many of you might have stumbled on it or have applied to jobs with it. :-)
Do you have some links? Because the first DDG result for all those concepts together is your comment on this page.
- More Meaningful Typography from List Apart, https://alistapart.com/article/more-meaningful-typography/
- Responsive typography with Modular Scale: Create meaningful proportions in your design using SASS, https://www.bugsnag.com/blog/responsive-typography-with-modu...
- How to Establish a Modular Typographic Scale, https://webdesign.tutsplus.com/articles/how-to-establish-a-m...
- https://www.modularscale.com is a tool to define your scale
- The Cicada Principle, revisited with CSS variables from an industry Design + Engineering guru/goddess, https://lea.verou.me/2020/07/the-cicada-principle-revisited-...
- https://utopia.fyi/blog have some good articles, more modern and relevant to the new CSS such as Clamps.
I had fun with Cicada - beautiful background patterns, which you yourself cannot predict. I was looking for my presentation on "Design with Mathematics, and Patterns" (or something in that line) but I think I may have lost it like many other open source files from before Github.
I tried to google major-third and minor-third with various keywords but couldn't find anything relevant. Could you expand on this and explain what it means?
Edit: Never mind, I found some. Here:
- https://medium.com/sketch-app-sources/exploring-responsive-t...
- https://spec.fm/specifics/type-scale
In particular trying to get content of some flexbox to fill the space provided by the flexbox itself, especially in a single non-scrolling app like page where you're trying to constrain everything to fitting in the window. Like the rule that you have to set min-width to 0 or something to get the desired behavior.
https://makandracards.com/makandra/66994-css-flex-and-min-wi...
That's not the only gotcha
I suppose you could argue that's not fundamental but to me, basic layout that most standard native UI toolkits would provide is an essential feature and yet to achieve it in CSS requires knowing a few obscure tricks
I wish someone would write some other UI language that would translate into CSS so I could write
full window frame
left side
right side
toolbar
content
And it would generate the correct CSS to make it workTo put it another way, imagine you want to repo the Keynote UI or Numbers UI in a webpage. Those are basic app UIs and yet they just slightly less than straight forward in CSS. I'm not saying they are hard, only that most people will have to goole some non-intuitive tricks to get things to work which is a different experience than trying to do them in a native UI kit where it's more likely fairly clear how to achieve it.
Some of the notable drawbacks:
- you can no longer select text on the page, anywhere
- right click doesn't work like you'd expect it to, either, since you're looking at a canvas
- currently the download sizes are absurdly large, since you need to pack a large amount of functionality in .js/.wasm (over 10 MB, actually)
- for simpler pages, the performance overhead will be very noticeable, just try zooming in or out in those pages (i got <2 fps while zooming on a decent desktop computer)
- essentially all of the former accessibility benefits of DOM are now gone and screen reader support needs to be developed anew, assuming that they'll even support your canvas based solution
- furthermore, learning about websites by inspecting source to see how they did it is no longer possible; and you can no longer crawl these sites in any sort of an automated manner, which may be a good/bad thing depending on how you look at it
I'd say that most GUIs should be simple enough to work well with DOM, instead of throwing away decades of progress while chasing niche goals. HTML and CSS are great for displaying all sorts of documents and personally i think that the web should not stray too far from that - utilitarian interfaces that are simple and easy to understand, rather than sci-fi ones like portrayed here: https://www.saji8k.com/kit-fui/movie/Wow, that's really, really bad. Thanks for calling attention to that.
> currently the download sizes are absurdly large, since you need to pack a large amount of functionality in .js/.wasm (over 10 MB, actually)
Just wait till someone uses this to build their desktop-only Electron app.
That's a deal breaker for me.
I’ll just stick with the easy paths.
I get where you're coming from, I really do. HTML5 and CSS are just bad tools for doing GUIs. But there is so much infrastructure built up around them that just overrules any advanage we would get hand rolling a GUI directly rendered to canvas.
Is there something better? With Qt Widgets, the poster child for cross-platform GUI toolkits, I also frequently have problems with getting the layout to behave the way I want.
Not cross-platform.
> iOS
Not cross-platform.
> Tk
Decreasing mind-share for 2 decades+, not something I'd start a new project with.
I agree, but when I need to develop some software for Windows and Linux, a great toolkit for Android or iOS is of no use to me. Similarly, I won't use a Linux-only or Windows-only toolkit, as awesome as it may be, because I'm not going to build two different GUI layers.
Thank you, you're my savior. This is exactly the problem I had on Friday and now I can finally solve it.
.header {
grid-area: hd;
}
.footer {
grid-area: ft;
}
.content {
grid-area: main;
}
.sidebar {
grid-area: sd;
}
.wrapper {
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-auto-rows: minmax(100px, auto);
grid-template-areas:
"hd hd hd hd hd hd hd hd hd"
"sd sd sd main main main main main main"
". . . ft ft ft ft ft ft";
}
Way more declarative, in my opinion.For fitting it all in one page, you can make the containing grid 100vh and ensure content takes all the extra free space, see this pen (from Mozilla) for an example: https://codepen.io/miriamsuzanne/pen/JjPeQYP?editors=0100
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_La...
I understand why this is the case (mostly thanks to a patient HN commenter who explained it to me), but holy hell it annoys me how unintuitive it is. You have to set min-width to stop a flex child being too big? Get out of here with that nonsense.
The common wisdom seems to be that 1D layouts should use flex and 2D should use grid, but I don't think I agree with that. These days, I only ever use flex for layouts that should actually be flexible. Even if it's a single dimension, if it's fixed-widths it's straight to grid for me now, and my life is measurably better for it.
I rarely find a need to think about `positioning` though and I think the article doesn't give enough credit for `display: flex` and `display: grid`. I tend to reach for `display: flex` as a default and find it much more intuitive. Whether it's easier for a beginner is a different question though.
I feel ashamed not knowing about this option. content-box has caused me so many troubles over and over again, and I didn't know it had such an easy solution.
My CSS knowledge has improved substantially since subscribing to Kevin Powell's YouTube channel.
I created this tiny script[1] in order to be able to view all the rectangles that makes a website, a little while ago. Here's a demo[2].
[1]: https://gist.github.com/corentinbettiol/85a8938175f89a15ac35...
* { border: dotted red; }The browser differences especially for the box model were the bigger problem.
As a frontend engineer who started 15 years ago, learning the box model required a lot of knowledge of the internals of the browser. The times of having to know things like quirks mode, abusing overflow hidden [1], how to break the behavior of float, etc are long gone. This is great both for us having to struggle less, and for the newer generations to be able to tackle harder problems than just layout and build cooler stuff.
[1]: https://css-tricks.com/clearfix-a-lesson-in-web-development-...
I still remember learning all of those things years ago. However, because I don't use vanilla css, I mostly always have to re-google them again.
Actually, for the past few years my css in both professional and side projects has always used tailwind in React (except for react native), 95% of the layout are flexboxes and grids.
They will? I didn't realize that.
Also, to be pedantic, not all buttons are entirely clickable, only small portions of them.
Edit: Interesting that HN doesn't use the cursor hand for reply / updating. ;)
I remember a time when everything was a table.
Only if you learned from the wrong source.
and web.dev has a pretty decent walk through for learning css https://web.dev/learn/css/
I'm honestly just trying to understand how articles like this get promoted to the front page of Hacker News more and more often. I imagine that writing an article about [insert related field of mathematics] before diving into [insert field of physics] every week would not land me on the front page of Hacker News every week.
Edit: "basic field" -> "related field".
You'd be surprised how many otherwise-competent engineers don't take CSS seriously enough to seek out the fundamentals, and instead spend their days avoiding it as much as possible and cursing it when they can't
So yes, I totally spend my days avoiding it. Not because I hate CSS (I love it in some way, and I miss writing elegant stylesheets) but because CSS is a delicate tool that is hard to be maintained by more than a few people.
Which is why, even though I don't love them, I respect the usefulness of things like CSS Modules that impose more scoping and constraint over CSS codebases. For bigger teams they do tend to be necessary.
Being someone who was ahead of the game in css 15 years ago, but not having touched it since - where would one go to understand the new fundamentals and options available?
And then follow it up with this one if you want to go a little deeper: https://css-tricks.com/flex-grow-is-weird/
Personally, I think flexbox is the single most versatile and important tool to be familiar with. You can create nearly any layout with it, in such a way that it's very naturally responsive almost by default.
Throwing out blanket statements is dangerous for new comers, which seems to be the target audience of this article according to its title. For example, collapsing margin isn't always bad and unpredictable, it just seems that way if you didn't take the time to understand the fundamentals behind and go for "what works".
That's not too different to telling CS students to not worry about learning the memory and time complexities of different algorithms and just use [insert "best" algorithm] because it works most of the time.
These are actual practical usable fundamentals.
Complement that with http://inclusive-components.design/
These days, as mentioned in the OP, MDN is the best point of call for checking syntax or learning how to implement something new the right way.
Her explanation of positioning, responsive design and the links to other resources she provides inside are all good.