CSS预编译:less入门

LESS预编译是向下兼容CSS并且为当前CSS扩展功能。

LESS官网就是最好的学习资源。

这篇文章是对LESS的一个Overview,仅作为初步了解。因为官网是英文,所以我也尽量顺应它的表达避免歧义。

Variables

use variables for more comprihensible code:

@nice-blue: #5B83AD;
@light-blue: @nice-blue + #111;

#header {
  color: @light-blue;
}

and the Output is:

#header {
  color: #6c94be;
}

Mixins

mixins are a way of including a bunch of properties you can use from one rule-set to anthor .See the fllowing css:

.bordered {
  border-top: dotted 1px black;
  border-bottom: solid 2px black;
}

if we want use the properties in other rule-set, just drop in the name of css where we want use it,like so:

#menu a {
  color: #111;
  .bordered;
}

.post a {
  color: red;
  .bordered;
}

now the properties will apear in #menu an .post. It‘s convenient.

Nesting

we also have the ablities to use nesting instead of for more concise code:

#header {
  color: black;
  .navigation {
    font-size: 12px;
  }
  .logo {
    width: 300px;
  }
}

it‘s mimics the structrue of your HTML.

we can also bundle pseudo-sectors with mixins using this method.For example,the classical clearfix hack.

.clearfix {
  display: block;
  zoom: 1;

  &:after {
    content: " ";
    display: block;
    font-size: 0;
    height: 0;
    clear: both;
    visibility: hidden;
  }
}

Nested At-Rules and Bubbling

at-rules such as @media or @support can be also nested .

the at-rule in the top is relative order against other elements inside the same ruleset remains unchanged.

.component {
  width: 300px;
  @media (min-width: 768px) {
    width: 600px;
    @media  (min-resolution: 192dpi) {
      background-image: url(/img/retina2x.png);
    }
  }
  @media (min-width: 1280px) {
    width: 800px;
  }
}

Operations

arthmatical operations can be operated in any number.

// numbers are converted into the same units
@conversion-1: 5cm + 10mm; // result is 6cm
@conversion-2: 2 - 3cm - 5mm; // result is -1.5cm

// conversion is impossible
@incompatible-units: 2 + 5px - 3cm; // result is 4px

// example with variables
@base: 5%;
@filler: @base * 2; // result is 10%
@other: @base + @filler; // result is 15%

but notice problems like these:

@base: 2cm * 3mm; // result is 6cm

// do arithemtic on colors:

@color: #224488 / 2; //results in #112244
background-color: #112244 + #111; // result is #223355

calc() exception

for CSS compatibility ,calc() can not evaluate math expression, but will evaluate variables and math in nested function.like so:

@var: 50vh/2;
width: calc(50% + (@var - 20px));  // result is calc(50% + (25vh - 20px))

Escaping

this allows you to use arbitratry strings as property or variable value with no change.Anything in ~"anything" or ~‘anthing‘ can work.

@min768: ~"(min-width: 768px)";
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}

Functions

Less provide a variety of function to handle with transform colors or manipulate strings or do maths.if you want more,see Function Reference.

Namespaces and Accessors

anytime you want group your mixins you can do this intuitively in LESS.

#bundle() {
  .button {
    display: block;
    border: 1px solid black;
    background-color: grey;
    &:hover {
      background-color: white
    }
  }
  .tab { ... }
  .citation { ... }
}

now if you use them in other place ,do like so:

#header a {
  color: orange;
  #bundle > .button;  // can also be written as #bundle.button
}

Scope

scope in LESS is as same as other programming lanuage like JavaScript.variables and mixins are fitst looked for locally,and if not found ,the compiler will look in parent scope,and so on.

Comments

Just like CSS.

Importing

how it can be?

原文地址:https://www.cnblogs.com/FunkyEric/p/9063556.html

时间: 2024-08-30 14:58:08

CSS预编译:less入门的相关文章

sublime中如何用less实现css预编译

实现css预编译的方式有很多,听说glup很流行而且功能也很强大,但是就目前的工作而言,仅要css预编译和YUIcompress就够了,接下来切入正题 Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. Less 可以运行在 Node 或浏览器端. 1.安装less插件 ctrl+shift+p ->  install package  -> less  即可安装 功能:LESS高亮插件 简介:用LESS的同学都知道,

css预处理器sass入门学习

