Issue link: https://read.uberflip.com/i/1146286
52 | Chapter 7: Rule 7: Avoid CSS Expressions Updating Expressions The problem with expressions is that they are evaluated more frequently than most people expect. Not only are they evaluated whenever the page is rendered and resized, but also when the page is scrolled and even when the user moves the mouse over the page. Adding a counter to the CSS expression allows us to keep track of when and how often a CSS expression is evaluated. Expression Counter http://stevesouders.com/hpws/expression-counter.php The CSS expression counter example has the following CSS rule: P { width: expression( setCntr( ), document.body.clientWidth<600 ? "600px" : "auto" ); min-width: 600px; border: 1px solid; } The setCntr( ) function increments a global variable and writes the value in the text field in the page. There are 10 paragraphs in the page. Loading the page executes the CSS expression 40 times. Subsequent to that, the CSS expression is evaluated 10 times for various events including resize, scrolling, and mouse movements. Moving the mouse around the page can easily generate more than 10,000 evaluations. The danger of CSS expressions is evident in this example. Worst of all, clicking in the text input field locks up Internet Explorer, and you have to kill the process. Working Around the Problem Most CSS experts are familiar with CSS expressions and how to avoid the pitfalls highlighted by the previous example. Two techniques for avoiding problems created by CSS expressions are creating one-time expressions and using event handlers instead of CSS expressions. One-Time Expressions If the CSS expression has to be evaluated only once, it can overwrite itself as part of its execution. The background style defined at the beginning of this chapter is a good candidate for this approach:

