Simplest Css Hack Ever

May 8th, 2008 | Save to del.icio.us now(0)

“What?!! the dawn of IE8 is upon us and someone is writing about css hack for IE”

Well… my response is simple, you don’t need to use this it’s just for your knowledge. ;)

I have been using this trick for two years now, as most of the other magical css hack were above my head.

I discovered this accidentally, and it might not be an hack also, but i use it a lot in my work.

So what is this hack?

Simple, just put ‘//’ in front of any valid css declaration, most of the other browser will treat this as comment and ignore it, while our friend Internet Explorer will not treat this as comment and render the property.

<style>

.test{
     color:red;
     //color:green;
}

</style>

<p class='test'>I will be green in Internet Explorer, red in all other browsers</p>

The only thing that you must note is the sequence of statements. The declaration that you want to override in IE should come after the declaration for other browsers. What it means is that if you put the //color:green first in above example then even IE will show the color of text as red.

Example:

I will be green in Internet Explorer, red in all other browsers.


How it works?

The idea is very simple as second line is treated as comment by other browsers like firefox, they simply ignore it. Internet Explorer on the other hand does not treat second statement as comment, so it simply overrides the first statement. The only know limitation is that it does not provide you with any means to differentiate IE6 and IE7

This is kind of interesting, for once i think IE followed the right path while others deviated to make writing comments easier for us developers.

I had googled to check if some one has already written about it, but i was surprised to see that no one had written anything about it, while they have listed all the big and complex hacks.

I thought it must be because this is so simple… or may be nobody ever tried it? who knows?

While writing this post i was even tempted to name this hack as ‘akHack’, and thought better off it.

Any ways I just want to satisfy my curiosity, did you knew about this?

update [10 may]: finally i know the name for this hack it is called Underscore hack, thanks to neil and chris. So what i am using is basically a variation of or extended underscore hack.

Related posts

  1. Simplest Css Hack Ever
  2. 5 key skills of a successful web application developer
  3. Center align a container in IE

Wondering what to do next?


(Search web development related contents)


