Ask HN: Is it just me, or is CSS too damn hard?
499 pointsI'm a seasoned backend developer and systems administrator, with over 10 years of full-time programming experience. I've worked with anything from Assembly, C, Rust all the way to Python, Erlang, Elixir, PHP, Haskell, Lisp, Clojure.
When it comes to frontend, I have used React, Vue.js, ReasonML, Clojurescript and Elm extensively, and I know my way around web technologies in general. However, ever since I first started programming, I was NEVER able to get the gist behind CSS. I can't center my divs properly. I can't say "hey, CSS, this is the parent div, and all child divs must obey its size". I can't do anything basic on CSS without turning for help.
Incidentally, but very likely related, I always failed to have any glimpse into how design works. I have no idea which colors "go" with which ones, and pretty much all fonts look like the same for me.
I'd like to know the experience from fellow programmers. Is CSS an "art" thing? Is it common for other developers to have issues with it, or is it just me? Is there any material out there that made CSS "click" for you?
Comments (340) 150 threads
Show all comments
Showing thread 1 of 150
2) I'm fascinated by the fact that so many of my fellow programmers - often the smartest ones - have so much trouble with CSS. I'm not sure exactly what the reason for that is, though I did write an article hoping to address it in some capacity: https://css-tricks.com/css-is-awesome/
I think part of the problem is that many programmers, who like things to be orderly, see CSS as arbitrary and chaotic. Here's the biggest point that I think has to click: CSS isn't a programming language, it's a constraint language. The browser has lots of default, complex behaviors that drive layout, and whereas in a traditional programming language you tend to "build up" from nothing, CSS is more about paring down and guiding that automatic engine with as few statements as necessary. You tell the browser exactly what your layout requires, and the browser figures out how to flesh that out into various contexts and situations.
Personally I think CSS is beautiful: it's stateless and declarative, and it allows you to talk about huge swaths of entities in very general terms, gently paring down their avenues of expansion until the possibility space aligns with what your application requires. It's incredibly elegant once you get the hang of it; I sometimes find myself wishing I could use a similar paradigm for talking about objects in other contexts.
I'd like to have a real constraint system, like the one in Autodesk Inventor sketch mode. You select two things and apply a constraint - parallel, tangent, horizontal, collinear, etc - and the constraint system enforces those constraints. The layout GUI tells you if you've created a conflict. CSS solves an easier problem, and not very well. It's such a mess that no one can attach a decent GUI to it.
Amusingly, tables came back. But they're called "layout tables" now, to avoid embarrassment. Trying to do 2D layout with 1D constructs (the "float" and "clear" era) was just silly.
I'm not really sure what you're talking about. Tables were bad because they were too rigid and weren't designed for layout purposes; they do still get used when you want to display an actual table. Maybe you meant CSS Grid?
> Trying to do 2D layout with 1D constructs (the "float" and "clear" era) was just silly.
The float era was certainly a dark time, and I'm glad I came into the field on the tail end of it. It was a hold-over from CSS' document-focused origins. Today we have flex-box, which quite elegantly serves 90% of people's layout needs, and CSS Grid which fills in the rest. For the record, flex-box is a 1D construct for doing 2D layouts, and it does the job fantastically.
Your Autodesk comparison belies a fundamental misunderstanding about the nature of the DOM - it is, to its great advantage, a 1D space which flows into a 2D shape. In fact, this was the great flaw with using tables for layout in the first place. By making the content fundamentally 2D, all it could do to respond to different screen sizes and different browser window resizes is stretch. But you don't actually want your layout to be totally percentage-based. You want your content to wrap and flow to make the best use of the available space that it can, with no assumptions about screen size or ratio. The web is 1D because text is 1D. As text flows in lines, within containers which may be different shapes and sizes, so does the entire document model. This is profoundly powerful. Modern CSS gives you the tools to make select aspects of your layout work differently - yes, even in 2D ways - but they're best used to shape and guide that fundamental flow instead of replacing it.
And when you have to implement a email design someone gave you who think it’s more important to have it look a certain way then to implement it reliable.
Continue this thread — 7 replies
What's the advantage? The screen is 2D space. Layouting is a 2D problem. Images are two-dimensional. It's simpler to start from 2D and make 1D content fit that space than the other way around.
> The web is 1D because text is 1D.
That made sense in the very early days of the web, now it's just a historical crutch. I fully understand why things are the way they are, but that's orthogonal.
Continue this thread — 9 replies
I'd even go as far to say the text on most webpages isn't even linear. It's either a tree (like this comment page) or a fragmented collection of text chunks.
The webpage that strictly consists of one linear stream of text is very much the exception. And it also happens to be the exception that CSS never had problems with.
What you seem to be saying is that paragraphs are 1D (which get wrapped and flowed in layout), therefore entire webpages are 1D (no) and therefore the web is 1D (definitely not).
Or maybe you could say that most viewport representations, scrolling vertically, are 1D. In which case CSS should be the tool that converts the tree or graph of content into this semi-1D viewing representation.
I agree that this mismatch is where CSS finds a lot of problems.
A very good point. My guess is that we could let designers do a lot more and developers do a lot less, if it wasn't for that impedance mismatch.
> I'd like to have a real constraint system, like the one in Autodesk Inventor sketch mode
Here you go: https://gss.github.io/
> GSS reimagines CSS layout & replaces the browser’s layout engine with one that harnesses the Cassowary Constraint Solver
[1]: http://overconstrained.io/
[2]: https://constraints.cs.washington.edu/cassowary/
Continue this thread — 2 replies
I'm not sure exactly what you are referring to, but there have been some misunderstandings regarding tables. Before CSS, -tags were there only way to control the layout, but since CSS 2, it have been possible to achieve the same kind of layout which display:table in CSS without having to abuse -tags. But there is nothing wrong with using when they are semantically appropriate.
Recently CSS have gotten display:grid which is way more powerful than table layout ever was.
Float and clear was never intended for general layout. They were invented by Netscape back in the day to allow text to float around images. Float/clear-hacks became a necessary evil in the dark age before IE supported display:table.
You cant really blame CSS for IE not supporting critical parts of the spec, since this was part a deliberate strategy by MS to "kill the web". Luckily this strategy failed and MS changed their minds.
As an historical aside: that's not quite true to my recollection.
p-elements had width, height (and possibly other attributes, margin, border?). And empty
, hr, br(?), were used for rudimentary layout? Then there was color, bgcolor attributes; and font tags. Not sure when came out?
This https://www.w3.org/Style/CSS20/history.html is probably much better than my recollection. I started making webpages/email-circulars about 1994-95.
Continue this thread — 2 replies
People have developed some techniques to mitigate these issues, but many are unintuitive and don't follow naturally from "make this thing look this way".
With the key difference being that there's no rigid structure to it at all, much less a strict-subset hierarchy. It's really more like structural object types, which can be easily composed, than OOP classes. It's set theory, rather than taxonomy.
> Without some sort of build system, static analysis is difficult so it's hard to say if a definition is being used or not
This can be easily solved with good project organization. At my day job I maintain a codebase with a couple hundred SCSS files, which are on average a couple hundred lines each. We've organized things well enough that the cascade is a non-issue, and "leftover" styles from an element that's been removed are a rare occurrence. All you have to do is closely mirror your UI components, pages, fragments, whatever, with the relevant CSS files. Then if you add or remove something in the markup, you add or remove it in the corresponding styles.
> don't follow naturally from "make this thing look this way"
This is precisely the problem; programmers come from a background where they think in terms of "make it do this thing", and they try to translate that to "make this thing look this way". That's how you create supremely fragile CSS which is impossible to safely make changes to. Instead you should be thinking in terms of, "make it behave this way in the general case, relative to what's around it".
"All you have to do is closely mirror your UI components, pages, fragments, whatever, with the relevant CSS files. Then if you add or remove something in the markup, you add or remove it in the corresponding styles."
Super simple. It is what it is and powerful etc, but simple? I have given up. I just use the minimum web tech to get by.
Continue this thread — 1 reply
CSS is actually pretty simple, as long as you start with the smallest elements and work up. It becomes a nightmare if you try and address everything individually. It’s not really a programming language, but a series of guidelines.
I’ve seen this simplicity actually throw developers as they’re used to something much more complex and systematic.
Preprocessors like SASS are great, but I wonder if they’re designed to complicate CSS a bit so developers feel at home.
Pixel perfect design is totally possible, as long as the designer is aware of the constraints and opportunities of the medium they’re working in.
But it is very possible to make websites look very good and inline with what was designed.
But good luck making an input, button and select the same height. It's just not going to be pixel perfect.
What is pixel perfect in your browser isn't in others.
But what designers should now is it doesn't matter as long as it looks good for everyone.
Designers coming from print often get caught out when designing for the web as they tend to think of a webpage as a fixed layout, rather than a flexible system of components.
It's also important to understand where the design and development budget should be effectively spent. For example, unless there's a real business or usability reason for doing so, there's little point in trying to custom-style form elements like selects as doing so is difficult and time consuming and system defaults look better and provide a more consistent experience.
We demand pixel-perfect accuracy at work between design and code – but the developers must approve the designs first to confirm that it's achievable.
I guess it comes down to what we define as 'pixel perfect'.
Continue this thread — 3 replies
It absolutely is possible—we used to do it all the time back in the table layout days—but it is not desirable. They are inherently fragile (often users can break them just by changing the default text size), and they do not adapt to different screen sizes very well.
It's... actually incredibly easy to do that. You just disable the browser's default styles for those elements and then set their height explicitly.
There is a certain interpretation of "pixel perfect" which, I agree, isn't really feasible or worth trying to achieve, and that's when it comes to layout. You can hardcode the dimensions of "atomic" elements like buttons and inputs all you want, but if you start giving hard pixel widths to containers that should be adjusting their size according to their content and surroundings, you're going to get yourself into trouble real fast. I think designers these days have mostly started to grasp this, though.
Sure you can. Just see how Bootstrap does it.
It is true that for a long time, input controls were rendered using the native platform controls, so there were are a lot of differences and limited styling capabilities. But these days all browsers (as far as I am aware) use the browser rendering engine for input controls, and they are fully stylable.
How much further could css have gone on its own had it supported variables and functions from the get-go?
(If you are old enough, you may have come across one of those WordPress templates in which they crammed PHP $variables inside inlined CSS. #goodolddays)
1) Nested selectors
2) Imports
3) Variables
#3 is now solved/being solved natively, but I couldn't live without #1 and I'm not really sure why it hasn't been added natively yet
You have a shitload of display sizes to support on the other hand and strange behaving browsers which aren't truly standardized on the other. That makes adjusting your CSS to all of these a pain in the ass even if you like fiddling around with styling (as I basically do).
I don't want to adjust my margins, paddings and fonts and then restart from scratch because f Apple decided they will do everything in Safari completely different from the rest of the browsers just to show how special they are.
I'm also sick of building selenium grids with a mac mini in them just to also test this piece of ... "ingenuity" ;-)
So nowadays I just go with a framework with sane defaults and no-hassle usability and when I need something special I ask people who don't do anything but CSS the whole day.
Without more standardization it's not a domain you'll be great at when you don't specialize in it - which means you got to invest a lot of time to master it.
Continue this thread — 1 more reply
Continue this thread — 4 more replies