CSS for JavaScripters 1

I am likely going to write a “CSS for JavaScripters” book, and therefore I need to figure out how to explain CSS to JavaScripters.

Below I take a stab at explaining CSS files as JSON files. What I’d like to know from you is if this comparison makes sense.

If you’re a JavaScripter who’d like to learn some more CSS, please tell me if this helps you understand CSS better or not, and what could be improved. I’d be grateful for your feedback.

If this article generates useful feedback I might do it again. What better way to figure out if you’re making sense than to actually ask the target audience?

I'm writing a CSS book.

CSS as JSON

Suppose your job is to revise a JSON file. This file is sent on to a module that produces a web page. This web page should be changed, and for Reasons the only way to do that is by revising the JSON. You do not have access to the module’s source code, but you have incomplete documentation.

Since JSON is declarative, the order of declarations/properties does not matter. Something like "heroImage": "/images/pngs/hero2.png" can occur anywhere. It’s clear that this property defines the hero image to be shown on the web page, and its exact position in the JSON file does not matter.

Suppose the JSON contains the following:

{
	[...],
	"heroImage": "/images/pngs/hero2.png",
	"heroImage": "/images/pngs/hero3.png",
}

Which hero image will be shown? hero3.png, obviously. The second use of "heroImage" overwrites the first one.

Other properties are much broader and vaguer in their usage. Suppose you find "layout": "sidebar" in the JSON, and you read in the documentation that the values "main" and "footer" are also allowed. The documentation does not make very clear what these values do, so you’re forced to experiment: just change the value of "layout" and see what happens.

There are many more properties like this, that range widely in their effects and aren’t always very clearly documented. The only way to start understanding their purpose is to just try them.

And what if you add "lyout" : "sidebar" to the JSON? Your expected layout won’t materialise — but there’s no error message to alert you to the fact that you’ve made a syntax error. JSON files don’t do error messages — unless the entire file is invalid. That’s not the case here: "lyout" : "sidebar" is perfectly valid JSON. You’ll have to spot the typo by yourself.

This situation resembles web developers creating or revising CSS files. Like JSON files, CSS files are not programs. but a series of declarations that programs use in order to create output. Also, they fail silently when they contain instructions that the receiving program does not understand.

If you approach CSS as you approach JSON you’ve taken a step towards understanding it.

I'm writing a CSS book.

This is the blog of Peter-Paul Koch, web developer, consultant, and trainer. You can also follow him on Twitter or Mastodon.
Atom RSS

If you like this blog, why not donate a little bit of money to help me pay my bills?

Categories: