views:

126

answers:

4
+1  Q: 

CSS: IE7 Selector

How could I select IE7 with pure (valid) CSS?

+5  A: 

If you don't want to use a conditional comment (outside the CSS, e.g. defining a separare <style> section), the only thing you can use is CSS Hacks. See here for a "IE7 only" hack.

Pekka
though eventually you'll have to hack the hack if they ever fix it
Dan Beam
Yup. Conditional comments are way nicer.
Pekka
+3  A: 

IE does support conditional comments, an IE-specific HTML comment syntax. You can use them to include IE7-specific CSS, e.g.

<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="ie7.css" />
<![endif]-->

There’s no equivalent in CSS, unfortunately. But, as mentioned in other answers, there are some valid CSS hacks you can use to target CSS rules as just IE 7.

I personally prefer the conditional comment syntax as it’s a bit more explicit, but you can make the hacks explicit with comments.

Paul D. Waite
+2  A: 

If you don't want a separate stylesheet for IE hacks, here's another way doing it with using conditional comments:

<!--[if lt IE 7]><body class="ie6"><![endif]-->
<!--[if (gte IE 7)&(lt IE 8)]><body class="ie7"><![endif]-->
<!--[if gte IE 8]><!--><body><!--<![endif]-->
    ...page content...
</body>

This give IE6, IE7 and [all other browsers] a different body element class. Now you can write rules like:

body.ie7 div.scroll { padding-bottom: 16px; }
bobince
interesting idea. this could come in handy...
Dan Beam
A: 

are expressions valid? if so:

cssAttr: expression( /msie 7/i.test( navigator.userAgent ) ? '#ie7val' : '#0th3r1' );

I highly doubt they are though, and technically that's CSS, but it's really JavaScript in disguise!

Dan Beam
No, unfortunately they’re not valid CSS.
Paul D. Waite
Figured, almost anything MS does tends to disagree with the W3C
Dan Beam