Sass common usage notes and sass usage notes

Source: Internet
Author: User

Sass common usage notes and sass usage notes

Sass is required for h5 projects recently developed by the company. Therefore, the leaders recommended me to go to the SASS Usage Guide blog of the great god of Ruan Yifeng to learn how to use sass for future convenience, so we should record it here.

I. code reuse

1. Inheritance: SASS allows one selector and inherits another selector.

To Inherit class1, use the @ extend command:

.class1 {    border: 1px solid #ddd;}
.class2 {
    @extend .class1;
    font-size:120%;
}

2. Mixin: Mixin is a bit like macro in C language. It is a reusable code block.

Use the @ mixin command to define a code block. Use the @ include command to call this mixin.

  @mixin left {    float: left;    margin-left: 10px;  }
  div {
    @include left;
  }

The power of mixin is that you can specify parameters and default values. Add parameters as needed:

   @mixin left($value: 10px) {    float: left;    margin-right: $value;   }
  div {
    @include left(20px);
  }

3. Color Functions

SASS provides some built-in color functions to generate a series of colors.

   lighten(#cc3, 10%) // #d6d65c  darken(#cc3, 10%) // #a3a329  grayscale(#cc3) // #808080  complement(#cc3) // #33c

4. Insert a file

@ Import command, used to insert external files

@import "path/filename.scss";

Ii. Advanced usage

1. Condition Statement:

@ If can be used to determine

   p {    @if 1 + 1 == 2 { border: 1px solid; }    @if 5 < 3 { border: 2px dotted; }  }

The @ else command is also supported:

    @if lightness($color) > 30% {    background-color: #000;  } @else {    background-color: #fff;  }

2. Loop statements

For Loop:

 @for $i from 1 to 10 {    .border-#{$i} {      border: #{$i}px solid blue;    }  }

While loop:

 $i: 6;  @while $i > 0 {    .item-#{$i} { width: 2em * $i; }    $i: $i - 2;  }

The each command is similar to:

 @each $member in a, b, c, d {    .#{$member} {      background-image: url("/image/#{$member}.jpg");    }  }

3. User-Defined Functions

SASS allows users to write their own functions.

 @function double($n) {    @return $n * 2;  }  #sidebar {    width: double(5px);  }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.