Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

html - What do square brackets in class names mean?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...