Sunday 15 September 2013

Complex Selectors - Part3

Sibling Selectors Overview

ExampleClassificationExplanation
h2 ~ pGeneral Sibling SelectorSelects an element that follows anywhere after the prior element, in which both elements share the same parent
h2 + pAdjacent Sibling SelectorSelects an element that follows directly after the prior element, in which both elements share the same parent

Attribute Selectors

Some of the common selectors looked at early may also be defined as attribute selectors, in which an element is selected based up on its class or ID value. These class and ID attribute selectors are widely used and extremely powerful but only the beginning. Other attribute selectors have emerged over the years, specifically taking a large leap forward with CSS3. Now elements can be selected based on whether an attribute is present and what it’s value may contain.

Attribute Present Selector

The first attribute selector identifies an element based on whether it includes an attribute or not, regardless of any actual value. To select an element based on if an attribute is present or not simply include the attribute name in square brackets, [], within a selector. The square brackets may or may not follow any qualifier such as an element type or class, all depending on the level of specificity desired.
CSS
  1. a[target] {...}
HTML
  1. <a href="#" target="_blank">...</a>

Attribute Equals Selector

To identify an element with a specific, and exact matching, attribute value the same selector from before may be used, however this time inside of the square brackets following the attribute name, include the desired matching value. Inside the square brackets should be the attribute name followed by an equals sign, =, quotations, "", and inside of the quotations should be the desired matching attribute value.
CSS
  1. a[href="http://google.com/"] {...}
HTML
  1. <a href="http://google.com/">...</a>

Attribute Contains Selector

When looking to find an element based on part of an attribute value, but not an exact match, the asterisk character, *, may be used within the square brackets of a selector. The asterisk should fall just after the attribute name, directly before the equals sign. Doing so denotes that the value to follow only needs to appear, or be contained, within the attribute value.
CSS
  1. a[href*="login"] {...}
HTML
  1. <a href="/login.php">...</a>

Attribute Begins With Selector

In addition to selecting an element based on if an attribute value contains a stated value, it is also possible to select an element based on what an attribute value begins with. Using a circumflex accent, ^, within the square brackets of a selector between the attribute name and equals sign denotes that the attribute value should begin with the stated value.
CSS
  1. a[href^="https://"] {...}
HTML
  1. <a href="https://chase.com/">...</a>

Attribute Ends With Selector

Opposite of the begins with selector, there is also an ends with attribute selector. Instead of using the circumflex accent, the ends with attribute selector uses the dollar sign, $, within the square brackets of a selector between the attribute name and equals sign. Using the dollar sign denotes that the attribute value needs to end with the stated value.
CSS
  1. a[href$=".pdf"] {...}
HTML
  1. <a href="/docs/menu.pdf">...</a>

Attribute Spaced Selector

At times attribute values may be spaced apart, in which only one of the words needs to be matched in order to make a selection. In this event using the tilde character, ~, within the square brackets of a selector between the attribute name and equals sign denotes an attribute value that should be whitespace-separated, with one word matching the exact stated value.
CSS
  1. a[rel~="tag"] {...}
HTML
  1. <a href="#" rel="tag nofollow">...</a>

Attribute Hyphenated Selector

When an attribute value is hyphen-separated, rather than whitespace-separated, the vertical line character, |, may be used within the square brackets of a selector between the attribute name and equals sign. The vertical line denotes that the attribute value maybe hyphen-separated however the hyphen-separated words must begin with the stated value.
CSS
  1. a[lang|="en"] {...}
HTML
  1. <a href="#" lang="en-US">...</a>

Attribute Selectors Example

HTML
  1. <ul>
  2. <li><a href="#.pdf" title="PDF Document">PDF Document</a></li>
  3. <li><a href="#.doc" title="Word Document">Word Document</a></li>
  4. <li><a href="#.jpg" title="Image File">Image File</a></li>
  5. <li><a href="#.mp3" title="Audio File">Audio File</a></li>
  6. <li><a href="#.mp4" title="Video File">Video File</a></li>
  7. </ul>
CSS
  1. ul {
  2. list-style: none;
  3. }
  4. ul a {
  5. padding-left: 22px;
  6. }
  7. ul a[href$=".pdf"] {
  8. background: url("images/pdf.png") 0 50% no-repeat;
  9. }
  10. ul a[href$=".doc"] {
  11. background: url("images/doc.png") 0 50% no-repeat;
  12. }
  13. ul a[href$=".jpg"] {
  14. background: url("images/image.png") 0 50% no-repeat;
  15. }
  16. ul a[href$=".mp3"] {
  17. background: url("images/audio.png") 0 50% no-repeat;
  18. }
  19. ul a[href$=".mp4"] {
  20. background: url("images/video.png") 0 50% no-repeat;
  21. }

Demo

Attribute Selectors Overview

ExampleClassificationExplanation
a[target]Attribute Present SelectorSelects an element if the given attribute is present
a[href="http://google.com/"]Attribute Equals SelectorSelects an element if the given attribute value exactly matches the value stated
a[href*="login"]Attribute Contains SelectorSelects an element if the given attribute value contains at least once instance of the value stated
a[href^="https://"]Attribute Begins With SelectorSelects an element if the given attribute value begins with the value stated
a[href$=".pdf"]Attribute Ends With SelectorSelects an element if the given attribute value ends with the value stated
a[rel~="tag"]Attribute Spaced SelectorSelects an element if the given attribute value is whitespace-separated with one word being exactly as stated
a[lang|="en"]Attribute Hyphenated SelectorSelects an element if the given attribute value is hyphen-separated and begins with the word stated