If you have spent any time writing programming code, you should already be scoping and using it to control a variable depending on where it has been set in code. Variable scope in LESS is controlled in the same way. Let's take a look at how it works within LESS, using a simple example of styling some <div>
tags.
<head>
section:<link rel="stylesheet" type="text/less" href="scoping.less">
<body>
section as follows and save it as test less scoping.html
:<body> <div class="item item-1"></div> <div class="wrapper w1"> <div class="item item-1">List Item 1</div> <div class="item item-2">List Item 2</div> </div> <div class="wrapper w2"> <div class="item item-1">List Item 3</div> <div class="item item-2">List Item 4</div> <div class="item item-3">List Item 5</div> <div class="item item-4">List Item 6</div> </div> </body>
scoping.less
and add the following lines of code, starting with the base variables://Default Variables @width: 98.5%; @height: 50px; @background: #333; @fontcolor: #fff;
.item
:.mixin(@width: @width, @height: @height, @background: @background) { width: @width; height: @height; background: @background; float: left; color: @fontcolor; font-weight: bold; font: 22px Arial, Helvetica, sans-serif; margin: 3px; padding: 5px; } .item { .mixin(); }
/* Overrides default variables for any .item in the .wrapper div */ .wrapper { @height: 20px; //Change height @background: #360; //Change background color .item { .mixin(); } } .w1 { @width: 48.6%; //Change width @background: #854; //Change background color padding-top: 10px; .item { .mixin(); } } .w2 { @width: 23.7%; //Change width @height: 55px; //Change height .item { .mixin(); } }
Remember how we looked at mixins at the start of this book? Well, we've used one here to illustrate scopes in action. If you were to look at the HTML on its own, you might expect the first two <div>
tags within wrapper w1
to be contained within the <div>
tag of wrapper w1
. However, this won't be the case; the base styles we included in the mixin being used for .item
are being overridden by additional styles that have been set up for w1
and w2
that override the base wrapper style. We can see the effects of the overridden styles by looking at the compiled CSS from within something like Firebug:
/* Uses the Default Mixin */ .item { width: 99.5%; height: 50px; background: #333333; float: left; color: #ffffff; font-weight: bold; font: 22px Arial, Helvetica, sans-serif; margin: 3px; padding: 5px; } /* Overrides default variables for any .item in the .wrapper div */ .wrapper .item { width: 99.5%; height: 20px; background: #336600; float: left; color: #ffffff; font-weight: bold; font: 22px Arial, Helvetica, sans-serif; margin: 3px; padding: 5px; } .w1 .item { width: 48.6%; height: 50px; background: #885544; float: left; color: #ffffff; font-weight: bold; font: 22px Arial, Helvetica, sans-serif; margin: 3px; padding: 5px; } .w2 .item { width: 23.7%; height: 55px; background: #333333; float: left; color: #ffffff; font-weight: bold; font: 22px Arial, Helvetica, sans-serif; margin: 3px; padding: 5px; }
The downside of using scopes in this manner is that you may get some duplication appear. It just means that you need to take care of which styles are used in the base mixin to help reduce unnecessary duplication.
It's time to move away from theory; we've looked at most parts of the LESS library to see how you can use it to help structure your CSS more effectively. Let's move away from theory, and turn our focus to looking at some real-life examples, beginning with using it to transform some images.