search
HomeWeb Front-endHTML Tutorialsass advanced syntax

github address: https://github.com/lily1010/sass/tree/master/course03

The sass syntax used is:

sass --watch test.scss:test.css --style expanded

As shown below:

1 Import external files, the default file suffix is ​​sass/scss file by default, usually declared in the header

test.scss content is:

<span style="color: #000000;">@import "lili.scss";  //导入一个文件
@import "lili.scss", "haha.scss"; //导入两个文件 
/*但在以下情况下, 仅作为普通的 CSS @import 规则语句,不会导入任何 Sass 文件。
*(1) 如果文件的扩展名是 .css。
*(2) 如果文件名以 http:// 开始。
*(3) 如果文件名是 url()
*(4)如果@import 中包含任何的媒体查询(media queries)
*/
@import "lili.css";
@import "http://foo.com/bar";
@import url(lili);
@import "lili" screen;

/*在import里面插入动态变量,但是仅适用于url方式*/
$name:family;
@import url("http://fonts.googleapis.com/css?family=#{$name}");

/*导入scss文件,却不需要将它编译为css文件做法:
 *(1)新建一个文件夹,为了将不需要编译的文件和需要编译的文件分开,这点千万注意
 *(2)在已经建好的文件夹里面,将不要编译的*.scss文件命名为_*.scss
 *(3)导入的时候不要用下划线,直接@import("新建文件夹名字/*.scss")
 */</span>

The content of lili.scss is:

<span style="color: #000000;">.test1 {
    color: black;
}</span>

The content of haha.scss is:

<span style="color: #000000;">.test11 {
    color: deeppink;
}</span>

The content compiled into test.css is:

<span style="color: #000000;">@import url(lili.css);
@import "http://foo.com/bar";
@import url(lili);
@import "lili" screen;
@import url("http://fonts.googleapis.com/css?family=family");
.test1 {
  color: black;
}

.test1 {
  color: black;
}

.test11 {
  color: deeppink;
}</span>

2 The extend function not only inherits the style of the class name selector, but also inherits the styles related to it, including inheriting its parent selector

test.scss content is:

<span style="color: #000000;">.test2 {
  border: 1px #f00;
  background-color: #fdd;
}
.test2.test21 {
  background-image: url("/image/hacked.png");
}
.test2 .test22 {
  background-image: url("/image/haha.png");
}
html .test2 {
  width: 100px;
}
.lili {
  @extend .test2;
  border-width: 3px;
} </span>

The content compiled into test.css is:

.test2, .lili {
  border: 1px #f00;
  background-color: #fdd;
}

.test2.test21, .test21.lili {
  background-image: url("/image/hacked.png");
}

.test2 .test22, .lili .test22 {
  background-image: url("/image/haha.png");
}

html .test2, html .lili {
  width: 100px;
}

.lili {
  border-width: 3px;
}

3 extend function, inherits the single element selector style, and inherits the styles related to it, including inheriting its parent selector

test.scss content is:

<span style="color: #000000;">a:hover {
    color: green;
}
a.class1:hover {
    height: 10px;
}
html a:hover {
    width: 10px;
}
.test3 {
    @extend a:hover;
    width: 20px;
}</span>

The content compiled into test.css is:

<span style="color: #000000;">a:hover, .test3 {
  color: green;
}

a.class1:hover, .class1.test3 {
  height: 10px;
}

html a:hover, html .test3 {
  width: 10px;
}

.test3 {
  width: 20px;
}</span>

4 extend medium chain expansion

test.scss content is:

<span style="color: #000000;">.test4 {
    width:20px;
}
.test41 {
    @extend .test4;
    height: 20px;
}
.test42 {
    @extend .test41;
    top:20px;
}</span>

The content compiled into test.css is:

<span style="color: #000000;">.test4, .test41, .test42 {
  width: 20px;
}

.test41, .test42 {
  height: 20px;
}

.test42 {
  top: 20px;
}</span>

5 Placeholder%,% will not be compiled into css

test.scss content is:

<span style="color: #000000;">.test5 a%name {
    width: 100px;
}
.lili {
    height: 200%;
    @extend %name;
}</span>

The content compiled into test.css is:

<span style="color: #000000;">.test5 a.lili {
  width: 100px;
}

.lili {
  height: 200%;
}</span>

6 To prevent errors in inheriting non-existent styles in extend, use !optional to skip empty styles directly

test.scss content is:

<span style="color: #000000;">.test6 {
    @extend noexist!optional;
    height: 100px;
}</span>

The content compiled into test.css is:

<span style="color: #000000;">.test6 {
  height: 100px;
}</span>

7 The @at-root directive causes one or more rules to be output at the root level of the document instead of being nested under its parent selector

test.scss content is:

<span style="color: #000000;">.test7 {
    height: 20px;
    @at-root {
        .children1 {
            color: red;
        }
        .children2 {
            color: black;
        }
    }
}</span>

