Can class attributes be random

When we create a
<div> or a <nav> element, we also assign a class attribute to it. For example,

<div class="nav-wrapper">

I read that this class attribute is used to style the container at a later stage using CSS. My question is that if this class attribute is used merely for referencing, can it assume any arbitrary names?

For example, can we write <div class="sitepoint"> etc. ?

The answer is “almost any name”… there are a few special character restrictions such as a classname cannot start wtih a digit. class=“column1” is valid, class=“1column” is not.

It is generally recommended that the classname reflect the purpose or function of the element. A classname of “sitepoint” would be functionally very ambiguous :slight_smile:

https://css-tricks.com/semantic-class-names/

https://www.w3.org/QA/Tips/goodclassnames

4 Likes

A class is not a requirement; add them where needed, but it’s perfectly acceptable to have these elements without a class attribute.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.