I’ve been using this trick for quite a while, but I never thought to blog about it. However, I recently realized that it might not be as common as I thought, so it might be a good idea to document it in a blog post.
If you follow the discussions on www-style, you might have noticed the proposal for a @supports rule to query property and value support. Some people suggested that it should also test for selectors, for example whether a certain pseudo-class is supported. However, you can do that today, albeit in a limited manner (no OR and NOT support).
The main principle that you need to keep in mind is that browsers are expected to drop rules with selectors they don’t understand, even partially. So, if only one selector in a group cannot be parsed, the whole rule will be dropped. This means we can construct selector “tests”, which are use cases of the selector whose support we want to test, that will not match anything, even if the selector is supported. Then, we include that selector in the beginning of our selector group. If all this is unclear, don’t worry, as there’s an example coming next
Example
Suppose you want to apply the following CSS (for rudimentary custom checkboxes):
input[type="checkbox"] {
position:absolute;
clip: rect(0,0,0,0);
clip: rect(0 0 0 0);
}
input[type="checkbox"] + label::before {
content: url('checkbox.png');
}
input[type="checkbox"]:checked + label::before {
content: url('checkbox-checked.png');
}
only in browsers that support the attribute equality selector, the :checked pseudo-class and the ::before pseudo-element. We need to try to think of a selector that includes all of them but matches nothing. One such selector would be #foo[type="checkbox"]:checked::before. Even in supporting browsers, this matches nothing as there’s no element with id=”foo”. We can reduce the test for every rule to conserve bandwidth: For example, we don’t need to include tests for the attribute selector in any of them, since they are present anyway in all three rules. Also, we may eliminate ::before from the second test and we don’t need any test for the 3rd one, since it includes all features we want to test for. To sum up:
#foo:checked::before,
input[type="checkbox"] {
position:absolute;
clip: rect(0,0,0,0);
clip: rect(0 0 0 0);
}
#foo:checked,
input[type="checkbox"] + label::before {
content: url('checkbox.png');
}
input[type="checkbox"]:checked + label::before {
content: url('checkbox-checked.png');
}
An important caveat of this technique is that Internet Explorer up to version 7 will split selectors before parsing them, so it will completely ignore our filters
(Thanks to Ryan Seddon for finding that out).
Disclaimer: The original idea about custom checkboxes belongs to Ryan Seddon, although his code was quite different.
Pingback: Powerful New CSS- and JavaScript Techniques | t1u
Pingback: Powerful New CSS- and JavaScript Techniques@smashing | seo博客大全
Pingback: Powerful New CSS- and JavaScript Techniques | Buypappa blog
Pingback: Powerful New CSS- and JavaScript Techniques | MyOfflineTheme.com Skyrocket Your Offline Business Just Now
Pingback: Powerful New CSS- and JavaScript Techniques | Web Design Kingston
Pingback: Powerful New CSS- and JavaScript Techniques | DigitalMofo
Pingback: Steve deGuzmanPowerful New CSS- and JavaScript Techniques » Steve deGuzman
Pingback: Powerful New CSS- and JavaScript Techniques « Web Development Website
Pingback: Powerful New CSS- and JavaScript Techniques » E BLADE
Pingback: Powerful New CSSand JavaScript Techniques - rehavaPress
Pingback: Powerful New CSS- and JavaScript Techniques | CS5 Design
Pingback: Rutweb Technology : Powerful New CSS- and JavaScript Techniques
Pingback: CSS and Javascript Techniques
Pingback: Powerful New CSS- and JavaScript Techniques | TuTsRUS - All Adobe Tutorials - Photoshop | Illustrator | Encore | Premeire | After Effects
Pingback: Powerful New CSS- and JavaScript Techniques — Заметки по дизайну
Pingback: Powerful New CSS- and JavaScript-Techniques (2012 Edition) | Smashing Coding
Pingback: Powerful New CSS- and JavaScript Techniques | Sedation Dentist Wayne
Pingback: Powerful New CSS – and JavaScript Techniques
Pingback: Powerful New CSS- and JavaScript Techniques
Pingback: Bergstrom Productions Powerful New CSS- and JavaScript Techniques | Bergstrom Productions
Pingback: custom checkbox input using CSS only | Jisku.com - Developers Network
Pingback: custom checkbox input using CSS only - feed99
Pingback: Powerful New CSS- and JavaScript Techniques « OhMyDev
Pingback: Kulapeta 200
Pingback: Nuevas técnicas CSS3, HTML5 y JavaScript – Sweety Web Designs
Pingback: Powerful New CSS- and JavaScript-Techniques (2012 Edition)
Pingback: Powerful New CSS- and JavaScript Techniques | CGBook