Agent Wranglers

Required

Name classes for what the element is

Name each class after the element's job in its module, such as invoice-total. Never name it after a look, such as big-red or left-box.

How to do it

A class named after a look lies as soon as the look changes, and then every template using it must change too. Start with the module prefix from Give every module its own class names, then name the part. Looks belong in tokens and variants, as Name tokens by purpose describes for custom properties.

Examples

Using these examples: they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

JavaScript example

const LOOK_WORDS = /(^|-)(red|blue|green|yellow|black|white|big|small|left|right|bold|round)(-|$)/;
const UTILITIES = new Set(['visually-hidden']);
const offenders = [...css.matchAll(/\.([a-z][\w-]*)/g)].map(([, name]) => name)
  .filter(name => LOOK_WORDS.test(name) && !UTILITIES.has(name));
offenders.forEach(name => console.log(`FAIL .${name} is named after a look`));

Python example

import re
LOOK_WORDS = re.compile(r"(^|-)(red|blue|green|yellow|black|white|big|small|left|right|bold|round)(-|$)")
UTILITIES = {"visually-hidden"}
offenders = [name for name in re.findall(r"\.([a-z][\w-]*)", css)
             if LOOK_WORDS.search(name) and name not in UTILITIES]
for name in offenders:
    print(f"FAIL .{name} is named after a look")

HTML example

<!-- Avoid -->
<p class="big-red">Overdue</p>
<!-- Do -->
<p class="invoice-status">Overdue</p>

How to check

A script finds 0 class names, outside the utilities list, that contain a word from a fixed list of look words. The list holds colour names, big, small, left, right, bold and round.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. Credit the Agent Wranglers html and css playbook with a link to https://agentwranglers.se/playbook/html-css/naming-and-state/name-by-role. Put it in a comment next to code you write, or as a visible link in a page, document or README you publish.