views:

16929

answers:

13
+7  Q: 

IE8 css selector

To target elements only in IE browsers i'll use

IE6:

* html #nav li ul {
    left:                                 -39px!important;
    border: 1px solid red;
}

IE7:

*+html #nav li ul  {
    left:                                 -39px!important;
}

Does anyone know how to target IE8?

+25  A: 

Use conditional comments to target IE only.

Example:

<!--[if gte IE 8]>
<style>
(your style here)
</style>
<![endif]-->

Everything inside the two <!--> will be ignored by all non-IE browsers as a comment, and IE versions that are less than IE8 will skip it. Only IE8 and greater will process it.

Rex M
+1, beat me to it :)
Darko Z
I'd go so far as to say "Use conditional comments to target any version(s) of IE".
David Dorward
Rex nailed it. CSS hacks are called hacks for a reason - they're unprofessional programming. Use conditional comments. http://blogs.msdn.com/ie/archive/2005/10/12/480242.aspx
Jon Galloway
I guess your conditional statements do the trick, though this doesn't really answer the question by Chris who wants to know of a selector to address IE8 only
Michiel
@Michiel since that's not possible without exploiting a browser bug, the answer has two parts: a) don't do that; and b) here's how to do it instead.
Rex M
A: 

Why would you want to target IE8 only? If your CSS is well written, you should not have any problems in IE8.

mdja
Good point. People are so comfortable with hacks that they forgot standards exists.
Adrian Godong
Wick
+1  A: 

In the ASP.NET world, I've tended to use the built-in BrowserCaps feature to write out a set of classes onto the body tag that enable you to target any combination of browser and platform.

So in pre-render, I would run something like this code (assuming you give your tag an ID and make it runat the server):

HtmlGenericControl _body = (HtmlGenericControl)this.FindControl("pageBody");
_body.Attributes.Add("class", Request.Browser.Platform + " " + Request.Browser.Browser + Request.Browser.MajorVersion);

This code enables you to then target a specific browser in your CSS like this:

.IE8 #nav ul li { .... }
.IE7 #nav ul li { .... }
.MacPPC.Firefox #nav ul li { .... }

We create a sub-class of System.Web.UI.MasterPage and make sure all of our master pages inherit from our specialised MasterPage so that every page gets these classes added on for free.

If you're not in an ASP.NET environment, you could use jQuery which has a browser plugin that dynamically adds similar class names on page-load.

