Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select elements by attribute in CSS

Is it possible to select elements in CSS by their HTML5 data attributes (for example, data-role)?

like image 973
Diogo Cardoso Avatar asked Mar 16 '11 11:03

Diogo Cardoso


People also ask

How can we select elements with a specified attribute in CSS?

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".

What CSS selector is used to select elements with a specified attribute and value?

The [attribute=value] selector is used to select elements with the specified attribute and value.

How do you target input values in CSS?

CSS attribute selector is used to targeting an input text fields. The input text fields of type 'text' can be targeting by using input[type=text].

What is the CSS selector for a tag containing the title attribute?

CSS [attribute~=value] Selector The [attribute~=value] selector is used to select elements with an attribute value containing a specified word.


1 Answers

If you mean using an attribute selector, sure, why not:

[data-role="page"] {     /* Styles */ } 

There are a variety of attribute selectors you can use for various scenarios which are all covered in the document I link to. Note that, despite custom data attributes being a "new HTML5 feature",

  • browsers typically don't have any problems supporting non-standard attributes, so you should be able to filter them with attribute selectors; and

  • you don't have to worry about CSS validation either, as CSS doesn't care about non-namespaced attribute names as long as they don't break the selector syntax.

like image 148
BoltClock Avatar answered Sep 22 '22 10:09

BoltClock