How to workaround: IE6 does not support CSS "attribute" selectors
Asked Answered
G

7

14

One of the projects which I am working uses CSS "attribute" selector [att]

CSS Selectors

which is not supported by ie6: Support for CSS selectors in IE6 (look for text "Attribute Selectors")

Is there any workaround/hack which is of course valid html/css to overcome this problem?

Gus answered 16/3, 2009 at 5:15 Comment(0)
A
20

Since IE6 is essentially limited to:

  • class selectors
  • ID selectors
  • (space) descendant selectors
  • a:-only pseudo-selectors

your only options are:

  • Use more classes to identify your elements
  • Use JavaScript (strongly not recommended except in highly specialized cases)

I find it very helpful to take advantage of the ability to assign multiple classes to an element by separating them with a space: class="foo bar"

Amalburga answered 16/3, 2009 at 5:18 Comment(8)
is <input type="button" class="foo" class="bar" /> valid html?Gus
@ngm no, duplicate attribute specifications are not allowedAmalburga
@ngm you can however use class="foo bar"Utile
@Rex M and @Ngm, Multiple classname styles are ignored by IE6 e.g. .foo.bar {}Amaranth
@Amaranth that's true but beside the point in this case. just because multiple classes on the same level can't be targeted in IE6 doesn't mean we cant use multiple classes to apply different styles to overlapping sets of elements.Amalburga
@ndorfin: it's not that they are ignored, it's that IE6 transfers the rules to all classes in the rule, regardless of if all classes are applied to the element.Roselynroseman
@crescentfresh i am not sure if that is true. targeting a.foo.bar in IE6 does not seem to work.Amalburga
@Rex: <a class="foo"... will get your foo styles and your bar styles, even though "bar" is not an assigned class. See interfacethis.com/misc/css-test/multiclass.htmlRoselynroseman
R
26

This isn't possible without peppering your HTML with a stack of extraneous class selectors, sadly.

I'd recommend designing your site so that your entirely valid CSS works for people using modern browsers, and that it's still usable in the IE6, albeit visually not quite right. You just have to find the right balance between getting your site up to standard and bending over backwards for users who won't upgrade. It's a broken browser, treat it as such.

Retiarius answered 16/3, 2009 at 6:0 Comment(2)
Yeah this is the way forward. Developers have got to stop trying to make things pixel-perfect in IE6. For example I've set up some columns using the :first-child pseudo-selector to remove the left margin on the first column. For IE6 I just made the margins smaller so it doesn't mess up.Saffian
It is possible with CSS expressions. See https://mcmap.net/q/260919/-how-to-workaround-ie6-does-not-support-css-quot-attribute-quot-selectors/143739Susansusana
A
20

Since IE6 is essentially limited to:

  • class selectors
  • ID selectors
  • (space) descendant selectors
  • a:-only pseudo-selectors

your only options are:

  • Use more classes to identify your elements
  • Use JavaScript (strongly not recommended except in highly specialized cases)

I find it very helpful to take advantage of the ability to assign multiple classes to an element by separating them with a space: class="foo bar"

Amalburga answered 16/3, 2009 at 5:18 Comment(8)
is <input type="button" class="foo" class="bar" /> valid html?Gus
@ngm no, duplicate attribute specifications are not allowedAmalburga
@ngm you can however use class="foo bar"Utile
@Rex M and @Ngm, Multiple classname styles are ignored by IE6 e.g. .foo.bar {}Amaranth
@Amaranth that's true but beside the point in this case. just because multiple classes on the same level can't be targeted in IE6 doesn't mean we cant use multiple classes to apply different styles to overlapping sets of elements.Amalburga
@ndorfin: it's not that they are ignored, it's that IE6 transfers the rules to all classes in the rule, regardless of if all classes are applied to the element.Roselynroseman
@crescentfresh i am not sure if that is true. targeting a.foo.bar in IE6 does not seem to work.Amalburga
@Rex: <a class="foo"... will get your foo styles and your bar styles, even though "bar" is not an assigned class. See interfacethis.com/misc/css-test/multiclass.htmlRoselynroseman
U
8

If you want attribute selector in IE6, you can use Dean Edward IE.js. http://dean.edwards.name/IE7/

That will make IE 5+ behave much more like IE7

supports the following CSS selectors: 
parent > child
adjacent + sibling
adjacent ~ sibling
[attr], [attr="value"], [attr~="value"] etc
.multiple.classes (fixes bug)
:hover, :active, :focus (for all elements)
:first-child, :last-child, only-child, nth-child, nth-last-child
:checked, :disabled, :enabled
:empty, :contains(), :not()
:before/:after/content:
:lang()

I didn't have IE6 (use IE7) so i can't said it worked, but give it a try

Utile answered 16/3, 2009 at 6:21 Comment(1)
This is a good option but be aware, (and this isn't from experience but from looking at the code and how it implements support) that your CSS rules will not automatically "apply" to dynamic changes to elements that happen through JS - adding classes, elements, etc. will not get the original CSS rules.Maximamaximal
S
6

You can use Internet Explorer CSS expressions combined with the safe underscore ("_", IE6 and earlier) CSS hack:

/* Adds dotted bottom border to `<ABBR>` with a `title` attribute. */
abbr {
        _border-bottom: expression(this.title ? '1px dotted' : 'none');
}

abbr[title] {
        border-bottom: 1px dotted;
}

I do understand, that you did ask for "valid" CSS, but if the CSS hack above freaks you out, please read about Safe CSS Hacks.

The above could be changed to:

.ie6 abbr {
        _border-bottom: expression(this.title ? '1px dotted' : 'none');
}

abbr[title] {
        border-bottom: 1px dotted;
}

That is if your HTML began as:

<!--[if lt IE 7]><html class="ie6"><![endif]-->
<!--[if IE 7]>   <html class="ie7"><![endif]-->
<!--[if IE 8]>   <html class="ie8"><![endif]-->
<!--[if gt IE 8]><!--><html><!--<![endif]-->
Susansusana answered 22/11, 2011 at 18:12 Comment(2)
+1 for the expression() statement. I've been building websites and crafting CSS for 12 years and it's the first time in my life to see this wizzardy cool shit!Keyway
It doesn't work in IE8. They finally took it out due to security issues, but it does work in IE7. IE also has a thing called "behaviors" in CSS.Susansusana
M
5

Dean Edwards' IE7 JavaScript library provides attribute selector support for IE 5 and 6.

Macklin answered 16/3, 2009 at 6:17 Comment(0)
S
2

Use classes, that's the only workaround, sadly.

Sweeping answered 16/3, 2009 at 5:19 Comment(3)
is <input type="button" class="foo" class="bar" /> valid html?Gus
use class="button bar" insteadSweeping
@ngm you can use class="foo bar"Utile
G
0

If you're using jQuery on your site, another option is SuperSelectors - it goes through your site’s stylesheets, dynamically adding classes to elements so that even IE6 can bask in the glow of properly supporting CSS selectors like ul li:first-child li:nth-child(odd) a:hover.

Gittle answered 21/4, 2009 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.