This method has the benefit of removing conditional comments from your markup, and also of keeping both your main styles and your browser-specific styles in roughly the same place in your CSS files. It also means your CSS is more future-proof (since it doesn't rely on bugs that may be fixed) and helps your CSS code make much more sense since you only have to see

.IE8 #container { .... }

Instead of

* html #container { .... }

or worse!

jsidnell
Just for completeness, the jQuery plugin that adds browser and platform classes that you can use in CSS (and even in your jQuery functions) is available at http://jquery.thewikies.com/browser/
jsidnell
+7  A: 

Take a look at these:

/* IE8 Standards-Mode Only */
.test { color /*\**/: blue\9 }

/* All IE versions, including IE8 Standards Mode */
.test { color: blue\9 }

(Source: David Bloom’s CSS hack for IE8 Standards Mode)

Gumbo
there's an updated note on this that says it may actually target IE7 also
philfreo
+6  A: 

Why would you want to target IE8 only? If your CSS is well written, you should not have any problems in IE8.

ROFL let me rephrase that for you If your IE8 is well written, you should not have any problems with CSS.

I do wish there was a star hack equivalent for IE8... maintaining CSS for all version in one stylesheet is becoming more and more complicated now.

They should have had CSS conditional comments. Pity they were so confident as to kill the hack before making sure the bugs were gone.

JeromeLapointe
That's simply not true, every browser has bugs. Usually you try to work around them in various ways -- making your layout tolerant of the bug, avoiding the bug, or, as we are asked here, by adding rules that target the buggy renderer directly. This is usually a last ditch sort of thing -- but also it can be a budget issue: you may simply not have the resources to afford a more elegant solution.
Adam Luter
Your "answer" should be on the comment.
Adrian Godong
Why do you need everything in one stylesheet?
WCWedin
A: 

OK so, it isn't css hack, but out of frustration for not being able to find ways to target ie8 from css, and due to policy of not having ie specific css files, I had to do following, which I assume someone else might find useful:

if (jQuery.browser.version==8.0) {
   $(results).css({
         'left':'23px',
         'top':'-253px'
      });
}
Zeljko Dakic
A: 

CSS style only for IE8:

.divLogRight{color:Blue; color:Red\9; *color:Blue;}

Only IE8 will be Red.

first Blue: for all browsers.

Red: IE6,7,8 Only

Second Blue: IE6,7 Only


So Red = for IE8 only.

http://paulirish.com/2009/browser-specific-css-hacks/

SirMoreno
+3  A: 

This question is ancient but..

Right after the opening body tag..

<!--[if gte IE 8]>
<div id="IE8Body">
<![endif]-->

Right before the closing body tag..

<!--[if gte IE 8]>
</div>
<![endif]-->

CSS..

#IE8Body #nav li ul {}

You could do this for all IE browsers using conditional statements, OR target ALL browsers by encapsulating all content in a div with browser name + version server-side

anon
I saw something similar on the jqueryui themeroller demo http://jqueryui.com/themeroller/developertool/ Works pretty nicely... if you can control the outer markup for your page/site. Working on a major rewrite now, and using this for my IE specific code... ie6,ie7,ie8,ie9, ieold (pre-9) and noie (non-ie) ... there's only a few places where I'm getting specific, but having the option from the markup + CSS alone is nice (no relying on scripts). Though I still need scripts to apply additional styles since IE6 doesn't have attribute selectors.
Tracker1
A: 

Rex nailed it. CSS hacks are called hacks for a reason - they're unprofessional programming.

but using * html and * + html are NOT hacks, that is, those two are VALID css,

on the other hand, blue/9 IS a hack, since it does NOT validate.

Using things like * html and * + html is a good practice, as they can be used to implement progressive enhancement.

edit: from the discussion below I must say its better to use html>body instead of the star html

those 2 selectors depend on the extra element IE adds before the html element (its like a IE "feature")

blue/9 on the other hand, depends on a IE8 bug, therefore is a hack.

the point is: CSS selectors are NOT CSS hacks

...old question, I know, but its important to make clear the difference, and anyone can find this on google

chrisl
And why are they called star- and star-plus-hack all over the web? just because the validator doesn't tell you that they are invalid, it doesn't tell you that it is not a hack. And using them is NOT good pracitce at all it is just a bad alternative to conditional comments.
Kau-Boy
IE adds an extra element before html.thats wrong, but its a bug on the html part of IE, not in the CSS parser.CSS rules don't forbid that certain element exist before other certain element. (like, css parser don't care if the certain element is html or body, in theory you could have a rule like p>body>html, and it would be valid CSS since it follows the css rules, of course such rule would never be used because is not valid html... and not even the worse websites I have seen have something like that XD )the slash one (blue/9) it is based on a bug in the IE8 css parser and its not valid css.
chrisl
oh! and why is called star hack?idk, because it is hack? XD lolok, a hack would be a ugly fix for a problem, using the word 'ugly' meaning that it doesn't follow the standards and/or abuses a bug.now, as far I remember, opera has in Dom something like window.opera.it is wrong that your scripts use that propietary object to detect opera? IF the opera JS runtime allowed things like... vars with semicolons in their name, that would be buggy (like var p;e;op;l;e = "blah";)
chrisl
window.opera is a proprietary object that follow the Dom rules.the unknown element before html in IE, DON'T follow the HTML rules, but it doesn't break the CSS rules.since we are using that unknown element to detect IE we are using a bug, but is not a CSS bug, it is a HTML bug...so yeah IT is a hack but no CSS hack (it dont abuse ANY error in css)the star hack would be an "html bug abused using valid css"XDBUT! in the end, you are right. ;)its better to use things like html>body, or things like the :not selector instead of using the star thing(all this when you can't use the if IE tag)
chrisl
A: 

I realize this is an old question but it was the first result on Google when I searched and I think I have found a better solution than the highest ranked suggestion and what the OP chose to do.

#nav li ul:not(.stupidIE) { color:red }

So technically this is the opposite of what the OP wanted, but that just means you have to apply the rule you want for IE8 first and then apply this for everything else. Of course you can put anything inside the () as long as it is valid css that doesn't actually select anything. IE8 chokes on this line and doesn't apply it, but previous IEs (ok I only checked IE7, I have stopped caring about IE6), just ignore the :not() and do apply the declarations. And of course every other browser (I tested Safari 5, Opera 10.5, Firefox 3.6) applies that css as you would expect.

So this solution, I guess like any other pure CSS solution would assume that if the IE developers add support for the :not selector then they will also fix what ever discrepancy was causing you to target IE8.

Moss
A: 

"Why would you want to target IE8 only? If your CSS is well written, you should not have any problems in IE8."

How about you just answer the fucking question and stop being a dip-shit.

Blah
I must say you're not doing any better by adding another pointless answer.
vlad003
True, but he has a point, although a little more strongly-worded than I was thinking... :) @mdja's answer is on the pretentious unhelpful side.
Wick
A: 

I'm not going to get in a debate about whether or not this method should be used, but this will let you set specific css attributes for IE8 only (note: it is not a selector, so a bit different than what you asked):

Use '\0/' after each css declaration, so:

#nav li ul  {
  left: -39px\0/ !important;
}

And to build off another answer, you can do this to assign variou styles to IE6, IE7, and IE8:

#nav li ul  {
   *left: -7px    !important; /* IE 7 (IE6 also uses this, so put it first) */
   _left: -6px    !important; /* IE 6 */
    left: -8px\0/ !important; /* IE 8 */
}

source: http://dimox.net/personal-css-hacks-for-ie6-ie7-ie8/

Nick
+1  A: 

I have a solution that I use only when I have to, after I build my html & css valid and working in most browsers, I do the occasional hack with this amazing piece of javascript from Rafael Lima. http://rafael.adm.br/css_browser_selector/

It keeps my CSS & HTML valid and clean, I know it's not the ideal solution, using javascript to fix hacks, but as long as your code is originally as close as possible (silly IE just breaks things sometimes) then moving something a few px with javascript isn't as big of a deal as some people think. Plus for time/cost reasons is a quick & easy fix.

Nicknameless