admin管理员组文章数量:1296463
I am experimenting (might be foolhardy) with reproducing in CSS what Douglas Crockford refers to as a bottom value.
What's a bottom value?
In Javascript, bottom values are
undefined
andnull
.
I can take a custom-data attribute:
data-my-custom-attribute=""
and I can give it a value of null (using Unicode U+2400
):
data-my-custom-attribute="␀"
In CSS, I can then reference any custom-data attribute which is null:
[data-my-custom-attribute="␀"] {
[... CSS PROPERTIES HERE...]
}
Next up, I'd like to deploy an equivalent to this Javascript:
if (myCustomAttribute !== null) { ... }
But it seems I can't reference any custom-data which isn't null, because something like this:
[data-my-custom-attribute!="␀"] {
[... CSS PROPERTIES HERE...]
}
doesn't work and isn't valid.
Having established that:
[data-my-custom-attribute!="␀"]
doesn't work, it occurs to me that:
[data-my-custom-attribute]:not([data-my-custom-attribute="␀"])
actually does work (and if nothing else es up, I'll stick with that).
Working Example:
div {
display: inline-block;
width: 100px;
height: 100px;
margin-right: 12px;
}
.rectangle {
display: block;
width: 450px;
height: 60px;
margin-top: 12px;
background-color: orange;
}
[data-my-custom-attribute="red"] {
background-color: red;
}
[data-my-custom-attribute="yellow"] {
background-color: yellow;
}
[data-my-custom-attribute="blue"] {
background-color: blue;
}
[data-my-custom-attribute="␀"] {
border-radius: 0;
background-color: black;
}
[data-my-custom-attribute]:not([data-my-custom-attribute="␀"]) {
border-radius: 50%;
}
<div data-my-custom-attribute="red"></div>
<div data-my-custom-attribute="yellow"></div>
<div data-my-custom-attribute="blue"></div>
<div data-my-custom-attribute="␀"></div>
<div class="rectangle"></div>
I am experimenting (might be foolhardy) with reproducing in CSS what Douglas Crockford refers to as a bottom value.
What's a bottom value?
In Javascript, bottom values are
undefined
andnull
.
I can take a custom-data attribute:
data-my-custom-attribute=""
and I can give it a value of null (using Unicode U+2400
):
data-my-custom-attribute="␀"
In CSS, I can then reference any custom-data attribute which is null:
[data-my-custom-attribute="␀"] {
[... CSS PROPERTIES HERE...]
}
Next up, I'd like to deploy an equivalent to this Javascript:
if (myCustomAttribute !== null) { ... }
But it seems I can't reference any custom-data which isn't null, because something like this:
[data-my-custom-attribute!="␀"] {
[... CSS PROPERTIES HERE...]
}
doesn't work and isn't valid.
Having established that:
[data-my-custom-attribute!="␀"]
doesn't work, it occurs to me that:
[data-my-custom-attribute]:not([data-my-custom-attribute="␀"])
actually does work (and if nothing else es up, I'll stick with that).
Working Example:
div {
display: inline-block;
width: 100px;
height: 100px;
margin-right: 12px;
}
.rectangle {
display: block;
width: 450px;
height: 60px;
margin-top: 12px;
background-color: orange;
}
[data-my-custom-attribute="red"] {
background-color: red;
}
[data-my-custom-attribute="yellow"] {
background-color: yellow;
}
[data-my-custom-attribute="blue"] {
background-color: blue;
}
[data-my-custom-attribute="␀"] {
border-radius: 0;
background-color: black;
}
[data-my-custom-attribute]:not([data-my-custom-attribute="␀"]) {
border-radius: 50%;
}
<div data-my-custom-attribute="red"></div>
<div data-my-custom-attribute="yellow"></div>
<div data-my-custom-attribute="blue"></div>
<div data-my-custom-attribute="␀"></div>
<div class="rectangle"></div>
However,
[data-my-custom-attribute]:not([data-my-custom-attribute="␀"])
feels awkward and verbose. Is there really nothing better?
Share edited Apr 16, 2021 at 14:43 BoltClock 724k165 gold badges1.4k silver badges1.4k bronze badges asked Apr 16, 2021 at 14:38 RouninRounin 29.5k13 gold badges98 silver badges123 bronze badges 6- it doesn't feel that awkward and verbose and it seems to be the only solution since you have 2 conditions to fulfill – Temani Afif Commented Apr 16, 2021 at 14:46
-
Re: "it doesn't feel that awkward and verbose" I like
[attribute!="value"]
much more than[attribute]:not([attribute="value"])
. – Rounin Commented Apr 16, 2021 at 14:48 -
even if the first exist you still need the condition that
attribute
exist so it will be[attribute][attribute!="value"]
unless you define it to work only on existing attribute – Temani Afif Commented Apr 16, 2021 at 14:50 -
That's not the case with
[attribute^="value"]
,[attribute*="value"]
,[attribute$="value"]
etc. – Rounin Commented Apr 16, 2021 at 15:04 - it's not the same. To have an attribute equal to something, it need to exist (you cannot have a non-existing attribute equal to something) but the not equal apply to non-existing attribute since if the attribute doesn't existe then it's for sure not equal to that value. – Temani Afif Commented Apr 16, 2021 at 15:07
3 Answers
Reset to default 9Right. Conclusions.
Just as there already exist in CSS the following attribute selectors:
[data-attribute="value"]
// hasdata-attribute
, the value of which isvalue
[data-attribute^="value"]
// hasdata-attribute
, the value of which begins withvalue
[data-attribute*="value"]
// hasdata-attribute
, the value of which containsvalue
[data-attribute$="value"]
// hasdata-attribute
, the value of which ends withvalue
I was hoping there might be something like:
[data-attribute!="value"]
// hasdata-attribute
, the value of which is notvalue
and then, by extension:
[data-attribute!^="value"]
// hasdata-attribute
and its value doesn't begin withvalue
[data-attribute!*="value"]
// hasdata-attribute
and its value doesn't containvalue
[data-attribute!$="value"]
// hasdata-attribute
and its value doesn't end withvalue
But, instead we only have:
:not([data-attribute="value"])
// value is notvalue
OR nodata-attribute
:not([data-attribute^="value"])
// value doesn't start withvalue
OR nodata-attribute
:not([data-attribute*="value"])
// value doesn't containvalue
OR nodata-attribute
:not([data-attribute$="value"])
// value doesn't end withvalue
OR nodata-attribute
So the only way to get rid of the alternative possibilities (ie. after logical OR) is:
[data-attribute]:not([data-attribute="value"])
//data-attribute
value is notvalue
[data-attribute]:not([data-attribute^="value"])
// value doesn't begin withvalue
[data-attribute]:not([data-attribute*="value"])
// value doesn't containvalue
[data-attribute]:not([data-attribute$="value"])
// value doesn't end withvalue
Unfortunately, there isn't a more concise way. Even jQuery's [att!=val]
selector, which has remained exclusive to jQuery all these years, doesn't require that the attribute be present to match, so you'd still need to pair that with [att]
.
I understand this is an experiment with the bottom value concept, but for the sake of pleteness I'll add that the closest things to a null attribute value in HTML (and by extension CSS) are either the empty string (the default value of custom data attributes), or the lack of the attribute entirely. The idiomatic way to achieve your desired result is to choose either the empty string or omission of the attribute altogether, and use a corresponding [data-my-custom-attribute=""]
or :not([data-my-custom-attribute])
selector respectively in CSS, and if (myCustomAttribute === "")
or if (("myCustomAttribute" in myDiv.dataset) === false)
respectively in JS.
Attribute selectors are selected with attributes like example:
Example:
<style>
a[target="_blank"] {
background-color: yellow;
}
</style>
<a href="http://www.google." target="_blank">google.</a>
本文标签: javascriptHow might I build a negative attribute selector in CSSStack Overflow
版权声明:本文标题:javascript - How might I build a negative attribute selector in CSS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741628891a2389242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论