Home Web Front-end HTML Tutorial First time using Sass

First time using Sass

Aug 25, 2016 am 10:20 AM

Look at the sass tutorial of senior materliu on MOOC.com, http://www.imooc.com/learn/364. By the way, I will restructure the project I just finished and write down some notes and experiences here~

 First install sass, here you can directly refer to Senior Desert’s installation tutorial http://www.w3cplus.com/sassguide/install.html.

  Then install compass, type the command in ruby ​​command, gem install compass. At this stage, I don’t understand much about compass. After watching the sass video, I feel that it is only used to compile scss files and compress css for the time being. (fog).

Compass command:

 compass create compass;

 Compass watch;

 Sass syntax:

When the file does not need to be compiled, it can be marked with _ prefix and underscore and then named. Usually functions or variables are placed in a folder.

 You can use import to import files, and the file name suffix does not need to be written. However, this is not a native CSS import.

 Two major disadvantages of native CSS import: 1. It must be placed at the front of the code. 2. It is detrimental to performance. If you really want to use native import, then: 1. When it ends with css. 2. Start with http://. 3. URL() function. 4. With media queries.

  Sass variables: Good thing. For example, every time you look for a color, you can’t remember the color code. If you use variables, you don’t have to look for the color code so slowly. You can just look at the variable file and it will be clear at a glance.

 Special variables, variables used in specific situations;

  eg:

  

<span style="color: #800000;">//普通变量及其使用
$common-ff :"微软雅黑"; //字体设置
body</span>{<span style="color: #ff0000;">
    font-family</span>:<span style="color: #0000ff;"> $common-ff</span>;
}<span style="color: #800000;">
//css输出----
body</span>{<span style="color: #ff0000;">
    font-family</span>:<span style="color: #0000ff;"> "微软雅黑"</span>;
}<span style="color: #800000;">

//特殊变量
$direction: top;
//应用于class和属性
.border-#</span>{<span style="color: #ff0000;">$direction</span>}{<span style="color: #ff0000;">
  border-#{$direction</span>}<span style="color: #800000;">:1px solid #ccc;
}
//应用于特殊属性同理</span>
Copy after login

  Multi-valued variable: As the name suggests, it means multiple values. For example, 0 1px 2px 3px and so on. There are many functions in it, and I have only used append($list,$value,[$separator]) for the time being.

Mixin: declared through @mixin, called by @include;

When I was working on a project, I used Flexible to write a lot of styles like this

<span style="color: #800000;">button,input,textarea</span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 12px</span>;
}<span style="color: #800000;">
[data-dpr="2"] button,
[data-dpr="2"] input,
[data-dpr="2"] textarea</span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 24px</span>;
}<span style="color: #800000;">
[data-dpr="3"] button,
[data-dpr="3"] input,
[data-dpr="3"] textarea</span>{<span style="color: #ff0000;">
    font-size</span>:<span style="color: #0000ff;"> 36px</span>;
}
Copy after login

It’s too troublesome to write like this, so after learning sass, I wrote one myself by referring to the hybrid macros they wrote on Taobao.com

<span style="color: #800000;">@mixin property-dpr($property,$px-values)</span>{<span style="color: #ff0000;">
        //判断参数是不是单个数字,若是
        @if type-of($px-values) == "number"{
                #{$property</span>}<span style="color: #800000;">: $px-values;
            [data-dpr="2"] & </span>{<span style="color: #ff0000;">
                #{$property</span>}<span style="color: #800000;">: $px-values * 2;
            }
            [data-dpr="3"] & </span>{<span style="color: #ff0000;">
                #{$property</span>}<span style="color: #800000;">: $px-values * 3;
            }
        }
        //若为数组则
        @else </span>{<span style="color: #ff0000;">
            //新建两个空数组
            $twodpr-values</span>:<span style="color: #0000ff;">()</span>;<span style="color: #ff0000;">
            $threedpr-values</span>:<span style="color: #0000ff;">()</span>;<span style="color: #ff0000;">
            //遍历多值变量
            @each $value in $px-values{
                $twodpr-values</span>:<span style="color: #0000ff;">append($twodpr-values,$value*2)</span>;<span style="color: #ff0000;">
                $threedpr-values</span>:<span style="color: #0000ff;">append($threedpr-values,$value*3)
            </span>}<span style="color: #800000;">
            // 返回处理后的多值变量
               #</span>{<span style="color: #ff0000;">$property</span>}<span style="color: #800000;">: $px-values;
                [data-dpr="2"] & </span>{<span style="color: #ff0000;">
                    #{$property</span>}<span style="color: #800000;">: $twodpr-values;
                }
                [data-dpr="3"] & </span>{<span style="color: #ff0000;">
                    #{$property</span>}<span style="color: #800000;">: $threedpr-values;
                }
        }
}</span>
Copy after login

 css, sass generated code:

<span style="color: #800000;">//调用mixin
div</span>{<span style="color: #ff0000;">
    @include property-dpr(font-size,12px);
</span>}<span style="color: #800000;">
//css style
div </span>{<span style="color: #ff0000;">
  font-size</span>:<span style="color: #0000ff;"> 12px</span>;
}
<span style="color: #008000;">/*</span><span style="color: #008000;"> line 7, ../../sass/common/_mixin.scss </span><span style="color: #008000;">*/</span><span style="color: #800000;">
[data-dpr="2"] div </span>{<span style="color: #ff0000;">
  font-size</span>:<span style="color: #0000ff;"> 24px</span>;
}
<span style="color: #008000;">/*</span><span style="color: #008000;"> line 10, ../../sass/common/_mixin.scss </span><span style="color: #008000;">*/</span><span style="color: #800000;">
[data-dpr="3"] div </span>{<span style="color: #ff0000;">
  font-size</span>:<span style="color: #0000ff;"> 36px</span>;
}
Copy after login

 That’s it for today.

  

 

 

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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

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

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 &lt;time&gt; 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

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.

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.

See all articles