tags:

views:

1574

answers:

8

Is it possible to use Javascript inside CSS?

if it is. Can give a simple example?

+1  A: 

Not in any conventional sense of the phrase "inside CSS."

Hank Gay
+2  A: 

You can manipulate CSS with Javascript. For example, I can change the color of a paragraph like this:

document.getElementById("myp").style.color = "#336699";
<p id="myp">This is my story. This is my song.</p>

I believe that line is correct - I rarely write javascript without using a framework like jQuery, which makes things a lot easier:

$("p#myp").css("color","#336699");
Jonathan Sampson
A: 

I'm afraid not. You can however dynamically generate CSS with Javascript, I believe (by setting style tags or possibly even generating blocks). If you're using a server-side language and want dynamic CSS, then there are solutions you can find for that. (For ASP.NET, see this for example.)

Noldorin
http://www.quirksmode.org/dom/w3c_css.html
geowa4
+14  A: 

I think what you may be thinking of is expressions or "dynamic properties", which are only supported by IE and let you set a property to the result of a javascript expression. Example:

width:expression(document.body.clientWidth > 800? "800px": "auto" );

This code makes IE emulate the max-width property it doesn't support.

All things considered, however, avoid using these. They are a bad, bad thing.

Paolo Bergantino
key part there being 'only supported by IE'; unless that css file is being included via a conditional comment, it should be avoided.
geowa4
It should be avoided in any case: http://developer.yahoo.com/performance/rules.html#css_expressions
Gumbo
Note also standards mode IE8 doesn't support expressions in CSS either.
AnthonyWJones
A: 

IE supports CSS expressions:

width:expression(document.body.clientWidth > 955 ? "955px": "100%" );

but they are not standard and are not portable across browsers. Avoid them if possible. They are deprecated since IE8.

jetxee
A: 

In short, no, but why don't you give us your reasoning for wanting it, or your problem, and I'm sure we can help you out with it!! Logically, I'm sure there's a simple alternative to trying to put js code in css that we can help you out with.

Cyprus106
A: 

To facilitate potentially solving your problem given the information you've provided, I'm going to assume you're seeking dynamic CSS. If this is the case, you can use a server-side scripting language to do so. For example (and I absolutely love doing things like this):

styles.css.php:

body
 {
margin: 0px;
font-family: Verdana;
background-color: #cccccc;
background-image: url('<?php
echo 'images/flag_bg/' . $user_country . '.png';
?>');
 }

This would set the background image to whatever was stored in the $user_country variable. This is only one example of dynamic CSS; there are virtually limitless possibilities when combining CSS and server-side code. Another case would be doing something like allowing the user to create a custom theme, storing it in a database, and then using PHP to set various properties, like so:

user_theme.css.php:

body
 {
background-color: <?php echo $user_theme['BG_COLOR']; ?>;
color: <?php echo $user_theme['COLOR']; ?>;
font-family: <?php echo $user_theme['FONT']; ?>;
 }

#panel
 {
font-size: <?php echo $user_theme['FONT_SIZE']; ?>;
background-image: <?php echo $user_theme['PANEL_BG']; ?>;
 }

Once again, though, this is merely an off-the-top-of-the-head example; harnessing the power of dynamic CSS via server-side scripting can lead to some pretty incredible stuff.

Hexagon Theory
+5  A: 

IE and Firefox both contain ways to execute JavaScript from CSS. As Paolo mentions, one way in IE is the expression technique, but there's also the more obscure HTC behavior, in which a seperate XML that contains your script is loaded via CSS. A similar technique for Firefox exists, using XBL. These techniques don't exectue JavaScript from CSS directly, but the effect is the same.

HTC with IE

Use a CSS rule like so:

body {
  behavior:url(script.htc);
}

and within that script.htc file have something like:

<PUBLIC:COMPONENT TAGNAME="xss">
   <PUBLIC:ATTACH EVENT="ondocumentready" ONEVENT="main()" LITERALCONTENT="false"/>
</PUBLIC:COMPONENT>
<SCRIPT>
   function main() 
   {
     alert("HTC script executed.");
   }
</SCRIPT>

The HTC file executes the main() function on the event ondocumentready (referring to the HTC document's readiness.)

XBL with Firefox

Firefox supports a similar XML-script-executing hack, using XBL.

Use a CSS rule like so:

body {
  -moz-binding: url(script.xml#mycode);
}

and within your script.xml:

<?xml version="1.0"?>
<bindings xmlns="http://www.mozilla.org/xbl" xmlns:html="http://www.w3.org/1999/xhtml"&gt;

<binding id="mycode">
  <implementation>
    <constructor>
      alert("XBL script executed.");
    </constructor>
  </implementation>
</binding>

</bindings>

All of the code within the constructor tag will be executed (a good idea to wrap code in a CDATA section.)

In both techniques, the code doesn't execute unless the CSS selector matches an element within the document. By using something like body, it will execute immediately on page load.

Zach
neat, I didn't know about the XBL hack!
scunliffe