The content compiled into test.css is:

<span style="color: #000000;">.test7 {
  height: 20px;
}
.children1 {
  color: red;
}

.children2 {
  color: black;
}</span>

8 @at-root(without:class name) moves the selector outside the nested directive

test.scss content is:

<span style="color: #000000;">.gaga {
    @media name {
      .page {
        width: 8px;
        @at-root (without: media) {  //注意此处目前测试是不支持类名的,比如.test8
          color: red;
        }
      }
    }
}</span>

The content compiled into test.css is:

<span style="color: #000000;">@media name {
  .gaga .page {
    width: 8px;
  }
}
.gaga .page {
  color: red;
}</span>

9 If conditional judgment, please note that if...else...

is not supported

test.scss content is:

.test8 {   //<span style="color: #0000ff;">if</span>...<span style="color: #0000ff;">if</span><span style="color: #000000;">..
    @</span><span style="color: #0000ff;">if</span> 1+1 == 2<span style="color: #000000;"> {
        width: 20px;
    }
    @</span><span style="color: #0000ff;">if</span> 5  {
        width: 100px;
    }
}
.test81 {  //<span style="color: #0000ff;">if</span>...<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #000000;">...
    @</span><span style="color: #0000ff;">if</span> 1+1 != 2<span style="color: #000000;"> {
        width: 20px;
    }
    @</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> 5 > 3<span style="color: #000000;"> {
        width: 100px;
    }
}
.test82 {  </span>//<span style="color: #0000ff;">if</span>...<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>...<span style="color: #0000ff;">else</span><span style="color: #000000;">...
    @</span><span style="color: #0000ff;">if</span> 1+1 != 2<span style="color: #000000;"> {
        width: 20px;
    }
    @</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> 5  { 
        width: 100px;
    }
    @<span style="color: #0000ff;">else</span><span style="color: #000000;"> {
        width: 10px;
    }
}</span>

The content compiled into test.css is:

<span style="color: #000000;">.test8 {
  width: 20px;
}

.test81 {
  width: 100px;
}

.test82 {
  width: 10px;
}</span>

10 for loop statement

test.scss content is:

//第一种格式 @<span style="color: #0000ff;">for</span> $var <span style="color: #0000ff;">from</span> <start> through <end>,注意范围包括<start>和<end><span style="color: #000000;">的值
@</span><span style="color: #0000ff;">for</span> $i <span style="color: #0000ff;">from</span> 1 through 3<span style="color: #000000;"> {
    .gray</span><span style="color: #008000;">#</span><span style="color: #008000;">{$i*3} {</span>
        color: <span style="color: #008000;">#</span><span style="color: #008000;">333*$i; </span>
<span style="color: #000000;">    }
}

</span>//第二种格式 @<span style="color: #0000ff;">for</span> $var <span style="color: #0000ff;">from</span> <start> to <end>,注意范围从<start>开始运行,但不包括<end><span style="color: #000000;">的值
@</span><span style="color: #0000ff;">for</span> $i <span style="color: #0000ff;">from</span> 1 to 4<span style="color: #000000;"> {
    .gray2</span><span style="color: #008000;">#</span><span style="color: #008000;">{$i*3} {</span>
        color: <span style="color: #008000;">#</span><span style="color: #008000;">333*$i; </span>
<span style="color: #000000;">    }
}</span></end></start></end></start></end></start></end></start>

The content compiled into test.css is:

<span style="color: #000000;">.gray3 {
  color: </span><span style="color: #008000;">#</span><span style="color: #008000;">333333;</span>
<span style="color: #000000;">}

.gray6 {
  color: </span><span style="color: #008000;">#</span><span style="color: #008000;">666666;</span>
<span style="color: #000000;">}

.gray9 {
  color: </span><span style="color: #008000;">#</span><span style="color: #008000;">999999;</span>
<span style="color: #000000;">}

.gray23 {
  color: </span><span style="color: #008000;">#</span><span style="color: #008000;">333333;</span>
<span style="color: #000000;">}

.gray26 {
  color: </span><span style="color: #008000;">#</span><span style="color: #008000;">666666;</span>
<span style="color: #000000;">}

.gray29 {
  color: </span><span style="color: #008000;">#</span><span style="color: #008000;">999999;</span>
}

11 each loop statement @each $var in

test.scss content is:

$name:<span style="color: #800000;">"</span><span style="color: #800000;">lili</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">yaya</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">sansa</span><span style="color: #800000;">"</span>;  //<span style="color: #000000;">注意数组list的写法
@each $i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> $name {
    test9.</span><span style="color: #008000;">#</span><span style="color: #008000;">{$i} {</span>
<span style="color: #000000;">        width: 10px;
    }
}

