I understand that css rules can target elements specified by attribute values, e.g.:
input[type="text"] {}
Can I make a rule that targets elements which omit a certain attribute? For example, can I target elements that lack an href or elements that don't specify a type?
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".
In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.
We use element selectors like a , p , and div to style a selected element, while pseudo-elements like ::after and ::before are used to style specific parts of an element. Element and pseudo-element selectors have the lowest specificity. In the specificity weight system, they have a value of 1.
You can follow this pattern:
a:not([href])
input:not([type])
The attribute selector is used to select elements with the specified attribute.
:not
is supported in all modern browsers and IE9+, if you need IE8 or lower support you're out of luck.
For links you can simply use:
a { color: red; }
a:link { color: green; }
fiddle: http://jsfiddle.net/ZHTXS/ no need for javascript.
For form attributes, use the not attribute pattern noted above input:not([type])
and if you need to support older versions of IE, I'd probably add a class and use an IE specific style sheet linked with conditional comments.
You can always set different style to a and a href
Check this: http://jsfiddle.net/uy2sj/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With