CSS expression in IE: Dynamic Style Binding Principles and Usage
This article explains the IE-specific CSS expression mechanism, including how the browser parses, evaluates, and applies expressions; two common use cases; a syntax pitfall; and the performance problems that led to its deprecation. It also outlines practical lessons for modern dynamic styling.
What Is expression?
expression is a CSS property-binding mechanism unique to Internet Explorer 5 and later. It belongs to the CSS Behaviors category. Its core idea is that a CSS property value does not have to be a static literal; it can be a JavaScript expression. The browser evaluates that expression and uses the result as the final value of the CSS property.
In other words, under the expression mechanism, code written in a CSS declaration no longer only describes “what the style is”; it describes “how the style should be calculated.” This gave CSS the ability to perform dynamic evaluation. At a time when mature standardized solutions were lacking, it provided developers with a shortcut for implementing dynamic effects with what appeared to be pure CSS.
Technically, expression embeds a JavaScript expression inside a CSS declaration. Within the expression, developers can directly reference the element’s own properties and methods, and can also access other browser objects. The expression behaves as if it were running inside a member function of that element, with direct access to the current element’s context.
How It Works
The expression mechanism can be broken down into three layers:
1. Parsing layer: When the browser parses a CSS declaration and finds that a property value starts with expression(, it identifies the value as a script expression rather than an ordinary style value.
2. Evaluation layer: The browser evaluates the JavaScript code inside the parentheses. During evaluation, this points to the element to which the style is being applied, so the code can directly call the element’s properties and methods and can also access browser objects such as document.
3. Application layer: The result returned by the expression is assigned to the corresponding CSS property. If the property is a native property such as width, the result directly changes the element’s rendering. If the property is a developer-defined property such as star in the examples below, the effect is to “mount” a piece of script logic onto every matched element, and operations such as event binding in the script are executed in sequence.
It is important to note that these expressions are not executed only once when the page loads. They are repeatedly triggered when the browser believes that conditions which may affect the result have changed. This “frequent evaluation” characteristic is the root cause of its performance problems, as discussed later.
Two Typical Use Cases
Use Case 1: Assigning Values to Native Element Properties
The most common usage is to make a CSS property dynamically calculated based on browser state. For example, the following code limits a container’s width to 725 pixels while allowing it to adapt when the window is narrower:
#container {width: expression(document.documentElement.clientWidth > 725 ? "725px" : "auto");}
The logic is: when the viewport width is greater than 725px, the container width is fixed at 725px; otherwise, auto is used so that the container shrinks with the viewport. Before modern responsive layout solutions became widespread, this kind of code was widely used to simulate a “maximum width” effect, because early versions of IE did not fully support the max-width property. expression therefore became an alternative implementation.
Use Case 2: Assigning Values to Custom Element Properties
Another use of expression is more ingenious: since arbitrary scripts can be executed inside an expression, a custom property can be used as a “mount point” to apply script logic in bulk to all matched elements.
A classic example is removing the dotted focus outline from links on a page. The traditional approach is to write an event handler for each link element individually:
<a href="#" onfocus="this.blur()">link1</a><a href="#" onfocus="this.blur()">link2</a><a href="#" onfocus="this.blur()">link3</a>
When a page has only one or two links, this approach is acceptable. But if the page has dozens or even hundreds of links, copying and pasting the handler for each one is tedious, produces a large amount of redundant code, and any omission during later maintenance can break overall consistency.
With expression, only one line of CSS is needed:
a { star: expression(this.onfocus = this.blur()); }
Here, star is an arbitrary custom property name defined by the developer and can be named according to personal preference. The browser executes the script inside the expression for every element matching the a selector, binding the blur behavior to that element’s focus event. In this way, the dotted focus outline can be removed from all links on the page at once.
Syntax details to note: this declaration still needs to be written inside a style tag rather than a script tag, because its nature is still CSS. The colon between the custom property and the expression, as well as the expression syntax, must conform to the structure of a CSS declaration.
A Common Syntax Pitfall
A common mistake for beginners using expression is this: when the triggered event needs to change a CSS style property, writing the style assignment statement directly in the event handler position causes a script error. For example, to change the background color of a text input on mouseover and mouseout, an intuitive but incorrect approach might be:
/* Incorrect: causes a script error */input {star: expression(onmouseover = this.style.backgroundColor = "#F5F5F5";onmouseout = this.style.backgroundColor = "#FFFFFF");}
The problem with this approach is that the expression context cannot correctly handle the execution logic of chained assignments. The correct approach is to wrap the style changes in functions and then bind those functions to the events:
/* Correct: wrap the style definitions in functions */input {star: expression(onmouseover = function() { this.style.backgroundColor = "#FF0000"; },onmouseout = function() { this.style.backgroundColor = "#FFFFFF"; });}
The difference between the two forms may seem subtle, but it reflects an essential constraint of expression: the code inside it must follow JavaScript statement and expression rules, and event-handling logic must be fully defined as functions so that it can be evaluated and bound correctly.
Performance Problems and Official Guidance
Although expression is flexible, it has an unavoidable flaw: poor execution efficiency.
The reason is that the browser cannot accurately determine which conditions affect the result of an expression, so it tends to re-evaluate expressions repeatedly during frequent operations such as scrolling the page, resizing the window, and moving the mouse. Every expression declaration on a page effectively introduces a continuously running script loop. When a page contains many elements and complex expressions, these evaluations consume a large amount of CPU resources, causing slow page response and browser lag.
For this reason, official guidance explicitly recommends avoiding expression unless there is truly no more suitable implementation. In practical development, the following principles should also be followed:
- Prefer standard solutions: for example, use the
max-widthproperty for maximum-width requirements instead of dynamically calculating width. - If an event binding can solve the problem, do not use CSS to mount scripts: bind events uniformly at the page framework or script level. This is semantically clearer and easier to maintain.
- If
expressionmust be used, simplify expressions as much as possible and limit the number of elements to which the style is applied, reducing the overhead caused by frequent evaluation.
Summary and Lessons
As a product of a specific historical period, expression reflects the early front-end approach of “borrowing script capabilities to compensate for insufficient CSS” when standard capabilities were limited. It allowed CSS properties to be dynamically bound to JavaScript expressions. It could calculate native property values and could also use custom properties to mount interaction logic in bulk. It was once popular in scenarios such as removing link focus outlines and simulating responsive widths.
However, the performance cost caused by its repeated evaluation mechanism was fatal. In addition, the feature never entered the W3C standards and was supported only by specific browsers. With modern browsers’ full implementation of CSS3 standards and the maturity of JavaScript frameworks, expression has been eliminated.
For today’s developers, understanding expression is valuable in two main ways. First, it helps clarify the underlying idea of dynamic styling: “let styles be calculated based on conditions.” This idea has been standardized and continued in modern solutions such as CSS custom properties combined with JavaScript, media queries, and container queries. Second, its performance lessons show that any solution that “makes the browser perform repeated calculations frequently” should be carefully evaluated. This remains practically relevant for writing high-quality front-end code.
