Amit Jain
Front-end engineer, CSS, HTML and JavaScript junkie and web lover. Senior UI developer @ iXiGO.com
Basic building blocks of all web Components
Cascade?
The cascade is a mechanism for determining which styles should be applied to a given element, based on the rules that have cascaded down from various sources.
Specificity?
Specificity is a method of conflict resolution within the cascade.
Static
Fixed
Relative
Absolute
When elements are positioned outside the normal flow, they can overlap other elements.
Z-index to rescue
z-index property specifies the stack order of an element.
@color: #4D926F;
#header {
color: @color;
}
rounded-corners (@radius: 5px) {
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
-ms-border-radius: @radius;
-o-border-radius: @radius;
border-radius: @radius;
}
#header {
.rounded-corners;
}
#footer {
.rounded-corners(10px);
}
@arguments
variablebox-shadow (@x: 0, @y: 0, @blur: 1px, @color: #000) {
box-shadow: @arguments;
-moz-box-shadow: @arguments;
-webkit-box-shadow: @arguments;
}
.box-shadow(2px, 5px);
#header { h1 { font-size: 26px; font-weight: bold; } p { font-size: 12px; a { text-decoration: none; &:hover { border-width: 1px } } } }
/* Compiled CSS */ #header h1 {font-size: 26px;font-weight: bold;} #header p { font-size: 12px;} #header p a {text-decoration: none;} #header p a:hover { border-width: 1px;}
.child, .sibling {
.parent & {
color: black;
}
& + & {
color: red;
}
}
Will output
.parent .child,
.parent .sibling {
color: black;
}
.child + .child,
.child + .sibling,
.sibling + .child,
.sibling + .sibling {
color: red;
}
@the-border: 1px; @base-color: #111; @red: #842210; #header { color: (@base-color * 3); border-left: @the-border; border-right: (@the-border * 2); } #footer { color: (@base-color + #003300); border-color: desaturate(@red, 10%); }
@base: #f04615; @width: 0.5; .class { width: percentage(0.5); // returns `50%` color: saturate(@base, 5%); background-color: spin(lighten(@base, 25%), 8); }
Sometimes, you may want to change the behaviour of a mixin, based on the parameters you pass to it.
.mixin (dark, @color) {
color: darken(@color, 10%);
}
.mixin (light, @color) {
color: lighten(@color, 10%);
}
.mixin (@_, @color) {
display: block;
}
Now, if we run:
@switch: light;
.class {
.mixin(@switch, #888);
}
We will get the following CSS:
.class {
color: #a2a2a2;
display: block;
}
Scope in LESS is very similar to that of programming languages. Variables and mixins are first looked up locally, and if they aren’t found, the compiler will look in the parent scope, and so on.
@var: red;
#page {
@var: white;
#header {
color: @var; // white
}
}
#footer {
color: @var; // red
}
Variables can be embeded inside strings in a similar way to ruby or PHP, with the @{name}
construct:
@base-url: "http://assets.fnord.com";
background-image: url("@{base-url}/images/bg.png");
Sometimes you might need to output a CSS value which is either not valid CSS syntax, or uses proprietary syntax which LESS doesn’t recognize.
To output such value, we place it inside a string prefixed with ~
, for example:
.class {
filter: ~"ms:alwaysHasItsOwnSyntax.For.Stuff()";
}
Questions & Answers!