-1

The following HTML is right or wrong?

<div class="navbar" class="menu">foo</div>

Do the 2 classes both work?

0

2 Answers 2

5

To specify two classes you can give space between them like

<div class="navbar menu">foo</div>
2

class is not a tag but an attribute. Having multiple attributes of the same name is an error.

[...]; if there is already an attribute on the token with the exact same name, then this is a duplicate-attribute parse error and the new attribute must be removed from the token.

https://html.spec.whatwg.org/multipage/parsing.html#attribute-name-state

Usually browsers will just ignore the second attribute. So in your case, class="menu" is ignored and only the navbar class is applied. To use both classes write class="navbar menu".

Not the answer you're looking for? Browse other questions tagged or ask your own question.