CSS Attribute Selectors CSS
- CSS Introduction
- CSS Selectors
- How To Add CSS
- CSS Colors
- CSS Backgrounds
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Height and Width
- CSS Outline
- CSS Text
- CSS Fonts
- CSS Links
- CSS Lists
- CSS Tables
- CSS Layout - The display Property
- CSS Layout - CSS position
- CSS Layout - display: inline-block
- CSS Layout - Horizontal & Vertical Align
- CSS Combinators
- CSS Opacity / Transparency
- CSS Dropdowns
- Image Gallery
- CSS Attribute Selectors
- CSS Counters
- CSS Media Queries
CSS Attribute Selectors
CSS [attribute] Selector
The [attribute]
selector is used to select elements with a specified attribute.
The following example selects all <a> elements with a target attribute:
CSS [attribute="value"] Selector
The [attribute="value"]
selector is used to select elements with a specified attribute and value.
The following example selects all <a> elements with a target="_blank" attribute:
CSS [attribute~="value"] Selector
The [attribute~="value"]
selector is used to select elements with an attribute value containing a specified word.
The following example selects all elements with a title attribute that contains a space-separated list of words, one of which is "flower":
CSS [attribute|="value"] Selector
The [attribute|="value"]
selector is used to select elements with the specified attribute starting with the specified value.
The following example selects all elements with a class attribute value that begins with "top":
Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text"!
CSS [attribute^="value"] Selector
The [attribute^="value"]
selector is used to select elements whose attribute value begins with a specified value.
The following example selects all elements with a class attribute value that begins with "top":
Note: The value does not have to be a whole word!
CSS [attribute$="value"] Selector
The [attribute$="value"]
selector is used to select elements whose attribute value ends with a specified value.
The following example selects all elements with a class attribute value that ends with "test":
Note: The value does not have to be a whole word!
CSS [attribute*="value"] Selector
The [attribute*="value"]
selector is used to select elements whose attribute value contains a specified value.
The following example selects all elements with a class attribute value that contains "te":
Note: The value does not have to be a whole word!
Styling Forms
The attribute selectors can be useful for styling forms without class or ID:
Example
input[type="text"] {
width: 150px;
display: block;
margin-bottom: 10px;
background-color: #e7e7e7e7;
padding:10px;
border:none
}
input[type="button"] {
width: 120px;
margin-left: 35px;
display: block;
}