Square brackets are used as an attribute selector to select all elements that have a specific attribute value. In other words, they detect the presence of attributes.
Example 1:
img[alt="picName"] {width:100px;}
will only affect
<img src="picName.png" alt="picName" />
in your code and will not affect
<img src="picName.png" alt="picName2" />
Example 2:
Listed below are all elements with the specified title attribute:
[title] {border:1px dotted #333;}
Example 3:
This CSS
p[class~="fancy"]
will affect the next html
<p class="fancy">Hello</p> <p class="very fancy">Hola</p> <p class="fancy maybe">Aloha</p>
but will not affect this:
<p class="fancy-fancy">Privet</p>
Example 4:
[lang|="en"]
will affect elements with the lang attribute, which is a hyphenated list of words starting with "en", for example
<div lang="en">Tere</div> <div lang="en-gb">GutenTag</div>
Examples 5, 6, 7: (CSS3)
The following selector attribute affects link elements; the value of the href attribute begins with the string "http:".
a[href^="http:"]
The following selector attribute affects image elements whose src attribute values ββend with the string ".png".
img[src$=".png"]
The following attribute selector affects any input element whose name attribute value contains the string "selection".
input[name*="choice"]
bogatyrjov Aug 14 '10 at 13:37 2010-08-14 13:37
source share