Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed Javascript in CSS?

Tags:

javascript

css

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?

like image 222
DNB5brims Avatar asked May 30 '26 21:05

DNB5brims


1 Answers

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>
like image 175
Lucas Jones Avatar answered Jun 02 '26 09:06

Lucas Jones