Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Start using less css

Start using less css

Less is a “dynamic stylesheet language”

Ali MasudianPour

October 25, 2013
Tweet

More Decks by Ali MasudianPour

Other Decks in Programming

Transcript

  1. What is less css • Dynamic CSS – Less is

    a “dynamic stylesheet language” • LESS extends CSS with dynamic behavior such as variables, mixins, operations and functions. • LESS runs on both the server-side (with Node.js and Rhino) or client-side (modern browsers only) LESS CSS - A BASIC INTRODUCTION 2
  2. Compiling in browser • Less.js will perform AJAX requests to

    grab your Less files, will then process those files to convert them into CSS, and finally inject the resulting CSS in the head of your document. • There is a bad thing about it – if the user’s JavaScript is deactivated, it will not work! LESS CSS - A BASIC INTRODUCTION 3
  3. Let’s start • LESS codes are written into a file

    with .less extension • Variables – Less variables can be declared and used with the @ symbol. • Example: @white: #fff; .class{ background-color:@white; } LESS CSS - A BASIC INTRODUCTION 4
  4. LET’s start • In addition, we can assign a variable

    to another – For instance: • @myDefaultColor:@white; • MIXINS – With Less we can define “mixins”, which are something comparable to functions in other programming languages. In Less they’re used to group CSS instructions. .Round{ -webkit-border-radius:9999px; -moz-border-radius:9999px; border-radius:9999px; } #shape1{ .Round; } That’s really simple! LESS CSS - A BASIC INTRODUCTION 5
  5. Parametric mixins • In LESS we can define parametric mixins.

    So we can send parameters through mixins – Example: @defaultRadius:30px; .RoundedShape(@radius:@defaultRadius){ -webkit-border-radius:@radius; -moz-border-radius:@radius; border-radius:@radius; } – What an incredible feature! LESS CSS - A BASIC INTRODUCTION 6
  6. Operations • One other powerful feature of Less is the

    ability to use mathematical operations in your stylesheets. • You can add, subtract, multiply and divide values – Example @defaultShapesWidth:200px; @borderSize:@defaultShapesWidth * 0.1; – it also works for colors @defaultThemeColor:@lightBlue; @borderColor:@defaultThemeColor - #222; LESS CSS - A BASIC INTRODUCTION 8
  7. Color functions • Less provides the following color functions: –

    darken() and lighten(), which add some black or white – saturate() and desaturate(), to make a color more colorful or more grayscale – Fadein() and fadeout, to increase or reduce transparency – Spin(), which modifies the hue of the color • Examples: @borderColor: desaturate(@defaultColor,100%) @borderColor: darken(desaturate($defaultColort,100%),20%) @defaultColor:spin(@mycolor1,100) LESS CSS - A BASIC INTRODUCTION 9
  8. Nested rules • When designing a complex page using css,

    you often have to chain selectors to aim a particular element in the DOM, like this: #header h1{} #main h1{} – With LESS you can nest rules inside parent rules to mimic the DOM structure: #header { h1{} } .shape{ &:hover{background-color:@myColor;} } – This & symbol represents the current selected elements. It is the equivalent of “THIS” in most programming language LESS CSS - A BASIC INTRODUCTION 10
  9. Importing • It is a very good practice to separate

    your rules into different files instead of having a 1000 lines file. • Importing a file into another LESS is as simple as that: – @import “file2.less”; • You can even omit the .less extension and just declare: – @import “file2”; LESS CSS - A BASIC INTRODUCTION 11
  10. Modify variables • modifyVars enables modification of LESS variables in

    run-time. • When called with new value, the LESS file is recompiled without reloading. • Usage: Less.modifyVars({ ‘@color1’:’red’, ‘@color2’: ‘blue’ }); LESS CSS - A BASIC INTRODUCTION 12