I know we can select attributes that begin with "foo" given an attribute name.
div[class^="foo"]
Can this be done without an attribute name?
div[^="foo"]
Note:
I am aware we can do this:
div[foo] {
color: blue;
}
But in my case, I am trying to select something that has a variable attribute name:
foo-1
foo-2
etc.
div[^=foo] {
color: red;
}
div[class^="foo"] {
color: red;
}
<div foo>This text is not red</div>
<div class="foo">This text is red</div>
Can this be done without an attribute name?
While the answer isn't too pretty, the short of it is no, it can't - not in the way you're suggesting.
References:
W3 Attribute Selectors Specification
[att]
Represents an element with the att attribute, whatever the value of the attribute.
[att=val]
Represents an element with the att attribute whose value is exactly "val".
[att~=val]
Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If "val" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "val" is the empty string, it will never represent anything.
[att|=val]
Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-" (U+002D). This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML) as described in BCP 47 ([BCP47]) or its successor. For lang (or xml:lang) language subcode matching, please see the :lang pseudo-class.
W3 General Selectors Specification
The following table summarizes the Selector syntax:
...
[Provides documentation of all valid selectors, omitting any mention of wildcard attribute selectors]
...
Related
Can I have multiple values in one HTML "data-" element? Similar to how a class can have multiple class names.
If possible, I would like to create a CSS/JS library that makes use of one "data-" element to house all of the library styles. For example:
<div data-library-name="xs-hidden col-md-10 col-xl-8 big-hero"></div>
That way, any of the programmers custom style rules can go into the elements class. My reasoning for this is to make readability easier, so together it would look like:
<div class="custom-style another-style" data-library-name="xs-hidden col-md-10 col-xl-8 big-hero"></div>
Can I have multiple values in one HTML "data-" element?
You can have a string. The spec doesn't define any particular format for the data in the attribute, which is designed to be processed by site specific JavaScript.
Similar to how a class can have multiple class names.
The class attribute takes a space separated list of classes.
Your JavaScript can your_data_attribute_value.split(" "); if you like.
Handling this with CSS would use the ~= attribute selector.
[att~=val]
Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If "val" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "val" is the empty string, it will never represent anything.
AFAIK, I don't think data- attributes can convert that to an array. Instead, I think it'll interpret it as one value, but it is allowed.
If you want to do that, you'll probably have to split() it later in JavaScript into an array of usable values.
See this example on JSFiddle.net.
CSS has the shortcut .class selector but it actually is parsing the attribute named "class" as a list for space separated values. This is supported in the non-shortcut form by the following attribute selector:
[att~=val]
Represents an element with the att attribute whose value is a white space-separated list of words, one of which is exactly "val". If "val" contains white space, it will never represent anything (since the words are separated by spaces). If "val" is the empty string, it will never represent anything either.
Ref: http://www.w3.org/TR/CSS2/selector.html#class-html
As your question is tagged CSS you're perhaps looking for that. The rules how the parsing of attribute values is done is given in that document as well, so in case the javascript library you're trying to use on this (if any) won't cover that, it should be easy to add:
var list = $("div").data("library-name").split(/\s+/);
^^^^^^^^^^^^
This split with the white-space regular expression parses the string attribute value into an array with javascript and the Jquery library (for accessing the DOM and the data attribute).
Is an empty class attribute valid HTML in the following formats:
<p class="">something</p>
<p class>something</p>
I found this question which is similar, but asks specifically about custom data attributes.
After looking at the specifications referred to in the other answers, I have found the sections that actually do answer the raised question.
<p class> is not allowed
The specification on attributes section 3.2.3.1 on Empty Attribute Syntax states the following:
An empty attribute is one where the value has been omitted. This is a syntactic shorthand for specifying the attribute with an empty value, and is commonly used for boolean attributes. This syntax may be used in the HTML syntax, but not in the XHTML syntax.
(...)
This syntax is permitted only for boolean attributes.
Seeing that the description of the class attribute (obviously) does not mention it being a boolean attribute, omitting the value is not permitted.
<p class=""> is allowed
From the section on class we learn that:
Every HTML element may have a class attribute specified.
The attribute, if specified, must have a value that is a set of space-separated tokens representing the various classes that the element belongs to.
and from the definition of space-seperated tokens:
A set of space-separated tokens is a string containing zero or more words (known as tokens) separated by one or more space characters, where words consist of any string of one or more characters, none of which are space characters.
we can conclude that the attribute value can in fact be empty (i.e. containing zero tokens).
From the HTML5 Reference page, section 3.2.3 Attributes:
Elements may have attributes that are used to specify additional information about them. Some attributes are defined globally and can be used on any HTML element, while others are defined for specific elements only. Every attribute must have an attribute name that is used to identify it. Every attribute also has an associated attribute value, which, depending on the attribute's definition, may represent one of several different types. The permitted syntax for each attribute depends on the given value.
So to answer your question,
Invalid:
<p class>
Valid (empty value)
<p class="">
See http://dev.w3.org/html5/html-author/ For all the reference regarding HTML5 you need.
Not having any values won't make it invalid. I have tested it in http://validator.w3.org/#validate_by_input
Put this code there and test:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class>Validiity
<input type="text" disabled>
</div>
</body>
</html>
Without quotes, just attribute names are drafted for boolean attribute like disabled, required
A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
More here: https://html.spec.whatwg.org/#boolean-attributes
Read this Q/A on boolean attribute discussion - What does it mean in HTML 5 when an attribute is a boolean attribute?
Class attribute should contain a value. without value its not a valid one. but it shows no impact while rendering.
Using the following CSS, why am I not able to target the 3 following anchor tags?
a[href~="checkout"] { /* Do something. */ }
<a href="http://shop.mydomain.com/checkout/onepage/">
<a href="https://shop.mydomain.com/checkout/onepage/">
<a href="https://shop.mydomain.com/checkout/onepage/?___SID=S">
What am I doing wrong in my CSS selector? I'm trying to select a partial match using ~= for all URLs containing the word "checkout."
Try this
a[href*="checkout"] { /* Do something. */ }
You have to use *, which means contain - see reference.
Also, don't forget to close anchor tags
</a>
Working JSFiddle
a[href~="checkout"]
matches elements where in a whitespace-separated list of words one of these is exactly "checkout".
See W3C Docs for Selectors Level 3:
[att~=val]
Represents an element with the att attribute whose value is
a whitespace-separated list of words, one of which is exactly "val".
If "val" contains whitespace, it will never represent anything (since
the words are separated by spaces). Also if "val" is the empty string,
it will never represent anything.
Source: http://www.w3.org/TR/css3-selectors/#attribute-selectors
If you want to match all elements where attr contains string, use
[attr*=string]
If
.animal {background: yellow}
will apply the styling rule to any elements with a class containing the word animal, even if it also contains other words eg...
<li class="toy animal">Toy Bear</li>
then what is the need for the below syntax for selecting by partial attribute?
*[class~="animal"] {background: yellow}
Thanks
The only difference is, you can use .value syntax only for classes, when [attribute~="value"] can be used to match any attribute values.
But when you use [class~="className"] to match class attribute values, it is equivalent to standard .className syntax.
According to the selectors spec, the period . is an alternative for the ~= notation for the class attribute.
Thus, for HTML, div.value and div[class~=value] have the same meaning
Just to clarify the ~= meaning:
E[foo~="bar"] an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar"
Note that this is different than *=
In other words, .animal and [class~=animal] (without the *) are the same.
Maybe I am missing something, but they seem similar. If you use for example...
a[alt~="thumb"]
or...
a[alt*="thumb"]
What can I narrow my selection down to differently? I am at the understanding that ~ gives you a partial match in the quotes while the * gives you a partial match. I am going to fiddle with the code a little, but since I could not find a question on the subject here, thought it would make a good topic either way.
From the JQuery help (which supports the standard selectors):
a[alt~="thumb"]
Description: Selects elements that have the specified attribute with a
value containing a given word, delimited by spaces. This selector
matches the test string against each word in the attribute value,
where a "word" is defined as a string delimited by whitespace. The
selector matches if the test string is exactly equal to any of the
words.
a[alt*="thumb"]
Description: Selects elements that have the specified attribute with a
value containing the a given substring. This is the most generous of
the jQuery attribute selectors that match against a value. It will
select an element if the selector's string appears anywhere within the
element's attribute value. Compare this selector with the Attribute
Contains Word selector (e.g. [attr~="word"]), which is more
appropriate in many cases.
Basically the selector ~= only matches if the value is found surrounded by white space. The selector *= matches if the value is found anywhere.
<div alt='heading navigation'>
<div alt='head'>
div[alt~='head'] would match only the second div, but div[alt*='head'] would match both.
[att~=value] is a contains word selector.
So a [alt="foo"] selector will match <a alt="foo bar"> but will not match <a alt="foobar">.
[alt*="foo"] will match both though, because this doesn't discriminate on words or whatever. As long as it's in the value, it hits.