[Ruby on Rails系列]5、专题:Talk About SaSS

本文详细介绍了SASS作为CSS扩展语言的特点、如何编写SASS代码、实例演示了变量、计算、嵌套、混合、选择器继承和函数等核心功能,并展示了通过在线环境进行代码测试的方法。

This is my presentation in the Rails lecture of Hosei University.

I will introduce SaSS by 5 examples. 

 

1, What is SASS?


We can get into the official site of SASS.

image

The official site give the definition of Sass.

Sass is the most mature,stable,and powerful professional grade CSS extension language in the world.

 

[Here are some characters of SASS]

•Sass (Syntactically Awesome Stylesheets) is a stylesheet language

•Sass is a scripting language that is interpreted into Cascading Style Sheets (CSS).

• Sass (in the larger context of both syntaxes) extends CSS by providing several mechanisms available in more traditional programming languages, particularly object-oriented languages, but that are not available to CSS3 itself.

•Sass is simply syntactic sugar for CSS

 

2,How to Write SASS


How to write Sass? If I just want to try, Do I need setup environment?

 

It is very easy to write Sass code. If you just want to try, you do not need config your environment. What you should do is using this website:http://sassmeister.com/ 

image

You can test your code here. It is a cool web environment to write SaSS code. You write SaSS code, then the environment will transfer it to CSS code. It is so cool to use it.

 

3,Some Examples


Now I get some examples to write Sass.

[Example 1]variables

Sass allow us to define variables.

SASS Code is here:

$blue : #1875e7;
div {
color : $blue;
}

It will be transfer to css  code.

image

image

Now I write HTML code to test it.

image

The result:

image

 

[Example 2]computing

 

Sass allow us to write some Expression. Then it will computing the expression.

SASS code is here:

image

 

[Example 3]nesting

 

CSS does support logical nesting, but the code blocks themselves are not nested. Sass allows the nested code to be inserted within each other.

 

Sass code:

nav {
  ul { 
    margin: 0; 
    padding: 0; 
    list-style: none;
  } 
  li { 
    display: inline-block; 
  } 
  a { 
    display: block; 
    padding: 6px 12px; 
    text-decoration: none; 
  } 
}

Css code will be automatic generated:

image

HTML code and test:

image

image

 

[Example 4]mixins

 

Mixins is just like the macro in C Program language.

Sass code :

@mixin left ($value: 10px) {

float: left;

margin-left: $value;

}


div  {
  @include left (30px)
}

image

image

image

 

[Example 5]Selector inheritance

 

SaSS code:

.message {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;
}

.success {
  @extend .message;
  border-color: green;
}

.error {
  @extend .message;
  border-color: red;
}

.warning {
  @extend .message;
  border-color: yellow;
}

image

image

image

 

[Example 6]function

 

Sass code:

@function changecolor ($color) {

@return $color;

}

#sidebar {

color: changecolor(red);

}

image

image

image

 

 

It is very interesting! Just enjoy it !

转载于:https://www.cnblogs.com/Kassadin/p/4029731.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值