Part 2 of 5

HTML on its own

What HTML can do with no help at all: structure, meaning, and more built-in behaviour than its reputation suggests. And what it plainly cannot do.

Start with the half of the pair that takes the blame. This part looks at HTML with no CSS and no script: what it does, what it can't, and the arguments people make about it. Then we score it against the four tests from part 1.

More than marking up text

The usual picture of HTML is of tags wrapped round text: this is a heading, that is a paragraph, this bit is bold. That was roughly true in 1993. Today's HTML describes the whole structure of an application's screen: navigation, sections and articles, images and video, tables, forms with a dozen kinds of input, and a growing set of controls with behaviour built in.

Each element means something specific, and every browser agrees on what. Write <button> and you get something that can be focused, pressed with a keyboard, announced by a screen reader and used to send a form, without saying how any of that should happen.

Try it: no script required

Here are four things HTML can do on its own. We've styled them to match the site, but the behaviour is all HTML: switch off the site's CSS and every one of them still works.

A section that opens and closes

What is <details>?

An element the browser shows folded, with its summary as a button. Click the summary and it opens; click again and it closes. The browser remembers which state it's in.

Only one open at a time

Turing

Described his imaginary machine in 1936.

Berners-Lee

Proposed the web at CERN in 1989, and HTML with it.

Eich

Wrote the first version of JavaScript in 1995, reportedly in ten days.

Giving the three the same name tells the browser they're a group, so opening one closes the others. That's a rule, and the browser enforces it.

A pop-up

This panel uses the popover attribute. Pressing Escape or clicking anywhere else closes it, and the browser handles all of that.

A form that checks itself

The browser checked both fields and let the form through. (This message is shown by CSS, which notices the page's address ends in #accepted; we come to CSS in part 3.)

The ticket code must match the pattern [A-Z]{3}-[0-9]{4}, a regular expression the browser tests your input against. Until both fields are valid, the browser refuses to send the form and tells you why.

None of that is decoration. The browser is holding state (which section is open), applying rules (only one at a time; the code must match the pattern) and refusing to go on until they're met. The question is whether that amounts to programming.

The case for

The strongest case for HTML comes from Professor David Brailsford of the University of Nottingham, in a Computerphile video. He describes HTML as a very restricted kind of programming language, and the restriction he means is that it's declarative rather than imperative: it states what should result, not the steps to get there.

Seen that way, each element is a little like a function call. <h1> asks the browser to draw a heading, attributes are its arguments (<img src="…" width="300">), and putting one element inside another combines them, the way functions are combined in a functional language. The browser is the interpreter that evaluates the whole thing into the page you see.

Answering on Stack Overflow, Chris Pietschmann put the broadest version simply:

Any instruction that tells the computer to do something is a programming language.

Chris Pietschmann, Stack Overflow

The case against

The case against is just as easy to state. Ben Romy, writing for Syracuse University's iSchool, lists what HTML doesn't have:

HTML contains no programming logic. It doesn't have common conditional statements such as If/Else. It can't evaluate expressions or do any math. […] You can't declare variables and you can't write functions. […] Think of it this way: you can't compute the sum of 2 + 2 in HTML; that's not what it's for.

Ben Romy, Syracuse University iSchool

On its own terms, that's right. HTML has no variables (and no, the <var> element, which marks up the name of a variable in text, doesn't count). It can't add two numbers, repeat a step, or let you define a piece of behaviour once and reuse it. It can take input through forms, but it can't do anything with that input: it can only check it against fixed rules and hand it to a server, a script or a stylesheet.

Hold on to "you can't compute the sum of 2 + 2", though. In part 4, HTML and CSS do exactly that.

Four arguments, tested

Some arguments come up whenever this debate does. Here's how they hold up.

"If HTML is a programming language, so are XML and JSON"

Not quite. XML is HTML's sibling, but it has no vocabulary of its own: you invent the elements, and each program that reads them decides what they mean. JSON is plain data. HTML is different because its elements have fixed, published meanings that every browser carries out in the same way. That's what an interpreter does with a language, and it's why a browser is sometimes compared to a very limited virtual machine. HTML's case may be weak, but XML's and JSON's are weaker.

"If HTML is a programming language, so is a Word document"

A Word document is stored as XML too, and Word does have heading styles. But that XML is written by Word, for Word: it's a record of formatting and layout, with no shared vocabulary that other programs carry out in the same way, and nobody writes it by hand. HTML is written by people every day, in a published language that every browser carries out alike. The comparison doesn't hold.

"If HTML were a programming language, we wouldn't need JavaScript"

JavaScript arrived in 1995, written by Brendan Eich at Netscape. Rather than add logic to HTML, browsers gained a separate language for it, and there were good reasons. Logic written as markup would be painfully wordy. Compare a short JavaScript function that lists the Fibonacci numbers:

function fibonacci(count) {
    const numbers = [0, 1];

    while (numbers.length < count) {
        numbers.push(numbers.at(-2) + numbers.at(-1));
    }

    return numbers;
}

with the same thing in an imaginary HTML-style syntax:

<function name="fibonacci">
    <parameter name="count" type="int" />
    <variable name="numbers" value="[0, 1]" />
    <while>
        <less-than left="numbers.length" right="count" />
        <call function="numbers.push">
            <add>
                <item of="numbers" index="-2" />
                <item of="numbers" index="-1" />
            </add>
        </call>
    </while>
    <return value="numbers" />
</function>

Nobody would choose to write that. Interestingly, it's close to how a compiler sees code internally, as a tree of nested operations, but that's exactly why we write code as text and leave the tree to the machine. The existence of JavaScript shows that logic was better kept out of HTML, not that HTML can't be a programming language.

"The browser does all the real work"

True: HTML relies on the browser to turn its descriptions into a page. But every language relies on something to run it. A Python script does nothing without the Python interpreter, and the interpreter relies on the processor. Pointing out that HTML needs a browser tells us how it runs, not whether it's a programming language. The fairer question is how much a language lets you express, and on its own, HTML lets you express very little logic.

How HTML scores

Against the four tests from part 1, HTML on its own comes out like this:

HTML against the four tests
Test HTML Why
Computes anything No It has no way to repeat a step, hold a value or calculate, so it can't work out anything at all.
Gives instructions Yes Every element is an instruction the browser carries out: draw a heading, show a form, play a video.
Decides and remembers Partly It holds state, such as a ticked box or an open section, and the browser makes a few fixed decisions about it, but you can't write decisions of your own.
Builds programs No On its own it makes documents and forms, not things that do a job by themselves.

So HTML passes the loosest test, fails the strictest, and only just gets a foot in the door of the middle one. It can hold state, but it can't make choices of its own about it. That's where CSS comes in: it can read the state HTML holds, and decide what happens next.