Detailed explanation of CSS preprocessor Less
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;}
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;}
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;}
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; } }}
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;}
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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

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.

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

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.

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

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