全局变量,局部变量,默认变量

  1. $width: 300px; //普通变量
  2. $width: 100px; //后面覆盖了前面
  3. $height: 50px;
  4. $height: 200px !default; //默认变量
  5. #div1{
  6. width: $width;
  7. height: $height;
  8. }
  9. //全局变量
  10. $color: red;
  11. #div2{
  12. background-color: $color;
  13. font-size: 30px;
  14. .box{
  15. $color: orange; //局部变量
  16. color: $color;
  17. }
  18. h2{
  19. color: $color;
  20. }
  21. }
  22. #div3{
  23. background-color: $color;
  24. font-size: 30px;
  25. .box{
  26. $color: orange !global; //全局变量
  27. color: $color;
  28. }
  29. h2{
  30. color: $color;
  31. }
  32. }
  33. //特殊变量
  34. $xxx: left;
  35. #div4{
  36. border-#{$xxx}: 1px solid black;
  37. }

选择器嵌套

  1. $fontSize: 20px; //规定 1em = 20px
  2. //选择器嵌套
  3. #div1{
  4. width: 100px;
  5. height: 200px;
  6. .box{
  7. background-color: red;
  8. font-size: 13px;
  9. a{
  10. text-emphasis: none;
  11. //& 父选择器
  12. &:hover{
  13. color: orange;
  14. }
  15. }
  16. }
  17. }

sass混合—@mixin

  1. //混合 清除浮动
  2. // 让节点居中
  3. @mixin center-block{
  4. display: flex;
  5. justify-content: center;
  6. align-items: center;
  7. }
  8. @mixin space-block{
  9. display: flex;
  10. justify-content: space-between;
  11. align-items: center;
  12. }
  13. //清除浮动
  14. @mixin clearBoth {
  15. content: "";
  16. height: 0;
  17. clear: both;
  18. display: block;
  19. overflow: hidden;
  20. visibility: hidden;
  21. }
  22. @mixin border-bottom {
  23. position: relative;
  24. &::after { //&代表给当前元素添加伪类
  25. content: '';
  26. position: absolute;
  27. left: 0;
  28. bottom: 0;
  29. width: 100%;
  30. height: 1px;
  31. background: #e9e9e9;
  32. transform: scaleY(0.5);
  33. }
  34. }
  35. ul{
  36. @include border-bottom;
  37. @include center-block;
  38. li{
  39. width: 100px;
  40. height: 100px;
  41. }
  42. }
  43. ol{
  44. @include space-block;
  45. li{
  46. background-color: red;
  47. @include clearBoth;
  48. }
  49. }

sass继承扩展

  1. .btn{
  2. width: 300px;
  3. height: 50px;
  4. background-color: yellow;
  5. border: 1px solid black;
  6. a{
  7. color: red;
  8. &:hover{
  9. background-color: blue;
  10. }
  11. }
  12. }
  13. .btn-success{
  14. @extend .btn;
  15. background-color: greenyellow;
  16. }
  17. .btn-danger{
  18. @extend .btn;
  19. background-color: red;
  20. }

sass使用公共样式

  1. //使用公共样式
  2. //1、声明公共样式 _base.scss
  3. //2、引入 @import "base"
  4. @import "reset";
  5. @import "mixin";
  6. #div1{
  7. .box{
  8. @include border-bottom;
  9. }
  10. }