.NET (298) administrative (42) Ajax (42) AngularJS (2) ASP.NET (146) bicycle (2) books (191) browser (10) C# (135) cars (1) chess (29) CodePlex (10) Coma (8) database (54) deployment (3) Entity Framework (2) essay (118) flash/shockwave (2) flex (1) food (3) friend (2) game (21) idea (5) IIS (8) javascript (85) LInQ (2) Linux (6) management (4) manga (44) misc (697) mobile (1) movies (97) MsAccess (1) murder (2) music (64) mysql (1) news (101) NuGet (1) permanent (1) personal (67) PHP (1) physics (2) picture (325) places (12) politics (15) programming (518) question (2) rant (121) religion (3) science (44) Sharepoint (3) software (58) space (1) T4 (2) technology (12) Test Driven Development (4) translation (2) VB (2) video (103) Visual Studio (45) web design (47) Windows API (8) Windows Forms (3) Windows Server (6) WPF/Silverlight (63) XML (12)

Thursday, February 02, 2012

CSS selectors for two or more classes

The CSS standard allows selecting an element with a certain CSS class, using the dot notation (.myClass), but it also allows an element to have more than one CSS class, separated by space (class="myClass anotherClass"). Now, sometimes we would like to select an element that has two simultaneous classes and the CSS syntax for this is to put two class selectors one after the other, with nothing to separate them (.myClass.anotherClass). This blog entry is about how you should avoid using this, at least for now, as it seems to be one of the buggiest parts of the CSS implementation in the current browsers.

First of all, Internet Explorer just fails with this. Up to version 8 simultaneous class selectors just failed in a random way. I was surprised a few days ago to see that Chrome and Firefox also have issues with this. Even if the selector did appear to work, it did not register in the css rule lists for either Firebug or the Chrome developer tools, when used with CSS3 content selectors. Remove the double class selector and they would magically appear.

The bug can be reproduced in this code:

<style type="text/css">
.a.b span:before {
content:"before ";

.b span:after {
content:" after";
<div class="a b">

and here is the result:

In Chrome and Firefox the span element will appear with an after rule, but not a before rule, even if they are both applied.

No comments: