Home Web Front-end HTML Tutorial SASS usage guide_html/css_WEB-ITnose

SASS usage guide_html/css_WEB-ITnose

Jun 24, 2016 am 11:40 AM

Just looking at it is useless, you will forget it all after a while, so let’s record it.

First of all, windows is still inseparable from me, so the environment is still windows..

1. SASS environment installation Configuration

SASS is written in ruby, so if you want to compile sass into a css file, you need to configure it with a ruby ​​environment.

Just download and install rubyinstaller for windows. Be sure to configure the environment variables.

For example, E:Ruby22-x64bin is configured into the system environment variable path

cmd command line execution ruby ​​-v If correct, the installation configuration is correct

The next step is to use gem to install sass for us

The general approach is to directly

gem install sass
Copy after login

But many times the report is wrong, maybe it is domestic The network is too bad and was blocked.. You can see the specific information through the -V parameter

Generally speaking, the Taobao team provides a lot of mirrors, go take a look~

Use simple commands to switch sources

gem sources --remove https://rubygems.org/gem sources -a https://ruby.taobao.org/gem sources -l*** CURRENT SOURCES ***https://ruby.taobao.org# 请确保只有 ruby.taobao.org gem install rails
Copy after login

Sass is installed successfully, let’s experience it first~

Create a new test.scss file in the sass directory, write a few sentences, and execute it directly.

You can use sass test.scss test.css to compile scss files directly into css files

2. SASS usage:

As in the test.scss file in the above example, I can define the style of the compiled css code.

* nested: Nested indented css code, it is the default value.

* expanded: Unindented, expanded css code.

* compact: css code in a concise format.

* compressed: compressed css code.

You can also directly define changes in the monitoring file, modify the scss file, and the css will be updated simultaneously

Next, let’s talk about the syntax of sass

1. Like css, directly define

div{width:50px;}
Copy after login

2. Use variables. Variables have $ identifiers . If you want to define a default variable value, just add !default at the end.

If the variable is to be placed in a string, use #{} to include it. You can use the calculation function

$width: 500px;$height: 300px !default;$side: right;div{     border-#{$side}-width: $width / 5;}  |  |  |div {  border-right-width: 100px;}
Copy after login

3. Nesting. Sass can nest selectors to indicate hierarchical relationships, which looks elegant and neat. If you want to use a parent class, use the ampersand, such as the common a:hover

$width: 500px;div{    width: $width;    .answer a{         &:hover{text-decoration:none;}    }}    |    |    |div {  width: 500px;}div .answer a:hover {  text-decoration: none;}
Copy after login

4. Mixins. Similar to defining a macro/function, and then calling

// *.scss$width: 500px;@mixin right($color: '#0f0'){     font-weight: bold;    color: $color;}div{    width: $width;    .answer{         @include right();    }}//---------------------------------------// *.cssdiv {  width: 500px;}div .answer {  font-weight: bold;  color: "#0f0";}
Copy after login

5. Import other scss or css files @import, import scss The file will be automatically compiled and expanded. If the css is imported,

//test.scss$width: 500px;div{    width: $width;    .answer a{         &:hover{text-decoration:none;}    }}@import './test1.css';@import './test1.scss';p{width: $width / 10;}//test1.cssp.new{    color: red;}//test1.scss$width: 200px;#nav{     width: $width;}//最后,test.css  可以看到,$width变量的值已经更新@import url(./test1.css);div {  width: 500px;}div .answer a:hover {  text-decoration: none;}#nav {  width: 200px;}p {  width: 20px;}
Copy after login

6. Inherit/Extend using @extend

// *.scss$width: 100px;.block{     color: #333;    border-left: 1px;}.block-1{     @extend .block;    width: $width / 2; }.block-2{     @extend .block-1;    background-color: green;}//  *.css.block, .block-1, .block-2 {  color: #333;  border-left: 1px;}.block-1, .block-2 {  width: 50px;}.block-2 {  background-color: green;}
Copy after login

7. Color function, sass has many built-in color functions , such as brightening, darkening, color gradient, etc.

For example:

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

More color functions

8. Other less commonly used methods

1] Variables can also have multiple values: similar to an array

// *.scss$px : 1px 2px 3px 4px;div{     border-left: nth($px,2);}// *.cssdiv {  border-left: 2px;}
Copy after login

2] You can use map to do key-value relationships using @each Traversal (similar to PHP syntax)

// *.scss$headings : (h1: 12px,h2:13px,h3:14px);@each $header,$size in $headings{     #{$header}{         font-size: $size;    }}// *.cssh1 {  font-size: 12px;}h2 {  font-size: 13px;}h3 {  font-size: 14px;}
Copy after login

3] Break out of selector nesting @at-root But it should be rarely used, if you want to break out of media nesting, such as @media .block-1{}, select media|all

for type. The syntax is @at-root (without: type). There are four types: all (all), rule (regular css, default), media (media), support (support)

// *.scss.block-1{     border-left:1px;    .flw{         color: red;        @at-root{             .today{                 font-size: 14px;            }        }    }    @at-root .tomo{         font-size: 12px;    }}// *.css.block-1 {  border-left: 1px;}.block-1 .flw {  color: red;}.today {  font-size: 14px;}.tomo {  font-size: 12px;}
Copy after login

4] Conditional judgment and loop, etc.

// *.scss$width: 25px;.block-5{     width: 50px;}// 现在不包含4,如果是froms 1 through 4 ,就会包含4@for $i from 1 to 4{     .block-#{$i}{         @if $i % 2 == 0 { width: $width;}        @else {width: 50px;}    }}// *.css.block-5 {  width: 50px;}.block-1 {  width: 50px;}.block-2 {  width: 25px;}.block-3 {  width: 50px;}
Copy after login

5] Of course, You can also define a function and then call ~

increase the width gradually. This css is more and more like programming..

// *.scss$width: 25px;.block-5{     width: 50px;}@function inc($num){     @return $num + 1;}@for $i from 1 through 4{     .block-#{$i}{         @if $i % 2 == 0 {             $width: inc($width);            width: $width;        }        @else {width: 50px;}    }}// *.css.block-5 {  width: 50px;}.block-1 {  width: 50px;}.block-2 {  width: 26px;}.block-3 {  width: 50px;}.block-4 {  width: 27px;}
Copy after login

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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.

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

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.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

See all articles