What do square brackets in class names mean?

Html

Html Problem Overview


I saw here square brackets that are used in class names:

<input class="validate[required,custom[onlyLetter],length[0,100]]" name="firstname" type="text" />

What do these square brackets mean?

Html Solutions


Solution 1 - Html

The square brackets are used as an attribute selector, to select all elements that have a certain attribute value. In other words, they detect attribute presence.

Example 1:

img[alt="picName"] {width:100px;}

would affect only

<img src="picName.png" alt="picName" />

in your code, and won't affect

<img src="picName.png" alt="picName2" />

Example 2:

The following affects all elements with title attribute specified:

[title] {border:1px dotted #333;}

Example 3:

This CSS

p[class~="fancy"]

will affect the following html

<p class="fancy">Hello</p>
<p class="very fancy">Hola</p>
<p class="fancy maybe">Aloha</p>

but won't affect this:

<p class="fancy-fancy">Privet</p>

Example 4:

[lang|="en"]

will affect elements with lang attribute, which is hyphen-separated list of words beginning with “en”, like

<div lang="en">Tere</div>
<div lang="en-gb">GutenTag</div>

Examples 5, 6, 7:(CSS3)

The following attribute selector affects link elements whose href attribute value starts with the string “http:”.

a[href^="http:"]

The following attribute selector affects image elements whose src attribute values ends with the string “.png”.

img[src$=".png"]

The following attribute selector affects any input element whose name attribute value contains the string “choice”.

input[name*="choice"]

Solution 2 - Html

That is most likely used by some sort of validator or validation library. The class here means that validate this field denoted by validate keyword and then:

required it is required field
custom validation type; allow only letters
length should be between 0 to 100 chars

Well, this information is used by the jQuery validation library you posted the link to :)

Solution 3 - Html

Apart from the use-case / example given by the OP for brackets in class names, there is also another use case which Harry Roberts proposed (and later stopped proposing) in his blog a while back: grouping related classes in your markup where the square brackets could be used to group

> two or more related class attributes to make them easier to notice > when scanning an HTML file > > ... > > and that looks something like this:

<div class="[ foo  foo--bar ]  baz">

where:

> - There must be more than one ‘set’ of classes. > - One ‘set’ must contain more than one class.

He also noted that adding the brackets is completely valid according to the html5 spec

> There are no […] restrictions on the tokens authors can use in the > class attribute…

Just to reiterate:

The brackets in the class attributes - while being valid CSS class names are not actually meant to be used in the CSS1 - but rather to help readability in the HTML.


Notes:

1 Although technically, they can be used when escaped,

.\[ {
  color: red;
}

<div class="[">Hi there</div>

Solution 4 - Html

Nothing. Brackets are a legal character for class names with no special meaning whatsoever.

Solution 5 - Html

In standard HTML, they have no particular meaning. It's just more text.

To the jQuery Validation plugin, they do.

Solution 6 - Html

Example:

[what-ever] {
    color: red;
}

<p what-ever>Hello</p>


This will color Hello red. You can use square-bracket as class names

Solution 7 - Html

There is no particular rule within a class name. In your example they are almost certainly being used by a JavaScript validation framework. This is because in HTML you can't simply add your own attributes to tags, therefore the validation framework is exploiting the fact that CSS class names can contain such characters in order to 'store' the validation rules within the class name. There won't actually be any corresponding class in the style-sheet - this is just a trick to work around the limitations of HTML.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMisha MoroshkoView Question on Stackoverflow
Solution 1 - HtmlbogatyrjovView Answer on Stackoverflow
Solution 2 - HtmlSarfrazView Answer on Stackoverflow
Solution 3 - HtmlDanieldView Answer on Stackoverflow
Solution 4 - HtmlJörg W MittagView Answer on Stackoverflow
Solution 5 - HtmlJames CurranView Answer on Stackoverflow
Solution 6 - HtmlmortenlundView Answer on Stackoverflow
Solution 7 - HtmlDan DiploView Answer on Stackoverflow