Here’s a great thread by Kevin Powell that’s making the rounds. He believes so many folks see CSS as a frustrating and annoying language:
Why do people respect JavaScript or other languages enough to learn them inside-out, and yet constantly dunk on CSS? Well, all this reminds me of what Jeremy Keith wrote a while back when he argued that CSS is simple, but not easy:
Unlike a programming language that requires knowledge of loops, variables, and other concepts, CSS is pretty easy to pick up. Maybe it’s because of this that it has gained the reputation of being simple. It is simple in the sense of “not complex”, but that doesn’t mean it’s easy. Mistaking “simple” for “easy” will only lead to heartache.
I think that’s what’s happened with some programmers coming to CSS for the first time. They’ve heard it’s simple, so they assume it’s easy. But then when they try to use it, it doesn’t work. It must be the fault of the language, because they know that they are smart, and this is supposed to be easy. So they blame the language. They say it’s broken. And so they try to “fix” it by making it conform to a more programmatic way of thinking.
There have been times where I’ve sat down with engineers to pair with them about a tricky layout issue and the CSS is treated as being beneath them — as if the language is somehow too unpredictable to warrant learning and mastering. Perhaps this has something to do with the past, where we’ve spent years fighting the way browsers render things differently. But this is mostly a solved problem. I can’t remember the last time I fought against browsers like that.
Instead, I reckon the biggest issue that engineers face — and the reason why they find it all so dang frustrating — is that CSS forces you to face the webishness of the web. Things require fallbacks. You need to take different devices into consideration, and all the different ways of seeing a website: mobile, desktop, no mouse, no keyboard, etc. Sure, you have to deal with that when writing JavaScript, too, but it’s easier to ignore. You can’t ignore the layout of your site being completely broken on a phone.
Side note: We have a guide to centering in CSS not because CSS is busted and dumb, but because there are so many variables to the extent that a simple question like, “How do I center text?” is actually much more complicated than it appears. There’s so much context that’s missed.
This reminds me of one of my favorite blog posts of all time, where Brandon Smith argues that CSS is awesome and we should respect the language and learn how it works under the hood:
CSS is hard because its properties interact, often in unexpected ways. Because when you set one of them, you’re never just setting that one thing. That one thing combines and bounces off of and contradicts with a dozen other things, including default things that you never actually set yourself.
One rule of thumb for mitigating this is, never be more explicit than you need to be. Web pages are responsive by default. Writing good CSS means leveraging that fact instead of overriding it. Use percentages or viewport units instead of a media query if possible. Use
min-width
instead ofwidth
where you can. Think in terms of rules, in terms of what you really mean to say, instead of just adding properties until things look right. Try to get a feel for how the browser resolves layout and sizing, and make your changes and additions on top of that judiciously. Work with CSS, instead of against it.
”CSS is hard because its properties interact, often in unexpected ways.” This is the definition of complexity. Good languages limit the ease with which different aspects of state can interfere with each other. Excellent languages make it very difficult.
CSS is hard because it’s too powerful and doesn’t limit side effects very well, making it nearly impossible to reason about.
As someone who can reason about CSS, it really just comes down to a lot of trial and error. Constantly encountering new layout requirements, new content use cases, new device/platform scenarios and combinations, etc., helps you form a sort of mental model of the spirit and philosophy behind CSS in a way that makes it a lot more predictable and manageable. Using architectural guidelines and pre-processors certainly helps though.
I learnt CSS before JS so maybe that makes it easier? CSS doesn’t seem too hard.
Still judging CSS from an imperative programming point of view… that won’t work.
We can discuss all day if CSS is a programming language, but at the end of the day, it’s much more language than programming. It relies primarily on verbal-linguistic intelligence, while programming in the traditional sense of the word is much heavier on the logical-mathematical intelligence.
CSS requires a different mindset, perhaps even a different mind.
CSS like JavaScript, is built for the browser to understand more specific, THE DOM.
Not that the human doesn’t count but our understanding it IS our responsibility.
Just compare a CSS Style Block to a JavaScript Object Literal :: if you apply what it takes to understand the basics of JavaScript, your life will become easier.
`
body, .header {
background: #788;
font-family: ‘Verdana’, sans-serif;
margin: 0;
padding: 0;
display: flex;
}
let address = {
owner: “Bradleys”,
lotNumber: “1313”,
street: “Big Pine Lane”,
city: “My Town”,
country: “USA”,
zip code: “12345”
}
`
This is a VERY in depth article, a good 30 to 45 minute read. If you are starting out or working / now someone who is struggling. It is enlightening and provides so many extra links to further your understanding. OF how the Browser engine and DOM operate. If you see that you get a better perspective why it has to be this way.
” WEB DEVELOPMENT
How the browser renders a web page? — DOM, CSSOM, and Rendering
In this article, we will deep dive into DOM and CSSOM to understand how the browser renders a webpage. ”
https://medium.com/jspoint/how-the-browser-renders-a-web-page-dom-cssom-and-rendering-df10531c9969
I’ve never understood this argument. CSS isn’t hard. It is what it is and you learn to work with it.
Counting (indexing) from 0 IS intuitive.
“Never be more explicit than you need to be”.
That’s good life advice in general!
In most programming languages, isolation is a feature, it’s how things work. But in CSS, leakage is a feature. C is for “Cascading”, meaning defining something somewhere makes it available somewhere else, but not any random places, certain places. If someone doesn’t know what to expect, it’s maybe because this someone doesn’t actually master the language…
Implementations of CSS also have the concept of a user agent style sheet. So you never start out from scratch, but always have to relate to code written by others.
Before starting it is good practice to read and understand the codebase you are adding to. Modern web development tools make this easier than it used to be.
Is it just me or does anyone else have to remind themselves that css elements are treated as boxes?
Before I write any css code, I always keep this in mind.
If you’re used to using any programming language that has a built-in error messages, CSS can be extremely annoying because you have no idea where bugs are or why things aren’t working the way you were hoping. The syntax itself is error tolerant as well so that doesn’t help trouble-shooting.
CSS is hard cus the language doesn’t make any sense, it’s literraly a lot of bs pilled up.
If u have a div which is flex 1 which means it’s take the whole size available, why writing width 100% and height 100% doesn’t make the child same size? i know why, cus css doesn’t make sense.
Why we have to put min-height:0 and height:100% in a div container of a img to avoid img exploding? this doesn’t even make sense and nobody knows they just reapeat the same logic in all css but when i sit down to ask them what’s the logic behind all people just say “i don’t nou dude, i just do this cus it’s works’ so yes css it’s not intuitive.
That’s why programming languages are easy to grasp, cus they make sense.