Formatting and cleaning up markup

Indentation is automatic now. What a formatter leaves behind is the part worth your time.

Formatting is solved and automatic. It is worth knowing precisely what it leaves untouched.

Badly indented markup beside the same markup formatted.
Badly indented markup beside the same markup formatted.

What formatting does

Consistent indentation, sensible line breaks, attributes spaced the same way throughout.

Every editor does this on save, and every language has a formatter that needs no configuration. There is no reason to indent markup by hand and no reason to argue about the style.

It changes nothing about what the page means or does. It makes the file readable.

What it leaves behind

Problem Fixed by formatting
Inconsistent indentation Yes
Long unbroken lines Yes
Four nested containers doing nothing No
Generic elements everywhere No
The same block repeated six times No
Missing alt text No

Generated markup in particular arrives with deep nesting where one element would do, mechanical class names, and the same structure repeated rather than reused.

Formatted, it is neatly indented and just as hard to work with. That is worth knowing before spending an afternoon on the wrong thing.

The cleanup that pays

Replacing generic containers with elements that say what they hold.

A navigation marked as navigation, a main area marked as main, a heading that is actually a heading rather than a large paragraph. It costs nothing at the time of writing and it changes three things.

Search engines understand the page. Assistive technology can navigate it, which is how many readers move through a page. And the next person to open the file, frequently you, can see the structure without reading every line.

Nested generic containers replaced with elements that describe their content.
Nested generic containers replaced with elements that describe their content.

When not to bother

If the page is published once and never opened again, tidy markup is not worth an afternoon. A leaflet page, a one-off announcement, a landing page for an event that has passed.

The test is whether anybody will open the file again. If the answer is no, format it and move on.

Minifying

Removing whitespace from markup and styles reduces the file, and the reduction is small next to a single unoptimised photograph.

If your build does it automatically, fine. Doing it by hand produces a file you can no longer read, in exchange for a saving that would be wiped out by one image exported at the wrong size.

Optimise the images first and the question stops being interesting.

If this is near what you are doing, HTML viewer and How to test a page before publishing cover the cases on either side. Tools for running consulting work is also close.

Put it at an address

Format automatically and never by hand, then look at structure, replace generic containers with elements that describe the content, delete nesting that does nothing, and leave minifying to the build.

Questions people ask

What does a formatter do?

Fixes indentation, line breaks and attribute spacing consistently. It does not change what the markup means.

What does it not fix?

Structure. Nested containers that do nothing, meaningless element choices and duplicated blocks survive formatting and look tidier afterwards.

Is generated markup worth cleaning?

If you will maintain it, yes. If it is published once and never touched, tidy markup is not worth the afternoon.

Should markup be minified for publishing?

The saving is small compared with images. Do it if the build does it automatically; do not do it by hand.

What is the most useful cleanup?

Replacing generic containers with elements that describe what they hold. It helps search engines, assistive technology and the next reader.

Keep reading