Home Web Front-end HTML Tutorial css extension technology: the difference between Less and Sass_html/css_WEB-ITnose

css extension technology: the difference between Less and Sass_html/css_WEB-ITnose

Jun 24, 2016 am 11:40 AM

This week I studied the two CSS frameworks of Less and Sass. I basically understood their respective syntax and characteristics, and through two HTML web design exercises, I felt what they bring to us developers. Convenience brought by css web page layout process. Below is some summary of my differences between them.

The similarities between Less and Sass:

1. Variables: You can define a series of common styles separately, when needed call.

2. Mixins: class in class (introducing one class into another class to realize inheritance between classes), and can also be mixed with parameters, Just like functions.

3. Nesting: Nest classes within classes, thereby reducing code duplication.

4. Operations: provides four arithmetic operations: addition, subtraction, multiplication and division, which can perform operations on attribute values ​​and colors.

The difference between Less and Sass:

1. Implementation method: Less is based on JavaScript and is It is processed on the client side; Sass is based on Ruby and is processed on the server side.

2. Define variables: Less uses the prefix: @ when defining variables; Sass uses the prefix: $ when defining variables.

//Less定义变量:@color: #4D926F;#header {    color: @color;}//Sass定义变量    $blue : #1875e7;           div {       color : $blue;      }
Copy after login

3. Mixins: When using mixins in Less, just name them in classB according to classA Yes; in Sass, you first need to use the @mixin command when defining a mix, and secondly, you need to use the @include command when calling to introduce the previously defined mix.

//Less中的混合:.rounded-corners (@radius: 5px) {    -webkit-border-radius: @radius;    -moz-border-radius: @radius;    -ms-border-radius: @radius;    -o-border-radius: @radius;    border-radius: @radius;}#header {    .rounded-corners;}#footer {    .rounded-corners(10px);}//Sass中的混合: @mixin left($value: 10px) {        float: left;        margin-right: $value;      }    div {        @include left(20px);      }
Copy after login

4. Parsing method: Less can be parsed upward/downward; Sass can only be parsed upward.

5. Scope of variables: Variables in Less are divided into global and local; Sass variables can be understood as being global, but you can follow the variable by! default, change the attribute value of the variable before importing it into the Sass file to solve this problem.

//Less:@width:100px;h1{  @width:200px;  width:@width;}h2{  width:@width;}编译后:h1 {  width: 200px;}h2 {  width: 100px;}//Sass:$borderColor:red !default;.border{  border:1px solid $borderColor;}编译后:.border {  border: 1px solid red; }
Copy after login

6. Compared with Less, Sass has added conditional statements (if, if...else) and loop statements (for Loops, while loops and each loops) and custom functions:

//if条件句:    p {        @if 1 + 1 == 2 { border: 1px solid; }        @if 5 < 3 { border: 2px dotted; }      }//if...else条件句:    @if lightness($color) > 30% {        background-color: #000;      } @else {        background-color: #fff;      }//循环语句://for循环:    @for $i from 1 to 10 {        .border-#{$i} {          border: #{$i}px solid blue;        }      }//while循环:     $i: 6;      @while $i > 0 {        .item-#{$i} { width: 2em * $i; }        $i: $i - 2;      }//each循环,类似于for循环:    @each $member in a, b, c, d {        .#{$member} {          background-image: url("/image/#{$member}.jpg");        }      }//自定义函数:    @function double($n) {        @return $n * 2;      }          #sidebar {        width: double(5px);      }
Copy after login

The above is my understanding of the difference between Less and Sass Summarize. They are all cool tools for developers, and they can also help developers work more efficiently and quickly. As for which one you choose to use, you can flexibly use one or two of them according to your own preferences or the requirements of the company you work for.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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 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

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 an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

See all articles