26 Responses to “Simplest Css Hack Ever”

  1. May 8, 2008 8:19 pm Jess says

    Interesting, I’ve never heard of that before. I’ll have to try it out. Do all other browsers but IE ignore it?

    My favorite hacks are the underscore _ and the star *, but hopefully with IE 8 we won’t need them anymore.

  2. May 9, 2008 5:44 am Ryan says

    Well, 30% of my visitors are still on IE6, no idea what will make them want to move up to IE 8 if they still don’t know there is an IE 7 our there. SP 3 in XP doesn’t even update the browser.

    That’s a pretty good hack though. I usually just make 3 style sheets, one for real browsers, one for IE 6 and occasionally one for IE 7, then I use IEs

  3. May 9, 2008 6:18 am Amit Kumar Singh says

    @ryan
    my feeling is that many people are holding out on IE7 because it’s not as good as IE6 is when it comes to user friendliness of the browser. I was one of those users, till windows auto updater, installed IE7 on my machine last december.

    It’s similar to why people are not upgrading from XP to vista.

    I just hope IE8 will be as compelling to normal users as it is to us developers.

  4. May 9, 2008 9:28 am Craig Francis says

    // – is not a valid comment mark, according to the W3C spec… and the good browsers are not treating it as a comment.

    Instead they are seeing it as an invalid CSS rule, and ignoring it (which is following the spec).

    I know the difference is small, but it kind of explains how this works… as IE tries to cater for bad developers who make mistakes in code (or even code from bad WYSIWYG editors), it will continue blindly over this.

    Ultimately though, this is yet another thing that IE got wrong, as in the future, when the CSS spec is expanded, these kinds of “I’m going to guess what you mean” type of features found in IE will mean that future rules (CSS3?), instead of being ignored, and hopefully leaving an ugly, but still working site, will be second-guessed by IE5/6/7 and all kinds of weird things will happen in each and every version (all probably doing a different thing based on their own guesswork).

    Oh well… for now though, that does kind of provide a good hack… but, like all hacks, I would advise against you using them. Rather you should write code which is kind of redundant.

    For example, if floating an element with a margin, you might find that the margin value is doubled… instead of putting in a hack to specify a different margin size, you should add the “display: inline” rule… according to the spec, a floated element over-rides the “display” rule, so it shouldn’t (and at the moment doesn’t) effect any other browsers… but for some reason, it gives IE5/6 a little kick (hasPosition), and fixes it’s bug.

  5. May 9, 2008 4:38 pm BTM says

    @Craig: it’s hasLayout not hasPosition, and as for the main point – you’re right, the “//” style comments is invalid (css won’t validate) and is skipped by modern browsers.

  6. May 9, 2008 5:12 pm Amit Kumar Singh says

    @craig
    you are right ‘//’ it is invalid as far as css specs is concerned, but just for sake of developers i said that it is commented out as this ‘//’ is comment symbol for most of the programming knowledge.

    anyways you explained it well though.

  7. May 9, 2008 6:34 pm bobby n. says

    one could replace the // with ##, $$, %%, etc to achieve the same non-valid effect.

  8. May 9, 2008 8:11 pm Chris Newman says

    Why not use the _ before an element instead.

    height:10px;
    _height:20px;

    Its valid for CSS 2.1 spec and all browsers but IE 6 and 5.5 ignore it.

  9. May 10, 2008 1:14 am neil says

    Look into the underscore (IE 6) and star (IE 7) hacks. They do the same thing you describe but can target versions of IE

  10. May 10, 2008 9:11 am Amit Kumar Singh says

    @neil & @chris : thanks for pointing out the underscore hack to me. now i know the name of the hack that i was using :D

    @bobby: you are right.

  11. May 16, 2008 6:51 am Center align a container in IE | am i works? says

    [...] some googling i found following solution on Webcredible.co.uk website. I added my “simple css hack” to make sure it was available only in Internet Explorer. The [...]

  12. May 17, 2008 6:08 pm Binny V A says

    Isn’t using the conditional comments a better option?

  13. May 17, 2008 6:50 pm Amit Kumar Singh says

    conditional comment is a better option, but sometimes using conditional comments could be tedious(say for just changing a margin or width) in that situations i would prefer to use underscore css hack.

  14. July 24, 2008 4:44 pm Aleksandr Pasevin says

    Well that is surprising in a very good way! It is just what i was searching for. Thanks!

  15. August 12, 2008 6:12 pm mukesh says

    hey Thanbks for the info but I think you can even use the !important, It works as this comment like

    .test{
    color:red !important;
    color:green;
    }

    I will be green in Internet Explorer, red in all other browsers

    now FF will take red and IE 6 will take green

  16. January 12, 2009 2:10 pm kapil says

    Hello,
    I need to know about hack only for IE8 beta 1 only, anyone can help me?
    Thanks

  17. February 19, 2009 4:17 pm Adrian von Gegerfelt says

    It’s red in Explorer 8 beta 2

  18. February 19, 2009 4:20 pm Amit Kumar Singh says

    @adrian … in that case time to stop using hacks :) and start using conditional css

  19. March 24, 2009 7:32 pm Sabuga says

    @Amit Kumar Singh
    IE have no idea what conditional/w3c etc.. means

    I hate IE

  20. June 10, 2009 12:04 am Monica Pitts says

    You are the coolest person I know right now. I just got IE 8 and was banging my head against a wall trying to optimize a site for all the major browsers and this information seriously made my day. Thank you sooo much for the post!!!!

  21. June 17, 2009 8:09 pm The bestist CSS Hack EVER (desipte it’s imperfections) in conjuction with what I learned about drop down menus and css in IE. says

    [...] That’s to this lovely fellow I now know: http://amiworks.co.in/talk/simplest-css-hack-ever/ [...]

  22. July 12, 2009 12:47 am Kite says

    This hack don’t work for ie8…

    Just add the \9 text at the end of an code…
    ex: width: 30px\9; :D

  23. February 5, 2010 2:50 pm satyendra says

    This // hack only work in IE6 not work in IE7, IE8… so please don’t say that this work in all IE. visit this site and know how to work with minimum using HACK as you know that all are not considered best in W3C.
    Satya

  24. March 9, 2010 7:06 pm Junaid Nawaz says

    Guys always use this code example=position:relative\9; before the other hack symbol codes(#abc,_abc,*abc)etc. otherwise it does not work in ie8.

  25. June 30, 2010 2:37 pm ciprian says

    its doesnt work :( (

  26. November 2, 2010 4:58 pm Chris Brandrick says

    Worked great for me in IE8. Cheers.

Trackback URI | Comments RSS

Say Something, and Be Counted

Name (required)

Email (required)

Website

Speak your mind