CSS Expressions

HtmlCssCss Expressions

Html Problem Overview


I read somewhere that CSS Expressions were deprecated and shouldn't even be used. I had never heard of them and decided to take a look. I found a code example that kept a floating element in the same spot on the screen, even if you scrolled.

<html>
<style>
#fixed {
position:absolute;
left:10px;
top:expression(body.scrollTop + 50 + "px");
background:white;
border:1px solid red;}
</style>
<body>
<p id="fixed">Here is some text, which is fixed.</p>
<p>
[many times: "stuff <br/>"]
</p>
</body>
</html>

This reminded me of the sites that had the "share bars" and stuff at the bottom of their pages.

So...

  1. Is this how they are doing it?
  2. Is it alright to use Expressions in this situation?
  3. If not, what should I use?
  4. Are there any other interesting/helpful things that expressions can help with?

Html Solutions


Solution 1 - Html

CSS expressions used to work in older IE's, but they have been completely abandoned in IE8:

> Dynamic properties (also called "CSS expressions") are no longer supported in Internet Explorer 8 and later, in IE8 Standards mode and higher. This decision was made for standards compliance, browser performance, and security reasons, as detailed in the IE blog entry titled Ending Expressions. Dynamic properties are still available in Internet Explorer 8 in either IE7 mode or IE5 mode.

So it's arguably not really worth learning them any more.

> If not, what should I use?

Depending on the use case, JavaScript or media queries.

As @Yet Another Geek notes, your above example can be implemented using position: fixed. IE6 doesn't support that - the CSS expression was probably created to work around that.

Solution 2 - Html

For keeping an element in the same place while scrolling you should use the position:fixed property and then use top,bottom,left and right properties to tell where it should be positioned.

EDIT: Here how it should be for your example:

<html>
<style>
#fixed {
position:fixed;
left:10px;
top: 50px;
background:white;
border:1px solid red;}
</style>
<body>
<p id="fixed">Here is some text, which is fixed.</p>
<p>
[many times: "stuff <br/>"]
</p>
</body>
</html>

Solution 3 - Html

  1. Probably not. It's easier to use position:fixed or Javascript
  2. Not unless you're only supporting IE < 8. IE8+ and other browsers don't support it*; it's not standards compliant and won't pass validation
  3. Use position:fixed; bottom:x; top:y; left: a; right:b; where x, y, a, and b are offsets. Or, use Javascript
  4. Again, not unless you're using old versions of IE only. Really, just dump it. the same effects can be achieved with JS and normal CSS.

*Officially, anyways. Apparently it worked on Chrome for @DalexL

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFreesn&#246;wView Question on Stackoverflow
Solution 1 - HtmlPekkaView Answer on Stackoverflow
Solution 2 - HtmlYet Another GeekView Answer on Stackoverflow
Solution 3 - HtmlThomas ShieldsView Answer on Stackoverflow