search
HomeBackend DevelopmentC#.Net TutorialWhat is Less? koala configuration and usage

What is Less? koala configuration and usage

Jun 24, 2017 am 10:49 AM
csslesssassstudynotes

 一、什么是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
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
The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

Is C# Always Associated with .NET? Exploring AlternativesIs C# Always Associated with .NET? Exploring AlternativesMay 04, 2025 am 12:06 AM

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

The .NET Ecosystem: C#'s Role and BeyondThe .NET Ecosystem: C#'s Role and BeyondMay 03, 2025 am 12:04 AM

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# as a .NET Language: The Foundation of the EcosystemC# as a .NET Language: The Foundation of the EcosystemMay 02, 2025 am 12:01 AM

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

C# vs. .NET: Clarifying the Key Differences and SimilaritiesC# vs. .NET: Clarifying the Key Differences and SimilaritiesMay 01, 2025 am 12:12 AM

C# is a programming language, while .NET is a software framework. 1.C# is developed by Microsoft and is suitable for multi-platform development. 2..NET provides class libraries and runtime environments, and supports multilingual. The two work together to build modern applications.

Beyond the Hype: Assessing the Current Role of C# .NETBeyond the Hype: Assessing the Current Role of C# .NETApr 30, 2025 am 12:06 AM

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.

The Future of C# .NET: Trends and OpportunitiesThe Future of C# .NET: Trends and OpportunitiesApr 29, 2025 am 12:02 AM

The future trends of C#.NET are mainly focused on three aspects: cloud computing, microservices, AI and machine learning integration, and cross-platform development. 1) Cloud computing and microservices: C#.NET optimizes cloud environment performance through the Azure platform and supports the construction of an efficient microservice architecture. 2) Integration of AI and machine learning: With the help of the ML.NET library, C# developers can embed machine learning models in their applications to promote the development of intelligent applications. 3) Cross-platform development: Through .NETCore and .NET5, C# applications can run on Windows, Linux and macOS, expanding the deployment scope.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools