Sass tutorial
in Tutorial with 15 comments
Sass tutorial
in Tutorial with 15 comments

Previous《 Webpack2 and Sass Setup Tutorial 》The installation of Sass and the setting of webpack have been explained, so this article is about how to use Sass, but it does not involve the advanced part. It is a relatively basic tutorial and belongs to the beginner

Variables

Sass supports 7 data types, as follows

  1. Value (e.g. 1.2, 13, 10px)
  2. String (e.g. "foo", 'bar', baz)
  3. Color (e.g. blue, # 04a3f9, rgba (255, 0, 0, 0.5))
  4. Boolean value (e.g. true, false)
  5. Empty (e.g. null)
  6. Array (e.g. 1.5em 1em 0 2em, Helvetica, Arial, sans serif)
  7. Mapping (e.g. (key1: value1, key2: value2))

For details, see: Here

Variables need to be $ To identify, see the following example:

scss:

 $title-font: normal 24px/1.5 'Open Sans', sans-serif; $cool-red: #F44336; $box-shadow-bottom-only: 0 2px 1px 0 rgba(0, 0, 0, 0.2); h1.title { font: $title-font; color: $cool-red; } div.container { color: $cool-red; background: #fff; width: 100%; box-shadow: $box-shadow-bottom-only; }

css:

 h1.title { font: normal 24px/1.5 "Open Sans", sans-serif; color: #F44336; } div.container { color: #F44336; background: #fff; width: 100%; box-shadow: 0 2px 1px 0 rgba(0, 0, 0, 0.2); }

Nesting

This part is used most frequently. See the following example

scss:

 ul{ margin:0; li{ list-style:none; } }

css:

 ul{ margin:0; } ul li{ list-style:none; }

The parent selector is used when nesting. The key here is &

scss:

 a{ font-size:14px; &:hover{ font-size:18px; } & &-biger{ font-size:22px; } }

css:

 a{ font-size:14px; } a:hover{ font-size:18px; } a a-biger{ font-size:22px; }

Of course, attributes can also be nested, as follows

scss:

 body{ font:{ family: sans-serif; size:15px; weight:normal; } } .nav{ border:1px solid #eee { left:0; right:0; } }

css:

 body{ font-family:sans-serif; font-size:15px; font-weight:normal; } .nav{ border:1px solid #eee; border-left:0; border-right:0; }

Mixins

Mixed use has two keywords: @mixin and @include , examples are as follows

scss:

 @mixin alert{ color:#eee; background-color:#111; a{ color:red; } } .alert-warning{ @include alert; }

css:

 .alert-warning{ color:#eee; background-color:#111; } .alert-warning a{ color:red; }

If parameters are required, they are as follows

scss:

 @mixin alert($text-color,$bg-color){ color:$text-color; background-color:$bg-color; a{ color:darken($text-color,10%); } } .alert-warning{ @include alert(#8a6d3b,#eee); }

css:

 .alert-warning{ color:#eee; background-color:#111; } .alert-warning a{ color:#66512c; }

Extend

Keyword is required for expansion @extend , its working mode is similar to that of mixing, and attention should be paid to the distinction. The following is an example

scss:

 .dialog-button { box-sizing: border-box; color: #ffffff; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.12); padding: 12px 40px; cursor: pointer; } .confirm { @extend .dialog-button; background-color: #87bae1; float: left; } .cancel { @extend .dialog-button; background-color: #e4749e; float: right; }

css:

 .dialog-button, .confirm, .cancel { box-sizing: border-box; color: #ffffff; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.12); padding: 12px 40px; cursor: pointer; } .confirm { background-color: #87bae1; float: left; } .cancel { background-color: #e4749e; float: right; }

Operations

Sass can also do some basic mathematical calculations. The following is an example

scss:

 $width: 800px; .container { width: $width; } .column-half { width: $width / 2; } .column-fifth { width: $width / 5; }

css:

 .container { width: 800px; } .column-half { width: 400px; } .column-fifth { width: 160px; }

For more operations related information, see: here

Functions

Sass has a large number of functions for us to call. The following are darken($color, $amount) Use examples of

scss:

 $awesome-blue: #2196F3; a { padding: 10px 15px; background-color: $awesome-blue; } a:hover { background-color: darken($awesome-blue,10%); }

css:

 a { padding: 10px 15px; background-color: #2196F3; } a:hover { background-color: #0c7cd5; }

For more functions, see: Here

Other

@import The usage of is as follows

base.scss:

 body{ padding:0; margin:0; }

scss:

 @import "base";

css:

 body{ padding:0; margin:0; }

The usage of notes is as follows

 /* *Multiline comment */

Single-Line Comments

 //Single line comment

Mandatory comment

 /*!  Multiline comment*/

Of course, there are more, such as control flow and expression( Here )

reference resources

Lots of content, so far~

Responses
  1. Touch

    Boss, I want to know how to set the thumbnail of the article on the home page as my own. I don't want to upload an attachment when the image of the attachment becomes my thumbnail.

    Reply
  2. Great theme! I have created a lot of bugs, and I'm going to use this.

    Reply
    1. @Liu Mingye's Blog

      You haven't completely followed https://www.linpx.com/p/add-a-separate-search-page-to-the-pinghsu-theme.html Do what is written here, for example, the search menu has not been hidden, and the template page has not added custom fields~

      Reply
  3. Professional, learned from the boss

    Reply
  4. 😔 good

    Reply
    1. @devmsg

      Boss, good~

      Reply
  5. Hey, why is the header of your theme article closed? I have done everything. I will update the theme when you update it. Ha ha ha ha ha ha ha ha ha ha ha ha

    Reply
    1. @Temporary workers

      The background settings can be disabled once, mainly to enhance the article reading experience~

      Reply
      1. @Chakhsu

        By the way, your topic comment reply does not appear under the comment, but above it. Do you have any way to fix this?

        Reply
        1. @Temporary workers

          I did it on purpose~

          Reply
          1. @Chakhsu Lau

            Good or bad, I prefer to appear under the current comment.. Don't know how to modify it?? Or do you want to switch? Hahahaha, I'm sorry

            Reply
            1. @Temporary workers

              The switch is ready to burst. The appearance settings need to be sorted out. I'll see if it can be adjusted

              Reply
              1. @Chakhsu

                Ok, I have to go up to the top every time I reply, and I forget which one I just met. By the way, there is a return. It's just a matter of code. I don't know whether you add it or not.

                Reply
                1. @Temporary workers

                  You mean comment sorting? Set comments that can be adjusted. I got a return on my last topic

                  Reply
                  1. @Chakhsu

                    I prefer the comment box to appear under the current comment..

                    Reply