To add a CSS class to an element in Pug, append it with dot notation directly to the tag (p.intro) or put it in a class attribute (p(class="intro")); both compile to HTML with that class. The element can be anything—a paragraph, a div, a link, even a Pug include target is not affected, because classes belong to emitted HTML elements, not to files.

This answer treats “Pug” as the JavaScript template engine (formerly Jade), not the dog breed. That matters because the source category mentions dog breeds, but the actual query is about markup syntax: a pug dog has no class attribute. The short version is that Pug offers several interchangeable ways to attach classes, and the best one depends on whether the class is static, conditional, or supplied as a variable.

The three core ways to add a class

Dot shorthand on the tag

Pug lets you write a class immediately after the tag name, prefixed by a dot:

That renders as:

Multiple dots chain multiple classes. This is the cleanest form when the class names are known while you are writing the template. Note that a bare .intro with no tag defaults to a div, so .intro and div.intro are equivalent.

The class attribute

The explicit attribute form works the same way and reads more like HTML:

It is slightly more verbose, but it pays off when the value comes from a variable or when you need other attributes alongside it and want them grouped visually:

Here bodyClass is a JavaScript expression evaluated when the template renders.

An array of class names

Pug accepts an array in the class attribute, which is useful for composing classes from several sources:

The array items are joined with spaces in the output. This is the form to reach for when the class list is assembled dynamically—for example, a base component class plus a variant variable.

Mixing static and dynamic classes

A frequent real need is “always this class, plus one chosen at render time.” In Pug you can combine the shorthand and the attribute:

Both btn and the chosen class end up on the element. Order in the output follows the order in which Pug encounters them, so the result is class="btn btn-primary". The reverse arrangement — button(class=...) with a dot elsewhere — is not valid syntax; the dot must stay attached to the tag.

If the class expression evaluates to undefined or null, Pug omits that entry rather than printing the literal word. That behavior is convenient but easy to rely on accidentally: a typo in a variable name produces a silently missing class, not an error.

Conditional classes without ternary clutter

For a single toggle, a ternary is fine. For several independent conditions, Pug’s class attribute also accepts an object whose keys are class names and whose values are truthiness tests:

Only the keys whose values are truthy are emitted. This keeps a list of toggles readable and avoids nested ternaries. It is worth knowing that this object form is a Pug convenience, not an HTML feature; it is resolved at compile or render time and disappears from the output.

Objects and arrays can be combined, and the resulting classes are flattened. That makes it practical to build a small helper that returns an object of flags and spread it in—though spreading syntax and its availability depend on the Pug version you are using, so check your installed version’s documentation before relying on it.

Classes on elements that are not tags

Two cases trip people up.

First, mixin calls. If you define a mixin, classes you pass as attributes are handled by the mixin’s own &attributes logic, not automatically by Pug. Writing +card.card--wide passes class into the mixin’s attributes, but the mixin must forward them:

Without &attributes(attributes), a class supplied at the call site is dropped. This is the most common reason someone says “adding a class in Pug didn’t work.”

Second, include and extends. These operate on template files, not HTML elements, so there is no element to attach a class to. If an included partial needs a class on its root element, the partial itself must be written to accept it—typically via a variable or a mixin.

Interpolation and computed class names

Pug supports interpolation inside attribute values, so p(class="item-#{index}") produces item-0, item-1, and so on. Interpolation is textual substitution and takes whatever the expression yields, so be deliberate about what can end up in the string. Class names are not a security boundary, but building them from untrusted input still invites surprising output.

For static suffixes, plain string concatenation outside Pug is often clearer than interpolation. For a genuinely dynamic list, the array form usually reads better than string building.

What “class” means here, and where the ambiguity lies

“Class” has at least three meanings that could collide in a question phrased this loosely:

CSS/HTML class: the attribute discussed above. This is almost certainly what the query means, since Pug is a template language that emits HTML. JavaScript class: Pug templates can contain JavaScript expressions and unbuffered code, but Pug has no construct for declaring a JavaScript class in markup. You would define the class in a script and reference it—or its instances—in attributes or interpolated content. A conceptual category such as a dog breed “class” (herding, toy, working). That reading belongs to the source category, not to Pug syntax, and there is no mapping between breed groups and template syntax.

Only the first reading yields a concrete, stable answer. The others do not change the markup mechanics.

How Pug emits classes: whitespace, duplicates, and ordering

Pug does not deduplicate class names. If you write p.a(class="a"), the output is class="a a". In HTML this is harmless—browsers effectively treat it as one class—but it is noise, and some linters flag it. If you are merging class lists from several sources, do the deduplication yourself or accept the redundancy.

Whitespace inside the class string is preserved as written, and multiple spaces collapse visually in the attribute but remain in the source. Pug does not trim or normalize class lists for you.

Attribute order in the output generally follows the order in Pug processes the element, with shorthand classes appearing where the dot sits relative to other attributes. You should not write templates that depend on a specific attribute order in the output, because HTML does not give attribute order meaning and different Pug versions may order things differently.

Common mistakes and how they present

Using a dot on something that is not a tag. .foo is fine because it implies a div. But #{x}.foo is not a valid tag-with-class form; the parser will not treat the dot as a class suffix there. Build such cases with an explicit class attribute.

Putting the class in the wrong place for a mixin. As noted, classes passed to a mixin only appear if the mixin forwards its attributes. A missing class on rendered output is usually this, not a Pug bug.

Expecting conditional shorthand. There is no p.active(if=...) syntax. Conditions go in the attribute expression, not in the dot shorthand.

Confusing class with id. They are separate attributes and separate shorthand: . for class, # for id. p#main.a gives an element with id main and class a.

Assuming a Pug version’s behavior carries across versions. Pug 2 and Pug 3 differ in some edge cases around attribute handling and syntax support. When a form that “should work” fails, the installed version is a legitimate suspect.

When this answer stops applying

This is a language-syntax answer, so it holds across Pug projects in the general case. It stops being a complete answer when the real question is about styling—which CSS framework to use, how specificity resolves, or whether a class will visually apply given cascade rules. It also stops applying to framework-specific wrappers, such as templating layers that compile to Pug or extend it with custom attributes; those may add their own class conventions that override the plain Pug behavior described here.

A concrete boundary: p.intro guarantees an intro class in the HTML string. It does not guarantee that any stylesheet defines .intro, that the class survives minification or a CSS-modules transform, or that a component library will recognize it. Those are separate concerns, and conflating them is the usual source of confusion when a class “doesn’t work.”

The safest habit is to write the shortest form that expresses the class’s origin: dot shorthand for fixed classes, class attribute for variables, arrays for composed lists, objects for toggles, and &attributes in mixins that must accept caller-supplied classes.