!important dans IE 6Màj: 7 février 2009
Il est notoriété publique qu’Internet Explorer 5 et 6 ne supportent pas le
mot-clef !important. Ce problème a d’ailleurs inspiré un
hack CSS pour Internet Explorer 5-6, qui peut se présenter ainsi:
p {
background: navy !important; /* Appliqué par tous les navigateurs récents */
background: black; /* Valeur appliquée par IE6 uniquement */
}
Ce hack, qui détourne une fonctionnalité du langage pour viser un navigateur en particulier, est comme la plupart des hacks CSS fortement déconseillé. Pour viser une version particulière d’Internet Explorer, on utilisera de préférence les commentaires conditionnels.
Cependant, il est faux de dire qu’Internet Explorer 5-6 ne comprend
pas !important. Il le comprend la plupart du temps, mais
un bug d’IE 5-6 dans un cas de figure précis fait que ce mot-clé n’est pas
pris en compte.
Voir ci-dessous une série de tests qui illustrent ce problème, et la conclusion plus bas.
Dans tous les cas de figure, les fonds des titres des blocs de test sont
censés apparaitre en vert clair (lime).
Si le mot-clé !important n’est pas supporté, ils doivent alors
apparaitre en bleu foncé (navy). Enfin, si rien ne va plus, ils
pourraient apparaitre en noir (black).
#test1 {
background: lime !important;
background: black;
background: navy;
}
#test2 {
background: black;
background: lime !important;
background: navy;
}
#test3 {
background: black !important;
background: lime !important;
background: navy;
}
#test4 {background: lime !important;}
#test4 {background: black;}
#test4 {background: navy;}
#test5 {background: black !important;}
#test5 {background: lime !important;}
#test5 {background: navy;}
#test6 {background: lime !important; background: black;}
#test6 {background: navy;}
#test7 {background: black !important; background: lime !important;}
#test7 {background: navy;}
#test8 {background: black !important;}
#test8 {background: lime !important; background: navy;}
J’analyse les résultats de ces tests de la manière suivante:
!important.!important est
appliqué correctement.!important, ce mot-clé est ignoré pour cette propriété dans ce
bloc de déclaration. Il n’est pas ignoré pour d’autres propriétés ou dans
d’autres blocs de déclaration.Le troisième point est un peu compliqué à saisir, mais il correspond bien,
je pense, à la complexité de la situation. Le hack CSS bien connu qui
exploite !important correspond en réalité à un cas très
particulier d’utilisation du mot-clé.