Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector for custom data-attribute?

Why my star is not appearing in YELLOW ? How to fix it ?

Here's the relevant code for the above issue.

HTML

<div class="tpl" data-favorite="1">
  <div>
    <span class="favorite">★</span>
    <span class="text">Italian Pizza: salmon, olives, onion, tomato, blue-cheese</span>
  </div>
</div>

CSS

[data-favorite=1] {
    background: #AAA;
    border-left: 3px solid green
}
.favorite {
    font-size: 2em;
    padding: 0 1 0 1em;
}
[data-favorite=1] .favorite {
    color:yellow;
}
[data-favorite=0] .favorite {
    color:#AAA;
}

Fiddle

like image 944
Hugolpz Avatar asked Feb 23 '14 17:02

Hugolpz


People also ask

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

[attribute] Selector: This type of attribute selector is used to select all the elements that have the specified attribute and applies the CSS property to that attribute. For example the selector [class] will select all the elements with the style attribute.

What is the CSS selector for an 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.

What is CSS attribute selector?

The CSS attribute selector matches elements based on the presence or value of a given attribute. a [title] { color: purple; } a

How to use jQuery selectors on custom data attributes?

How to Use jQuery Selectors on Custom Data Attributes jQuery provides several selectors to make the queries you are looking for. The starts with, ends with, and contains () selectors also can be used to select the specified string. Let’s consider the HTML list:

Is it possible to specify data attributes in CSS?

Specifying data attributes in CSS is no different to specifying presentational attributes in HTML. If you're trying to assign metadata to a class name which then applies to all elements with that class name, that's (again) completely outside of the purview of CSS, and simply not possible in HTML.

What is the [ attribute*] selector used for?

The [attribute*="value"] selector lets you select DOM elements that have the given custom attribute and whose value contains a given value, whether by itself or within a word or number.


2 Answers

You'll want to use

[data-favorite="1"] {}

The difference being the quotes "" around the value.

Here's the working jsFiddle

like image 151
Brett DeWoody Avatar answered Oct 18 '22 10:10

Brett DeWoody


You need to use " around the attribute value

[data-favorite="1"] {
   /* Styles goes here */
}

Demo


Why is that so?

CSS Specification - 6.3. Attribute selectors

Attribute values must be CSS identifiers[1] or strings. [CSS21] The case-sensitivity of attribute names and values in selectors depends on the document language.

Identifiers

[1] In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".


So the issue is that the value of your attribute starts with a number, if you have something like this in your HTML (You already do)

<span data-favorite="0">Color Me red</span>

[data-favorite=0] { color: red;}

WONT WORK

Demo


But, if you have something like

<span data-favorite="a0">Color Me red</span>

[data-favorite=a0] { color: red;}

WILL WORK (Even without quotes) because the value of your attribute starts with an alphabet [1].

Demo

like image 31
Mr. Alien Avatar answered Oct 18 '22 09:10

Mr. Alien