SASS 叫做css预处理器,他的基本思想是用一门专门的编程语言来进行页面样式的设计,然后在编译成正常的css文件. Sass的用法 安装 sass是用ruby语言写的,所以我们在安装sass之前要先安装ruby(ruby安装自行百度),执行下面命令安装: gem install sass 使用 sass就是普通的文本文件,后缀名为.scss.然后让我们用sass的语法写好文件之后,需要编译成.css文件才能供html文件使用(即通过link标签引入html文件),那么我们就可以使用命令行或是g

CSS 预编译stylus

stylus是css的预编译语言,用于去掉css中的冒号和分好,例如 .style{ position fixed width 100% z-index 10 } vue的使用方式是需要安装 npm install stylus stylus-loader --save-dev 原文地址:https://www.cnblogs.com/wjw-/p/11548874.html

浅谈css的预编译---less语言

正如各位所知道的一样,css是一门标记性语言,语法相对简单,对使用者的要求也比较低 .不过可乐不知道友友们有没有发现,在使用css的时候需要书写大量看似没有逻辑的代码,不方便维护及扩展,不利于复用,尤其对于一些缺乏css编写经验的猿猿来讲,写出组织良好且易于维护的 CSS 代码是相当困难的一件事情.这个时候呢,可乐悄悄地告诉你们,咱们的程序员蜀黍是无所不能的,针对这个调皮的css,专门开发了less语言.那么,就由可乐来细细为你们介绍这一个新朋友吧~~~ 一.less简介: 1.less语言是在

Rails : css或js文件无法成功预编译或调用jquery类插件时预编译问题

调用bootstrap css框架时,将bootstrap文件夹放入 vendor/assets/下 bootstrap文件结构如下:    [[email protected] demo]$ ls vendor/assets/bootstrap/     css  img  js [[email protected] demo]$ ls vendor/assets/bootstrap/css/     bootstrap.css  bootstrap.min.css  bootstrap-re

一张图掌握移动Web前端所有技术(大前端、工程化、预编译、自动化)

你要的移动web前端都在这里! 大前端方向:移动Web前端.Native客户端.Node.js. 大前端框架:React.Vue.js.Koa 跨终端技术:HTML 5.CSS 3.JavaScript 跨平台框架:React Native.Cordova 前端工程化:Grunt.Gulp.Webpack 前端预编译:Babel.Sass.Less 自动化测试:Jasmine.Mocha.Karma 一图在手,应有尽有! 更多信息参考:https://item.jd.com/12170351.h

关于前端CSS预处理器Sass的小知识!

前面的话 ??"CSS预处理器"(css preprocessor)的基本思想是,用一种专门的编程语言,进行网页样式设计,然后再编译成正常的CSS文件.SASS是一种CSS的开发工具,提 供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护.本文将详细介绍sass的使用 定义 ??Sass是一门高于CSS的元语言,它能用来清晰地.结构化地描述文件样式,有着比普通CSS更加强大的功能.Sass能够提供更简洁.更优雅的语法,同时提供多种功能来创建可维护和管理的样式

CSS预处理器Sass(Scss)、Less、Stylus

CSS 预处理编译器能让我成程序化其的方式编写CSS代码,可以引入CSS中没有的变量.条件.函数等特性,从而让代码更简单易维护,但一般按预处理器语法编写的代码无法直接在浏览器中运行,需用通过工具比如gulp转换成标准的CSS语法,从而在浏览器中运行.个人理解它是标准CSS语法的扩展及增强,能在一定程度上提高编码效率. 三种预处理器的对比: Sass诞生是最早也是最成熟的CSS预处理器,有Ruby社区和Compass支持:安装.编译依赖Ruby环境: Stylus早期服务器Node JS项目,在该

【前端开发】优化代码之减少引入,css预编译语言的优点,stylus的使用

前言:我必须得承认在最最最开始的时候,我对于css的预编译是非常不以为然的,这是错误的.一般在页面编写过程中,我会将需要reset的css放在reset.css中,讲会需要重复用到的放置到public.css中,对于常用的几个数据,例如字体font-10,font-12,font-24,font-36,对于margin来说margin-top-10,margin-bottom-10等等,这样修改起来也非常方便,不过在看了(瞬间忘记了书名)以后,减少引入能够优化页面性能. 我们都知道,浏览器的工作