Using javascript expressions in CSS

by Armand Niculescu Email
  • Currently 2.97/5
  • 1
  • 2
  • 3
  • 4
  • 5
  • i

I usually prefer a fixed layout for pages, because this allows for more control over typography, readability and so on.

Still, one of biggest clients specifically required two years ago their site to be built with a liquid layout that expands full-width; the whole site (about 2000 html and aspx pages) was eventually done this way and everybody was happy…

…until last week.

The directors just got some new fancy widescreen laptops and they realized the site doesn’t look very nice when spread to 1920 pixels wide. So I was told "the site should not expand more than 1024 pixels, but also not shrink below 800 pixels."

This would be relatively easy to do with a simple CSS rule like

CSS:

{
    min-width: 760px;
    max-width: 955px;
  }

Alas, our good ol’ friend IE6 doesn’t support min-width and max-width properties. I found however a very nice little trick for IE. It looks like you can actually use javascript expressions in CSS, like this:

CSS:

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

I know it’s not standards-compliant, but it works and doesn’t bother the other browsers.

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

This is the blog of the Media Division team. We're giving back to the community some of the things we have learned while building all sorts of apps. Concentrating on Flash/Actionscript, we'll also cover C#, MSSQL, ColdFusion/Oracle and areas like Photography and design. We're writing original articles only - no silly stuff to generate more traffic.

Search

XML Feeds

Aggregated by MXNA

Aggregated by MXNA

Related Links