2

I have a CSS file, which contain the style information of my web. Some height & width is based on the screen size, so I can calculate the size using the Javascript, can I embedded the javascript in the CSS file to do so?

2 Answers 2

7

In IE, and only IE, you can use CSS expressions:

width: expression(blah + "px");

Then width becomes whatever's inside the brackets.

This only works in IE, though - so don't use it. Use a JS function to assign the elements the style with element.style.width or similar.

For example:

<script type="text/javascript">
    document.getElementById('header').style.width = (100 * 2) + 'px';
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

Shouldn't the example use .STYLE.width as well?
Now that you mention it, I'm not actually sure... I'll just check.
Using expressions are so bad that they took it out of IE8
@bobince: Thanks. @epascarello: They did? Thank God. :)
Or with jQuery: $("#header").css("width", 100 * 2)
|
2

I don't think so, but you can do the opposite, i.e. make the javascript create an inline css on the fly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.