I want to target IE7 and IE8 with W3C-compliant CSS. Sometimes fixing CSS for one version does not fix for the other. How can I achieve this?
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- Is TWebBrowser dependant on IE version?
The answer to your question
A completely valid way to select all browsers but IE8 and below is using the
:not()
pseudo-class. Since IE versions 8 and below do not support:not()
, selectors containing it are ignored. This means you could do something like this:This is still completely valid CSS, but it does cause IE8 and lower to render different styles (and also Opera<9.5 and Safari<3.2).
Other tricks
Here's a list of all completely valid CSS browser-specific selectors I could find, except for some that seem quite redundant, such as ones that select for just 1 type of ancient browser (1, 2):
Credits & sources:
:first-child
;:root
;:first-of-type
;:not()
;:any()
;* ~ *
The actual problem is not IE8, but the hacks that you use for earlier versions of IE.
IE8 is pretty close to be standards compliant, so you shouldn't need any hacks at all for it, perhaps only some tweaks. The problem is if you are using some hacks for IE6 and IE7; you will have to make sure that they only apply to those versions and not IE8.
I made the web site of our company compatible with IE8 a while ago. The only thing that I actually changed was adding the meta tag that tells IE that the pages are IE8 compliant...
Explicitly Target IE versions without hacks using HTML and CSS
Use this approach if you don't want hacks in your CSS. Add a browser-unique class to the
<html>
element so you can select based on browser later.Example
Then in your CSS you can very strictly access your target browser.
Example
For more information check out http://html5boilerplate.com/
Target IE versions with CSS "Hacks"
More to your point, here are the hacks that let you target IE versions.
Use "\9" to target IE8 and below.
Use "*" to target IE7 and below.
Use "_" to target IE6.
Example:
Update: Target IE10
IE10 does not recognize the conditional statements so you can use this to apply an "ie10" class to the
<html>
elementI did it using Javascript. I add three css classes to the html element:
So for IE7, it adds
ie7
,lte-ie7
...,lt-ie8
...Here is the javascript code:
Thereafter, you use the
.ie<version>
css class in your stylesheet as described by potench.(Used Mario's detectIE function in Check if user is using IE with jQuery)
The benefit of having lte-ie8 and lt-ie8 etc is that it you can target all browser less than or equal to IE9, that is IE7 - IE9.
I would recommend looking into conditional comments and making a separate sheet for the IEs you are having problems with.
Well you don't really have to worry about IE7 code not working in IE8 because IE8 has compatibility mode (it can render pages the same as IE7). But if you still want to target different versions of IE, a way that's been done for a while now is to either use conditional comments or begin your css rule with a * to target IE7 and below. Or you could pay attention to user agent on the servers and dish up a different CSS file based on that information.