CSS ATTRIBUTE SELECTORS

The CSS attribute selectors provides an easy and powerful way to apply the styles on HTML elements based on the presence of a particular attribute or attribute value.

You can create an attribute selector by putting the attribute—optionally with a value—in a pair of square brackets. You can also place an element type selector before it.

The following sections describe the most common attribute selectors.

CSS [attribute] Selector

This is the simplest form of an attribute selector that applies the style rules to an element if a given attribute exists. For example, we can style all the elements that have a title attribute by using the following style rules:

EXAMPLE

1.	[title] {
2.	    color: blue;
3.	}

The selector [title] in the above example matches all elements that has a title attribute.

You can also restrict this selection to a particular HTML element by placing the attribute selector after an element type selector, like this:

EXAMPLE

1.	abbr[title] {
2.	    color: red;
3.	}

The selector abbr[title] matches only <abbr> elements that has a title attribute, so it matches the abbreviation, but not the anchor elements having title attribute.

CSS [attribute=”value”] Selector

You can use the = operator to make an attribute selector matches any element whose attribute value is exactly equal to the given value:

EXAMPLE

1.	input[type="submit"] {
2.	    border: 1px solid green;
3.	}

The selector in the above example matches all <input> element that has a type attribute with a value equal to submit.

CSS [attribute~=”value”] Selector

You can use the ~= operator to make an attribute selector matches any element whose attribute value is a list of space-separated values (like class="alert warning") , one of which is exactly equal to the specified value:

EXAMPLE

1.	[class~="warning"] {
2.	    color: #fff;
3.	    background: red;
4.	}

This selector matches any HTML element with a class attribute that contains space-separated values, one of which is warning. For example, it matches the elements having the class values warningalert warning etc.

CSS [attribute|=”value”] Selector

You can use the |= operator to make an attribute selector matches any element whose attribute has a hyphen-separated list of values beginning with the specified value:

EXAMPLE

1.	[lang|=en] {
2.	    color: #fff;
3.	    background: blue;
4.	}

The selector in the above example matches all elements that has an Lang attribute containing a value start with en, whether or not that value is followed by a hyphen and more characters. In other words, it matches the elements with lang attribute that has the values enen-USen-GB, and so on but not US-enGB-en.

CSS [attribute^=”value”] Selector

You can use the ^= operator to make an attribute selector matches any element whose attribute value starts with a specified value. It does not have to be a whole word.

EXAMPLE

1.	a[href^="http://"] {
2.	    background: url("external.png") 100% 50% no-repeat;
3.	    padding-right: 15px;
4.	}

The selector in the example above will target all external links and add a small icon indicating that they will open in a new tab or window.

CSS [attribute$=”value”] Selector

Similarly, you can use the $= operator to select all elements whose attribute value ends with a specified value. It does not have to be a whole word.

EXAMPLE

1.	a[href$=".pdf"] {
2.	    background: url("pdf.png") 0 50% no-repeat;
3.	    padding-left: 20px;
4.	}

The selector in the example above select all <a> elements that links to a PDF document and add a small PDF icon to provide hints to the user about the link.

CSS [attribute*=”value”] Selector

You can use the *= operator to make an attribute selector matches all elements whose attribute value contains a specified value.

EXAMPLE

1.	[class*="warning"] {
2.	    color: #fff;
3.	    background: red;
4.	}

This selector in the example above matches all HTML elements with a class attribute that values contains warning. For example, it matches the elements having class values warningalert warningalert-warning or alert_warning etc.

Styling Forms with Attribute Selectors

The attribute selectors are particularly useful for styling forms without class or id:

EXAMPLE

•	input[type="text"], input[type="password"] {
•	    width: 150px;
•	    display: block;
•	    margin-bottom: 10px;
•	    background: yellow;
•	}
•	input[type="submit"] {
•	    padding: 2px 10px;
•	    border: 1px solid #804040;
•	    background: #ff8040;
•	}
Categories: CSS