Home Backend Development C#.Net Tutorial What is Less? koala configuration and usage

What is Less? koala configuration and usage

Jun 24, 2017 am 10:49 AM
css less sass study notes

 一、什么是Less

    css的Less好比是js的Jquery,可以让人们更方遍快捷的使用css,使css代码更简洁,可以减少重复的代码,减少开发人员的工作量。

  Less CSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法,为CSS赋予了动态语言的特性,如变量、继承、运算、函数等,更方便CSS的编写和维护。

   Less中文手册:less.bootcss.com

 

二、编译工具

  1.Koala:国人开发的LESS/SASS编译工具

   下载地址:

  2.Node.js库

  3.浏览器端使用

 

三、koala配置及使用

  1.新建后缀为.less文件:index.less

   头部写上:@charset "utf-8";   //设定字符集

  2.把文件夹拖到koala中,设置输出路径为style下的index.css

     使用koala进行编译,然后就生成了一个index.css文件。

  3.之后我们只要编辑index.less文件即可。

 

四、注释

  1./*编译后会被保留*/

  2.//编译后不会被保留

 

五、变量

  1.设定变量:

  @text_width: 300px;

  2.使用变量:示例如下

  

  .box{
    width: @text_width;
    height: @text_width;
    background: #000;
  }

 

六、混合

  1.混合简介:与原有的在class中新追加class的方法有所不同(原有

),使用Less的混合功能后,只要声明.border后,在.box中加  入.border;即可。

  使用示例一:

  

  .box{
    width: @text_width;
    height: @text_width;
    background: #000;

    .border;
  }

  .border{

    border: 5px solid yellow;

  }

  使用示例二:新增一个box2,但是它和box1只有一点点细微的区别,可以直接拿来box1使用,然后加上它自己的样式。

  .box2{

    .box1;

    margin-left: 100px;

  }

  2.混合是可以带参数的

  在index.less中使用带参数的值,在border_02后面加一个(),里面声明一个变量;

  然后传入参数30px。

  

  在编译后的index.css文件中,带参数的变量不显示

  

  3.混合也可以带默认值

  在border_03后面加一个(),里面声明一个变量,变量后面附一个初始值“:10px”;

  如果不想用默认值,只要在text_hunhe下的border_03的()中写入值即可。

  默认值必须带(),不然会报错。

  

  4.混合高级使用示例,兼容性也可以使用

  

  

  在编译后的index.css文件中,展示如下

  

 

七、匹配模式

  1.简介:相当于js中的if,但不完全是

  2.示例

  先介绍一个画倒三角的方法

  

  .sanjiao{
    width: 0;
    height: 0;
    overflow: hidden;
    border-width: 10px;
    border-color: red transparent transparent transparent;
    border-style: solid dashed dashed dashed;
  }

  匹配模式示例一:

  .triangle(top,@w:5px,@c:#ccc){

    border-width: @w;

    border-color: @c transparent transparent transparent;

    border-style: solid dashed dashed dashed;

  }

  .triangle(right,@w:5px,@c:#ccc){

    border-width: @w;

    border-color:  transparent @c transparent transparent;

    border-style:  dashed solid dashed dashed;

  }

  .triangle(bottom,@w:5px,@c:#ccc){

    border-width: @w;

    border-color:  transparent transparent @c transparent;

    border-style:  dashed dashed solid dashed;

  }

  .triangle(left,@w:5px,@c:#ccc){

    border-width: @w;

    border-color:  transparent transparent transparent @c;

    border-style:  dashed dashed dashed solid;

  }

  .triangle(@_,@w:5px,@c:#ccc){    // @_ 是固定格式,表示不管你匹配到谁,都会带上这些内容

    width: 0;
    height: 0;
    overflow: hidden;

  }

  .sanjiao{

    .triangle(right,100px);

  }

  匹配模式示例二:

  匹配模式定位

  .pos(r){position:relative;}
  .pos(a){position:absolute;}
  .pos(f){position:fixed;}
  .posi{
    width: 100px;
    height: 100px;
    background: blue;
    .pos(r);
  }

 

八、运算

  1.less中的运算可以是任何数字、颜色、变量,运算要包裹在括号中。如:+ - * /

  2.示例如下:

  @test_01:300px;

  .box_02{

    width: (@test_01 - 20) * 5;   //未强制规定必须要带px单位,只要有一个带即可

    color: #ccc - 10;                      //不太常用到

  }

 

九、嵌套规则

  1.示例

   

  原css 

  .list{}
  .list li{}
  .list a{}
  .list span{}

  使用Less嵌套:

  .list{
    width: 600px;
    margin: 30px auto;
    padding: 0;
    list-style: none;
    font-size: 16px;    li{

      height: 30px;
      line-height: 30px;
      background: blue;
      margin-bottom: 5px;
      padding: 0 10px;
    }
    a{
      float: left;
      color: #000;

      &:hover{    //&代表上一层选择器

        color: red;

      }
    }
    span{
      float: right;
    }
  }

 

十、arguments变量

  .border_arg(@w:30px,@c:#ccc,@xx:solid){border:@arguments}     //省了一点点事儿,懒人必备

  .test_arguments{.border_arg(40px);}

 

十一、避免编译、!important

  1.避免编译:在我们需要输入一些不正确的css语法或者使用一些less不认识的专有语法时使用。在字符串前加上一个~即可

  .test_03{
    width: ~'calc(300px - 30px)';    //~'...'避免编译,把单引号里的内容按照原样传给浏览器
  }

  2.!important关键字:适合用来调试,一般不会用到

  .test_important{

    .box !important;    //给所有样式加上!important

  }

 

The above is the detailed content of What is Less? koala configuration and usage. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles