[attr] selector

Not supported by Explorer 6-.

Selects elements with a certain attribute or an attribute with a certain value.

Testsheet:

p[class] {color: #6374AB}
p[ppk] {background-color: red}
p[align=right] {border: 1px solid black;}
p[ppk=false] {font-weight: 600;}
p[class~=test] {text-decoration: underline;}

Attribute presence

p[class] {color: #6374AB}

Each element p with an attribute class—the value of the attribute doesn't matter.

This paragraph has class="testclass". It should be blue.

p[ppk] {background-color: red}

Each element p with an attribute ppk—the value of the attribute doesn't matter..

This paragraph has a ppk="true" attribute. It should have a red background.

Attribute value

The p[align=right] selector means "each p that has an ALIGN attribute with value "right". The p[class~=test] selector means "each p that has a CLASS one of whose values is "test".

p[align=right] {border: 1px solid black;}

Each p with an attribute align="right".

This paragraph has align="center". Nothing should happen.

This paragraph has align="right". It should have a border.

p[ppk=false] {font-weight: 700;}

Each p with an attribute ppk="false".

This paragraph has a ppk="false" attribute. It should be bold and have a red background.

The previous paragraph with a ppk attribute didn't have the value "false" so it should not become bold.

p[class~=test] {text-decoration: underline;}

Each p with an attribute class, one of whose values is "test". The values of the attribute should be separated by spaces.

This paragraph has a class="small test" attribute. It should be blue and underlined.

The blue colour comes from the previous rule p[class] {color: #6374AB}, which obviously also works on this paragraph, since it has a class.