Post Archive
› November 21, 2003
CSS: The Underscore MSIE-Only Hack
There is a simple CSS hack allowing you set CSS properties for Windows IE only. Czech web designer Petr Pisar found it out: if you add the underscore ("_") before the property name, WinIE ignores it and uses the property without the underscore. See details on my web.
Comments
1. November 21, 2003 12:17 PM
2. November 21, 2003 12:39 PM
liorean Posted…
It is technically allowed by the css grammar. That doesn't make it valid, because it has to be a defined property name to be valid, which _position is not. CSS grammar is built for future compatibility, which means it supports the future addition of a _position property. However, W3C has stated that they will not use underscore or hyphen as the first character in standard property names, which leaves it to proprietary vendor specific extensions.
3. November 21, 2003 05:01 PM
mw22 Posted…
I use // all the time for this, but that's probably not correct in css.
4. November 21, 2003 05:15 PM
pixy Posted…
Right, liorean. I'll just add the link to the proper place in the CSS specification to show that this really IS correct and valid (in opposite to several other CSS hacks). Since CSS 2.1, W3C no more requires the property name must exists according to the specification. Maybe someone forgot tell it to the validator... :)
5. November 21, 2003 10:15 PM
liorean Posted…
Not quite correct. That is the lexer, which defines the css grammar and syntax. The grammar tells us how to parse the CSS, but in the same way the XML syntax rules, well-formedness rules and grammar specifies not the validity of the document, but how to parse the XML, it does not describe the validity. The validity is described by the property list of what ever CSS specification you are validating against, as explained by [CSS2.1] 3.1 Definitions, under valid style sheet
6. November 23, 2003 05:23 AM
David H Posted…
I found this out a while ago too, but then with a colon instead of a underscore. I tested a little more when I saw this, and it seems like several other special characters works too (at least ,.:;()!?+*= does).
7. November 23, 2003 07:22 AM
Robbert Broersma Posted…
Internet Explorer 6 for Windows only: _prop\erty: value;
8. August 15, 2004 08:30 AM
webmaster Posted…
Underscore hack will not validate, but the star hack will http://www.info.com.ph/~etan/w3pantheon/style/starhtmlbug.html
andreas Posted…
Hm. I don't quite get it. Validating the stylesheet results in: "Property _position doesn't exist : absolute". So, what do you mean with "Underscore ("_") is allowed in CSS identifiers by the CSS2.1 Specification"?