3、SASS - CSS的功能扩展

 

1、嵌套规则

sass是允许在当前css样式中嵌套另一套css样式,内层样式将外层的选择器作为父选择器;例如:

#main p {
  color: #00ff00;
  width: 97%;

  .redbox {
    background-color: #ff0000;
    color: #000000;
  }
}

编译为:

#main p {
  color: #00ff00;
  width: 97%; 
}
#main p .redbox {
  background-color: #ff0000;
  color: #000000; 
}

 

2、父选择器 &

有时候需要直接使用嵌套外层的夫选择器。当给某个元素设置hover样式时,可以用 代表嵌套外层的父选择器。例如:

a {
  font-weight: bold;
  text-decoration: none;
  &:hover { text-decoration: underline; }
  body.firefox & { font-weight: normal; }
}

编译为:

a {
  font-weight: bold;
  text-decoration: none; 
}
a:hover {
  text-decoration: underline; 
}
body.firefox a {
  font-weight: normal; 
}

 

& 必须作为选择器的第一个字符,其后可以跟随后缀生成复合的选择器,例如:

#main {
  color: black;
  &-sidebar { border: 1px solid; }
}

编译为:

#main {
  color: black; 
}
#main-sidebar {
  border: 1px solid; 
}

 

3、属性嵌套

有些css属性有着相同的前缀(命名空间)。比如font-family,font-size,font-weight 都已 font 作为属性的命名空间。为了避免重复输入,sass循序将属性嵌套在命名空间中,例如:

.funky {
  font: {
    family: fantasy;
    size: 30em;
    weight: bold;
  }
}

编译为:

.funky {
  font-family: fantasy;
  font-size: 30em;
  font-weight: bold; 
}

 

下一篇文章我们介绍 《 SASS - 注释 /* */ 与 // 》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值