14

Is it possible to use the noscript element in CSS selectors?

noscript p {
    font-weight: bold;
}
nickf
  • 537,072
  • 198
  • 649
  • 721
  • Anyone facing the same problem should probably have a look at [that question](http://stackoverflow.com/a/9503382/2657549). – Clément Nov 08 '14 at 01:16

3 Answers3

16

Yes! You can definitely do that.

In fact, many (all?) browsers support targeting any arbitrary tag using CSS. "Official" tags in the HTML spec only define what a browser should do with them. But CSS is a language that targets any flavor of XML, so you can say foo {font-weight:bold;} and in most browsers, <foo> hello world </foo> will come out bold.

As Darko Z clarifies, IE6/7 do not add arbitrary (non-standard) elements to the DOM automatically from the source; they have to be programmatically added.

Community
  • 1
  • 1
Rex M
  • 142,167
  • 33
  • 283
  • 313
9

I have an IE bug to keep in mind. If, like OP, you just want to style text within the noscript tag and not the noscript tag itself, please disregard this..

Say you want to make the noscript tag's background red. In IE8, it will show up with JS enabled. Just the box itself, not the text inside.

So this combination isn't good:

CSS

noscript {
    background-color: red;
}

HTML

<noscript>Turn on your damned JavaScript! What is this, 1999?</noscript>

But this workaround works fine:

CSS

noscript div {
    background-color: red;
}

HTML

<noscript><div>Turn on your damned JavaScript! What is this, 1999?</div></noscript>

Weirdly, I only see this behavior in IE8, not IE7. And who knows about 6..

mattalxndr
  • 9,143
  • 8
  • 56
  • 87
  • It's an IE bug: http://www.positioniseverything.net/explorer/ie8stylednoscriptbug/index.html. Thanks for the workaround through, I was hiding the `noscript` element with Javascript, which was terribly inefficient. This bug also shows in the IE9 beta – Yi Jiang Sep 24 '10 at 10:42
3

In addition to Rex M's answer - IE 6/7 (6 def, 7 maybe?) will not style an arbitrary tag for you. But lucky for you as with all many IE problems there's a workaround.

Say you want to style an element called foo. To get IE to recognise it as a styleable element you need to include this line somewhere in your JS:

document.createElement('foo');

You don't need to append it, just create it.

This will kick IE into styling that element for you with the CSS rule:

foo { font-weight:bold; }
Community
  • 1
  • 1
Darko
  • 38,310
  • 15
  • 80
  • 107
  • 2
    thanks, but I have to disagree with your point about "as with all IE problems theres a workaround."... :p – nickf Mar 24 '09 at 04:24
  • 1
    Given that the question is about the `noscript` element, your workaround wouldn't seem very useful. – user2736012 Sep 11 '13 at 23:32
  • @user2736012 if you substitute `foo` with `noscript` then it is. I'm not sure I understand your point. – Darko Sep 12 '13 at 14:48
  • 5
    @DarkoZ: Your solution requires JavaScript. If JavaScript is disabled, the solution won't be able to run. If JavaScript is enabled, it runs, but the content is hidden anyway. – user2736012 Sep 12 '13 at 14:49
  • @user2736012 couldn't see the forest for the trees! Well pointed out – Darko Sep 12 '13 at 14:52