Home Web Front-end HTML Tutorial Detailed explanation of CSS preprocessor Less

Detailed explanation of CSS preprocessor Less

Feb 28, 2018 am 10:13 AM
less sass

CSS preprocessor

Why there is a CSS preprocessor

CSS is basically a tool for designers, not programmers. In the eyes of programmers, CSS is a headache. Unlike other programming languages, such as PHP, Javascript, etc., it has its own variables, constants, conditional statements and some programming syntax. It is just a line of pure attributes. Description is quite time-consuming to write, and the code is difficult to organize and maintain.

Naturally, some people began to wonder whether some programming elements could be added to CSS like other programming languages, so that CSS could do some predetermined processing like other programming languages. In this way, there is a "CSS Preprocessor".

What is CSS preprocessor

It is a superset of the CSS language and is more complete than CSS.

CSS preprocessor defines a new language. Its basic idea is: use a specialized programming language to add some programming features to CSS, use CSS as a target to generate files, and then developers Just use this language for coding work.

In layman’s terms, the CSS preprocessor uses a specialized programming language to design Web page styles, and then compiles them into normal CSS files for project use. The CSS preprocessor adds some programming features to CSS without having to consider browser compatibility issues. For example, you can use variables, simple logic programs, functions, etc. in CSS. Some basic features in programming languages ​​allow you to CSS is more concise, more adaptable, more readable, easier to maintain, and many other benefits.

CSS preprocessor technology has been very mature, and many different CSS preprocessor languages ​​have emerged, such as: Sass (SCSS), LESS, Stylus, Turbine, Switch CSS, CSS Cacheer, DT CSS etc. There are so many CSS preprocessors, so "Which CSS preprocessor should I choose?" has become a hot topic on the Internet recently, on Linkedin, Twitter, CSS-Trick, Zhihu and major technical forums , many people are arguing about this. This is a big step forward from where we once were on the subject of whether we should use CSS preprocessors.

So far, among the many excellent CSS preprocessor languages, Sass, LESS and Stylus are the best, with many discussions and comparisons. This article will introduce you to these three CSS preprocessor languages ​​from their background, installation, usage syntax, differences and other comparisons. I believe that front-end development engineers will make their own choice-which CSS preprocessor should I choose?

Introduction to less, less is a popular preprocessing CSS that supports variables, mixes, functions, nesting, loops and other features.

less syntax

Comments

less can have two types of comments.

The first type of comment: template comment

// The comments here in the template comment will be deleted after they are converted into CSS

because less needs to be converted into css before it can be used used in the browser. After converting to css, this kind of comment will be deleted (after all, css does not recognize this kind of comment).

Second type of comment: CSS comment syntax

/* CSS comment syntax is converted to CSS and then retained*/

Summary: If written in less Comments, we recommend writing the first type of comments. Unless it is something similar to copyright, the second type of annotation is used.

Define variables

We can define reused or frequently modified values ​​as variables, and just reference this variable where it needs to be used. This avoids a lot of duplication of work.

(1) In the less file, define the format of a variable:

@Variable name: variable value; //Format @bgColor: #f5f5f5; //Format example

(2) At the same time, reference this variable in the less file.

Finally, the complete code of the less file is as follows:

main.less:
// 定义变量@bgColor: #f5f5f5;// 引用变量body{    background-color: @bgColor;}
Copy after login

After we compile the above less file into a css file (the next section talks about the compilation of the less file), the automatically generated code is as follows:

main.css:
body{    background-color: #f5f5f5;}
Copy after login

Using nesting

Children selectors are often used in css. The effect may be like this:

.container {
  width: 1024px;}.container > .row {
  height: 100%;}.container > .row a {
  color: #f40;}.container > .row a:hover {
  color: #f50;}
Copy after login

The above code is nested in many layers. It’s tedious to write. But if you use less's nested syntax to write this code, it will be more concise.

Examples of nesting are as follows:

main.less:
.container {  width: @containerWidth;  > .row {    height: 100%;    a {      color: #f40;      &:hover {        color: #f50;      }    }  }  div {    width: 100px;    .hello {      background-color: #00f;    }  }}
Copy after login

After compiling the above less file into a css file, the automatically generated code is as follows:

main.css
.container {    width: 1024px;}.container > .row {    height: 100%;}.container > .row a {    color: #f40;}.container > .row a:hover {    color: #f50;}.container div {    width: 100px;}.container div .hello {    background-color: #00f;}
Copy after login

I believe you have read these cases After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!

Related Reading:

The 20 most commonly used regular expressions in JavaScript

Common settings for vscode

How to convert decimal numbers to hexadecimal

How to implement a custom mouse right-click menu in JS

The above is the detailed content of Detailed explanation of CSS preprocessor Less. 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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

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.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

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.

See all articles