A powerful feature within Less is the ability to use basic JavaScript to generate CSS, such as using the screen.height
function to determine how big your visitor's screen is. In this exercise we're going to demonstrate the power of using JavaScript within Less to draw a simple shape on screen, which uses the screen.height
function of JavaScript.
test Less javascript evaluation.html
:<body>
<div id="testbox">This is test number 1</div>
</body>
test Less javascript evaluation.less
:@height: `screen.width`; #testbox { background-color: orange; height: 100px; width: ~`@{width}/2 + "px"`; }
Although this is an overly simplified example, it serves to illustrate how you can use JavaScript to determine how big the box should appear. The use of LESS code is based around standard JavaScript code, but must use the ~`…`
format to ensure that it is correctly interpreted by the compiler:
#testbox { background-color: orange; height: 100px; width: 640px; }