- vừa được xem lúc

Basic introduction about SASS for newbie

0 0 13

Người đăng: Vivian

Theo Viblo Asia

I. Introduction

  • SASS stands for Syntax Awesome Style Sheets, is a powerful CSS extention so all valid syntaxes in CSS will be valid in SASS

  • SASS has 2 syntaxes:

1. SCSS syntax (.scss) - used most commonly

2. Indented syntax (.sass) - unuasually used

II. Installation using Node.js

npm install -g sass

III. Execution

  • Command to compile Sass to CSS: sass input.scss output.css --watch

  • input.scss: source file to build from

  • output.css: path to put output CSS to

  • watch: flag to observe the change from input file and re-compile css file each time the change happened

IV. SASS Syntaxes

1. Variables

  • Variables are places used to store information which you want to reuse throughout your sheet.
$textColor: #fff;
p {
color: $textColor;
}
  • SASS syntax uses indentation and newlines to replace curly braces {} and semicolons ; respectedly.
$textColor: #fff
p 
 color: $textColor: #fff

2. Nesting

  • Sass allows you to nest your CSS selectors as the way HTML elements nested.
<nav class="header"> <ul class="header-inner"> <li> <a></a> </li> </ul>
</nav>
nav { ul{ } li { }
}

or

.header { &-inner{ // .header-inner } &:hover{ // .header:hover }
}

3. Mixins

  • mixin is a CSS code block which you can reuse throughout your sheet. mixin can receive variables as arg so its somehow the same as function.
@mixin bgc($bgColor: "black") { background-color: $bgColor;
} body { @include bgc($bgColor: "#333");
}

4. Partials/Modules

  • A partial is a SASS file name starting with underscore _: _partial.scss
  • A partial file can contain little snippets of CSS so you can include in other SASS files, using partial is a great way to modualize and maintain CSS code.
  • Partial files will not be generated into CSS files by SASS.
  • Partials are used with @use rule, @use loads SASS file as a module so you can refer to its variables, mixins and functions.
// _base.scss
$textColor: #fff; // style.scss
@use 'base'; body { background-color: #000; color: base.$text-color;
}

5. Extend/Inheritance

  • Using @extend allows you to share a set of CSS prop from one selector to others.
%common-style { // place-holder border-radius: 25px; width: 100px;
} .yes-btn{ @extend %common-style; color: green;
} .no-btn{ @extend %common-style; color: red;
}

6. If condition statement

@if $direction == "left" { // do smt
}

7. Operators

  • Some SASS math operators are +, -, *, math.div(), %

Bình luận

Bài viết tương tự

- vừa được xem lúc

[Front-end Developer] Một vài mẹo để viết code CSS chuyên nghiệp hơn

Để tiếp tục với phần 1 của bài viết [Front-end Developer] Viết CSS sao cho chuẩn không cần chỉnh thì ở bài viết này mình xin chia sẻ một vài bí kíp nữa để chúng ta có thể nâng cao trình độ CSS của bản thân lên một tầm mới =)). 1.

0 0 25

- vừa được xem lúc

[Front-end Developer] Viết CSS sao cho chuẩn không cần chỉnh

Đôi nhời phát biểu. Trong quá trình phát triển các sản phẩm phần mềm, mình thấy việc chú ý đến code conventions(các nguyên tắc chung khi lập trình làm cho code dễ hiểu, dễ đọc và dễ bảo trì) khá là quan trọng.

0 0 15

- vừa được xem lúc

Build Blog Post Card with Animation Using HTML SCSS JavaScript

Build Blog Post Card with Animation Using HTML SCSS JavaScript. . In this video, we will make blog post card with animation using HTML, SCSS and JavaScript. .

0 0 22

- vừa được xem lúc

Cấu trúc thư mục với SASS

Khi một project phát triển và mở rộng, việc mô-đun hoá và cấu trúc là thiết yếu. Vì vậy việc tổ chức tệp tin và folder là việc cần thiết mặc dù không có cấu trúc nào "hoàn toàn chính xác" mà nó phụ th

0 0 62

- vừa được xem lúc

Học Cùng HoaL- Cài đặt môi trường SCSS

1. Cài đặt. 1.1.

0 0 18

- vừa được xem lúc

Cách kết hợp BEM, SASS và Bootstrap một cách ăn ý

Giới Thiệu. Chào mọi người, đối với những người mới học về BEM, SASS và Bootstrap như mình thì không khỏi bối rối khi muốn kết hợp cả ba một cách ăn ý.

0 0 33