Expressions in CSS

Aug 16

Expressions in CSS

About Dynamic Properties: Ever heard of IE’s CSS “expression” property? It allows you to set a CSS property not to a constant, but to the result of a JavaScript expression. Like this:


p
{
width:expression(400 + "px");
}

What this lets you do, according to this really good article, is make IE emulate the max-width CSS property that it really should support like all other good browsers. Like this:


p
{
max-width:800px;
width:expression(document.body.clientWidth > 800? "800px": "auto" );
}

Ugly hack, but crappy browsers will do that.


Comments

by Deane,   September 13, 2004 9:02 PM  

Incidentally, this hack is as good as worthless under Windows XP SP2. I tried it, and I got something I've never seen before:

A little sound effect went off, and a yellow bar appeared under the address bar explaining that "for my protection" the page had been prevented from "displaying ActiveX content."

It gave me the option to allow it, but what good is that? Too bad -- this was a nice way around a stupid, stupid limitation of IE.


by wilpak,   October 14, 2004 11:47 AM  

I think a lot is going to change in the client-side scripting since SP2 blocks most of the JS hacks.


by Deane,   October 14, 2004 11:58 AM  

I agree. Very sad, really -- this was a good way to get IE to do what you wanted, not to mention a great tool for intranet developers standardized on IE.


by jflanigan,   November 14, 2004 1:15 PM  

The ActiveX warning with XP SP2 is not just targeted at JS hacks. Just including an empty script element will cause it to fire.


by Scroll,   February 1, 2006 2:48 PM  

I know the comments are very old but when you upload your website you shouldnt have the yellow bar appear. It only appears when the file is locally for me


by Satya,   February 21, 2006 5:01 PM  

We found a number of machines would hang or otherwise behave weirdly when expression() was used with IE. There may have been other factors like spyware involved.


by Robert K S,   August 4, 2006 1:21 PM  

I can corroborate Satya's comment. Especially, minimizing IE and trying to come back to the browser will hang if expression() is included in a page's CSS. Too bad; it would have been useful.


by Josh,   January 21, 2008 6:56 AM  

I realize that this comment comes almost a year and a half following the last one, but I thought I'd add this: that expression() in css is a javascript line, and any script blocker will block it or inform you that a site is using scripts. Javascript is client-side meaning that it is working on your machine. Things like the markup, php or asp, cgi, or whatever are server-side, but scripts like Javascript will be recognized as some kind of Active-X addon. In this case it's an IE Active-X control that allows expression() css hacks to be utilized.


by Josh,   January 21, 2008 6:57 AM  

I realize that this comment comes almost a year and a half following the last one, but I thought I'd add this: that expression() in css is a javascript line, and any script blocker will block it or inform you that a site is using scripts. Javascript is client-side meaning that it is working on your machine. Things like the markup, php or asp, cgi, or whatever are server-side, but scripts like Javascript will be recognized as some kind of Active-X addon. In this case it's an IE Active-X control that allows expression() css hacks to be utilized.


by Mali Dharam,   May 8, 2008 6:57 AM  

hi guys...

i found it...i am not sure that it will run in all browsers ???

i will check it out. till then u all try your all self.


by Aaron,   May 11, 2008 9:25 PM  

I tend to use these sort of hacks to fix IE specific margins and other quirky behaviour. All good browsers completely ignore it, and I don't see much difference between these expressions and other CSS hacks.


by Smart Pad,   August 4, 2008 1:38 AM  

I never thought there's a thing like css expression. Is this backward compatible? I will definitely try this one. Thanks!


by jake,   August 24, 2008 7:23 PM  

Funny how this article is nearly 4 years old and we are still having to use the same lame hacks to satisfy browser failures.


by rick,   September 22, 2008 6:45 PM  

Couldn't agree more jake. MS fail.


by iGuide,   September 30, 2008 10:16 AM  

But do expressions do anything else besides min/max height/width? Expressions seem to fail miserably for most anything else.


by Josh L,   April 23, 2009 3:21 PM  

@iGuide

I used my first one today (hence finding this page) and it was for something other than min/max properties:

In certain situations where a table is told via CSS to have a width of 100%, IE6 makes the table 100% of the parent's width PLUS the width of the scrollbar, causing the table to spill out of its container. No other browsers including IE7 showed the problem, so in my IE6 stylesheet I set the width of the table to be 100% minus the scrollbar width, and now all's gravy. This is for a semi-liquid AJAX application that will be used across many sites of varying sizes, which is why a static width couldn't be used.

And yes, I was using the table for tabular data. :P


by sadfsdaf,   May 15, 2009 5:36 AM  

dasadsfdas


by damian,   July 5, 2009 11:43 AM  

Great hack! Anyway, this degrades performance a lot, specially in javascript dynamic pages.


by A. Santamaria,   July 20, 2009 9:54 AM  

ATTENTION: I'm afraid I traced the CSS 'expression' function under IE7 and it keeps running continuously even when the result of the expression would never change. Of course, there is no way for the CSS engine to determine whether that expression will change ;)
I recommend not to use that 'hack' unless you want to slow down the machines of the users who visit your web pages.
Remember there are many ways to do stuff ;)


by rana,   October 7, 2009 10:47 PM  

Nice tips, thanks..


by T. Vandierendonck,   October 21, 2009 6:12 AM  

Just fyi, we've discovered the use of "expression" makes IE6 hang when run via Cytrix on Win XP


by Andrei,   May 9, 2010 10:18 AM  

Is it really so hard for Microsoft to add "AUTO UPDATE" feature into their browser instead of coming out with 6 7 8 9 browsers and waiting for pipl to actually update on their own?

This would solve 99 % of all the *ucking hacks:)

makes me sad panda


by Okonomiyaki3000,   November 2, 2010 4:50 AM  

Expressions are certainly very dangerous and the best solution is to stop supporting IE6 altogether. However, if you must use them, try something like this:

p { max-width:800px; width:expression( function(e){ e.style.width = document.body.clientWidth > 800? "800px": "auto" }(this) ); }

This will solve the problem of the element being continuously updated. To explain, the expression executes a function. "this" is the element selected by the css and is passed into the function. Inside the function, the element is referred to as 'e'. Now we set the element's width to whatever it should be which means it is no longer set to 'expression(...)'. The down side is that it will only be set once so if something about the page changes, the element won't live-update with the new correct width.

A more complete solution might be to use 'debouncing' to prevent the element from updating too often but then you're going to need to include a lot of javascript in your css and it's already ugly enough.

How about just telling people stuck with IE6 to use Chrome Frame instead?


by huj,   March 18, 2011 12:59 PM  

What de fack



Add Comment