205-10

Attribute selectors

Slide instructions

SPACEBAR to move forward through slides.

SHIFT & SPACEBAR to move backwards through slides.

LEFT ARROW & RIGHT ARROW to move through sections.

ESC to see overview and ESC again to exit.

F to enter presentation mode and ESC to exit.

Introduction

Attribute selectors are used to select elements based on their attributes or their attribute and values.

[att] attribute selector (CSS2)

The [att] attribute selector is written as "E[att]".

The "E" is for an optional element.

The "att" is for the relevant attribute.


/* syntax */
E[att] { }

/* example */
input[required] { }

How does it work?

The [att] attribute selector targets any element with the relevant attribute.

In the following example, any <input> element with a required attribute will be selected.


/* CSS selector */
input[required] { }

<!-- HTML markup -->
<input type="text" required>
<input type="text">

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

The [att] attribute selector is not supported by IE6 or IE7.

Exercise 1: Write an [att] selector

Open this HTML file in an editor:
exercises-start/exercise-205-10-01.html

Write a CSS rule to style any <input> with a boolean required attribute. Set with border: 1px solid red.


/* Add styles here */
input[required] { 
  border: 1px solid red;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-10-01.html

[att=val] attribute selector (CSS2)

The [att=val] attribute selector is written as "E[att=value]".

The "E" is for an optional element.

The "att" is for the relevant attribute.

The "val" is for the relevant attribute value which can be placed inside single, double or no quotes.


/* syntax */
E[att=val] { }
E[att='val'] { }
E[att="val"] { }

/* example */
p[class="blue"] { }

How does it work?

The [att=val] attribute selector targets any element with the relevant attribute value.

In the following example, any <p> with a class of blue will be selected. Multiple values and hyphen-separated values will not be selected.


/* CSS selector */
p[class="blue"] { }

<!-- HTML markup -->
<p class="blue"></p>
<p class="blue red"></p>
<p class="red blue"></p>
<p class="blue-new"></p>
<p class="new-blue"></p>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

The [att=val] attribute selector is not supported by IE6.

Exercise 2: Write an [att=val] selector

Open this HTML file in an editor:
exercises-start/exercise-205-10-02.html

Write a CSS rule to style any <p> element with a class value of one. Set to color: burlywood.


/* Add styles here */
p[class="one"] {
  color: burlywood;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-10-02.html

[att~=val] attribute selector (CSS2)

The [att~=val] attribute selector is written as "E[att~=value]".

The "E" is for an optional element.

The "att" is for the relevant attribute.

The "val" is for the relevant attribute value which can be placed inside single, double or no quotes.

The "~" symbol after the "att" means the value must be "space-separated".


/* syntax */
E[att~=val] { }
E[att~='val'] { }
E[att~="val"] { }

/* example */
p[class~="blue"] { }

How does it work?

The [att~=val] attribute selector targets any space-separated instances of a value.

In the following example, any <p> element with a class value of blue, including space separated instances, will be selected.


/* CSS selector */
p[class~="blue"] { }

<!-- HTML markup -->
<p class="blue"></p>
<p class="blue red"></p>
<p class="red blue"></p>
<p class="blue-new"></p>
<p class="new-blue"></p>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

The [att~=val] attribute selector is not supported by IE6.

Exercise 3: Write an [att~=val] selector

Open this HTML file in an editor:
exercises-start/exercise-205-10-03.html

Write a CSS rule to style any <p> element with a class value of one. Set to color: palevioletred.

Also, you need to style any space-separated instances of the class value of one.


/* Add styles here */
p[class~="one"] {
  color: palevioletred;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-10-03.html

[att|=val] attribute selector (CSS2)

The [att|=val] attribute selector is written as "E[att|=value]".

The "E" is for an optional element.

The "att" is for the relevant attribute.

The "val" is for the relevant attribute value which can be placed inside single, double or no quotes.

The "|" symbol after the "att" means the value must be "hyphen-separated".


/* syntax */
E[att|=val] { }
E[att|='val'] { }
E[att|="val"] { }

/* example */
p[class|="blue"] { }

How does it work?

The [att|=value] attribute selector targets any element with an attribute value that matches, including hyphen-separated values.

In the following example, any <p> element with a class value of blue or blue- will be selected.


/* CSS selector */
p[class|="blue"] { }

<!-- HTML markup -->
<p class="blue"></p>
<p class="blue red"></p>
<p class="red blue"></p>
<p class="blue-new"></p>
<p class="new-blue"></p>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

The [att|=val] attribute selector is not supported by IE6.

Exercise 4: Write an [att|=val] selector

Open this HTML file in an editor:
exercises-start/exercise-205-10-04.html

Write a CSS rule to style any <p> element with a class value of one. Set to color: mediumspringgreen.

Also, you need to style any hyphen-separated instances of the class value of one.


/* Add styles here */
p[class|="one"] {
  color: mediumspringgreen;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-10-04.html

[att^=val] attribute selector (CSS2)

The [att^=val] attribute selector is written as "E[att^=value]".

The "E" is for an optional element.

The "att" is for the relevant attribute.

The "val" is for the relevant attribute value which can be placed inside single, double or no quotes.

The "^" symbol after the "att" means the value must "start with the relevant string".


/* syntax */
E[att^=val] { }
E[att^='val'] { }
E[att^="val"] { }

/* example */
a[href^="http"] { }

How does it work?

The [att^=val] attribute selector targets any element whose attribute starts with the value.

In the following example, any <a> element with an href that starts with http will be selected.


/* CSS selector */
a[href^="http"] { }

<!-- HTML markup -->
<a href="http://abc.com"></a>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

The [att^=val] attribute selector is not supported by IE6.

Exercise 5: Write an [att^=val] selector

Open this HTML file in an editor:
exercises-start/exercise-205-10-05.html

Write a CSS rule to style any <a> element with href value that starts with http. Set to color: cyan.


/* Add styles here */
a[href^="http"] {
  color: cyan;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-10-05.html

[att$=val] attribute selector (CSS2)

The [att$=val] attribute selector is written as "E[att$=value]".

The "E" is for an optional element.

The "att" is for the relevant attribute.

The "val" is for the relevant attribute value which can be placed inside single, double or no quotes.

The "$" symbol after the "att" means the value must "end with the relevant string".


/* syntax */
E[att$=val] { }
E[att$='val'] { }
E[att$="val"] { }

/* example */
a[href$=".pdf"] { }

How does it work?

The [att$=val] attribute selector targets any element whose attribute ends with the value.

In the following example, any <a> element with an href that ends with .pdf will be selected.


/* CSS selector */
a[href$=".pdf"] { }

<!-- HTML markup -->
<a href="form.pdf></a>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

The [att$=val] attribute selector is not supported by IE6.

Exercise 6: Write an [att$=val] selector

Open this HTML file in an editor:
exercises-start/exercise-205-10-06.html

Write a CSS rule to style any <a> element with href value that ends with .pdf. Set to color: mediumorchid.


/* Add styles here */
a[href$=".pdf"] {
  color: mediumorchid;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-10-06.html

[att*=val] attribute selector (CSS2)

The [att*=val] attribute selector is written as "E[att*=value]".

The "E" is for an optional element.

The "att" is for the relevant attribute.

The "val" is for the relevant attribute value which can be placed inside single, double or no quotes.

The "*" symbol after the "att" means the value must "contain the relevant string".


/* syntax */
E[att*=val] { }
E[att*='val'] { }
E[att*="val"] { }

/* example */
a[href*="abc.com"] { }

How does it work?

The [att*=val] attribute selector targets any element whose attribute value matches a string.

In the following example, any <a> element with an href value with a string of abc.com will be selected.


/* CSS selector */
a[href*="abc.com"] { }

<!-- HTML markup -->
<a href="http://www.abc.com></a>

Specifications

W3C Selectors Level 4
Status: Working Draft

Selectors Level 3
Status: Recommendation

CSS Level 2 (Revision 1)
Status: Recommendation

Support

The [att*=val] attribute selector is not supported by IE6.

Exercise 7: Write an [att*=val] selector

Open this HTML file in an editor:
exercises-start/exercise-205-10-07.html

Write a CSS rule to style any <a> element with href value that contains the string example.com. Set to color: mediumpurple.


/* Add styles here */
a[href*="example.com"] {
  color: mediumpurple;
}

Check your work against the finished HTML file:
exercises-finished/exercise-205-10-07.html

Russ Weakley

Site Twitter Github Slideshare Linkedin

Next deck

206: CSS Inheritance