$name2:(name21:</span><span style="color: #800000;">"</span><span style="color: #800000;">lili</span><span style="color: #800000;">"</span>,name22:<span style="color: #800000;">"</span><span style="color: #800000;">yaya</span><span style="color: #800000;">"</span>,name23:<span style="color: #800000;">"</span><span style="color: #800000;">sansa</span><span style="color: #800000;">"</span>);  //<span style="color: #000000;">注意对象map的写法
@each $i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> $name2 {
    test9.</span><span style="color: #008000;">#</span><span style="color: #008000;">{$i} {</span>
<span style="color: #000000;">        width: 10px;
    }
}

$name3:(name31:</span>1,name32:2,name33:3);  //<span style="color: #000000;">注意对象map的写法
@each $key,$value </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> $name3 {
    test9.</span><span style="color: #008000;">#</span><span style="color: #008000;">{$key} {</span>
        width: 10px*<span style="color: #000000;">$value;
    }
}</span>

The content compiled into test.css is:

test9.lili {
  width: 10px;
}

test9.yaya {
  width: 10px;
}

test9.sansa {
  width: 10px;
}

test9.name21 lili {
  width: 10px;
}

test9.name22 yaya {
  width: 10px;
}

test9.name23 sansa {
  width: 10px;
}

test9.name31 {
  width: 10px;
}

test9.name32 {
  width: 20px;
}

test9.name33 {
  width: 30px;
}

 

12 while loop statement

test.scss content is:

$i:3;
@while $i > 0 {
    .gray#{$i} {
        color: #333*$i;
    }
    $i:$i - 1; //注意此处不能写成$i:$i-1,因为会被当成字符串
}

The content compiled into test.css is:

.gray3 {
  color: #999999;
}

.gray2 {
  color: #666666;
}

.gray1 {
  color: #333333;
}

 

13 Mix instructions to achieve code block reuse

test.scss content is:

@mixin left01 {  //不带参数
    float: left;
}
.test10 {
    @include left01; 
}

@mixin left02($left) {  //带1个参数
    float: $left;
}
.test101 {
    @include left02(left); 
}

@mixin left03($left,$width) {  //带2个参数,或者说参数为数组
    float: $left;
    .lili {
        width: $width;
    }
}
.test102 {
    @include left03(left,100px); 
}

@mixin left04($name31,$name32,$name33) {  //参数为对象,但是接受传递的参数必须是对象相对应key,同时需要用...传递参数
    .lili {
        width: $name31;
        height: $name32;
        top: $name33;
    }
}
$map:(name31:"1px",name32:"2px",name33:"3px");
.test103 {
    @include left04($map...); 
}

@mixin left05($left:right) {  //带默认参数,不传参的话就用默认参数
    float: $left;
}
.test104 {
    @include left05; 
}

@mixin box-shadow($shadows...) {  //不定参数,用...
      -moz-box-shadow: $shadows;
      -webkit-box-shadow: $shadows;
      box-shadow: $shadows;
}
.shadows {
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}

The content compiled into test.css is:

.test10 {
  float: left;
}

.test101 {
  float: left;
}

.test102 {
  float: left;
}
.test102 .lili {
  width: 100px;
}

.test103 .lili {
  width: "1px";
  height: "2px";
  top: "3px";
}

.test104 {
  float: right;
}

.shadows {
  -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}

 

14 Pass the content block @content to the mixin and pass it to the @content position

test.scss content is:

<span style="color: #000000;">@mixin lala {
    html {
        color: </span><span style="color: #008000;">#</span><span style="color: #008000;">888;</span>
<span style="color: #000000;">        @content;
    }
}
@include lala {  </span>//<span style="color: #000000;">此处名字必须和上面保持一致
    .logo {
        font</span>-<span style="color: #000000;">size: 15px;
    }
}</span>

The content compiled into test.css is:

<span style="color: #000000;">html {
  color: </span><span style="color: #008000;">#</span><span style="color: #008000;">888;</span>
<span style="color: #000000;">}
html .logo {
  font</span>-<span style="color: #000000;">size: 15px;
}</span>

15 Variables are mixed in the scope of @mixin, that is, the content block passed to the mixin is operated in the scope in which it is defined, not the scope of the mixin. This means that local variables from mixins cannot be passed to style blocks for use

test.scss content is:

$color: white;
@mixin haha($color:black) {
    background-color: $color;
    @content;
}
.test12 {
    @include haha{
        color: $color;
    }
}

编译成test.css内容是:

<span style="color: #000000;">.test12 {
  background</span>-<span style="color: #000000;">color: black;
  color: white;
}</span>

 

16 函数,用法类似@mixin

test.scss内容是:

<span style="color: #000000;">@function sasa($name) {
    @</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $name;
}
.test13 {
    font</span>-<span style="color: #000000;">size: sasa(15px);
}</span>

编译成test.css内容是:

<span style="color: #000000;">.test13 {
  font</span>-<span style="color: #000000;">size: 15px;
}</span>
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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

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

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools