经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 大数据/云/AI » SAS » 查看文章
css预处理器scss/sass语法以及使用
来源:cnblogs  作者:半糖也甜吖  时间:2023/1/6 8:53:54  对本文有异议

scss

scss在css基础语法上面增加了变量 (variables)、嵌套 (nested rules)、混合 (mixins)、导入 (inline imports) 等高级功能,使用scss可以很方便的提高开发效率
scss语法以.scss文件后缀结尾,其中语法格式有两种sass,scss,两种语法在书写风格有差异,如下代码所示

scss

  1. .container {
  2. width: 100px;
  3. height: 100%;
  4. .nav {
  5. width: 100px;
  6. }
  7. }

sass

  1. .container
  2. width: 100px;
  3. height: 100%;
  4. .nav
  5. width: 100px;

语法


嵌套规则 (常用)

scss允许将一套css样式嵌入另一套样式中,外层的容器将作为内层容器的父选择器,如下代码

  1. .container {
  2. width: 500px;
  3. height: 100px;
  4. header {
  5. width: 100%;
  6. height: 20%;
  7. }
  8. main {
  9. width: 100%;
  10. height: 20%;
  11. }
  12. footer {
  13. width: 100%;
  14. height: 20%;
  15. }
  16. }

编译后

  1. .container {
  2. width: 500px;
  3. height: 100px;
  4. }
  5. .container header {
  6. width: 100%;
  7. height: 20%;
  8. }
  9. .container main {
  10. width: 100%;
  11. height: 20%;
  12. }
  13. .container footer {
  14. width: 100%;
  15. height: 20%;
  16. }

父选择器 (常用)

有时需要在内层样式内选择外层的父元素,那么就可以使用&符号,如下代码所示

  1. .container {
  2. width: 500px;
  3. height: 100px;
  4. &_header {
  5. width: 100%;
  6. height: 20%;
  7. &:hover {
  8. color: red($color: #000000);
  9. }
  10. }
  11. &_main {
  12. width: 100%;
  13. height: 20%;
  14. &:disabled {
  15. color: red;
  16. }
  17. }
  18. &_footer {
  19. width: 100%;
  20. height: 20%;
  21. &::after {
  22. position: absolute;
  23. content: '';
  24. }
  25. }
  26. }

编译后

  1. .container {
  2. width: 500px;
  3. height: 100px;
  4. }
  5. .container_header {
  6. width: 100%;
  7. height: 20%;
  8. }
  9. .container_header:hover {
  10. color: 0;
  11. }
  12. .container_main {
  13. width: 100%;
  14. height: 20%;
  15. }
  16. .container_main:disabled {
  17. color: red;
  18. }
  19. .container_footer {
  20. width: 100%;
  21. height: 20%;
  22. }
  23. .container_footer::after {
  24. position: absolute;
  25. content: '';
  26. }

属性简写 (不常用)

  1. .container {
  2. width: 500px;
  3. height: 100px;
  4. font: {
  5. family: fantasy;
  6. size: 30em;
  7. weight: bold;
  8. }
  9. background: {
  10. image: url('xxx');
  11. size: 100%;
  12. }
  13. }

编译后

  1. .container {
  2. width: 500px;
  3. height: 100px;
  4. font-family: fantasy;
  5. font-size: 30em;
  6. font-weight: bold;
  7. background-image: url('xxx');
  8. background-size: 100%;
  9. }

变量 (常用)

scss中使用$符号定义变量

  • 全局变量
    在scss文件顶部定义的变量,为全局变量
  1. $font-color: red;
  2. $font-size: 18px;
  3. $font-size-base: $font-size;
  4. .text {
  5. color: $font-color;
  6. font-size: $font-size;
  7. }
  8. span {
  9. font-size: $font-size-base;
  10. }

编译后

  1. .text {
  2. color: red;
  3. font-size: 18px;
  4. }
  5. span {
  6. font-size: 18px;
  7. }
  • 局部变量
    在属性内定义的变量为块级变量
  1. .text {
  2. $font-color: red;
  3. $font-size: 18px;
  4. $font-size-base: $font-size;
  5. h1 {
  6. color: $font-color;
  7. font-size: $font-size;
  8. span {
  9. color: $font-color;
  10. font-size: $font-size;
  11. }
  12. }
  13. }

编译后

  1. .text h1 {
  2. color: red;
  3. font-size: 18px;
  4. }
  5. .text h1 span {
  6. color: red;
  7. font-size: 18px;
  8. }

运算 (常用)

scss中支持+ - * /运算

  1. $base-width: 10;
  2. $small-width: 30;
  3. $large-width: $base-width + $small-width;
  4. .div {
  5. width: $large-width + px;
  6. }
  7. .div1 {
  8. width: $small-width - $base-width + px;
  9. }
  10. .div2 {
  11. width: $small-width * $base-width + px;
  12. }
  13. .div2 {
  14. width: calc($small-width / $base-width) + px;
  15. }

编译后

  1. .div {
  2. width: 40px;
  3. }
  4. .div1 {
  5. width: 20px;
  6. }
  7. .div2 {
  8. width: 300px;
  9. }
  10. .div2 {
  11. width: 3px;
  12. }

@extend

scss允许使用@extend集成其他样式规则

  1. .item {
  2. width: 100%;
  3. height: 20%;
  4. background-color: red;
  5. }
  6. .item-1 {
  7. @extend .item;
  8. border: 1px solid blue;
  9. }
  10. .item-2 {
  11. @extend .item;
  12. border: 2px solid blue;
  13. }

编译后

  1. .item,
  2. .item-2,
  3. .item-1 {
  4. width: 100%;
  5. height: 20%;
  6. background-color: red;
  7. }
  8. .item-1 {
  9. border: 1px solid blue;
  10. }
  11. .item-2 {
  12. border: 2px solid blue;
  13. }

@if

当条件满足时,输入对应的样式

  1. p {
  2. @if 1 + 1 == 2 {
  3. border: 1px solid;
  4. }
  5. @if 5 < 3 {
  6. border: 2px dotted;
  7. }
  8. @if null {
  9. border: 3px double;
  10. }
  11. }
  12. $type: monster;
  13. p {
  14. @if $type == ocean {
  15. color: blue;
  16. } @else if $type == matador {
  17. color: red;
  18. } @else if $type == monster {
  19. color: green;
  20. } @else {
  21. color: black;
  22. }
  23. }

编译后

  1. p {
  2. border: 1px solid;
  3. }
  4. p {
  5. color: green;
  6. }

@for

  • 语法一:@for $var from <start> through <end>
    从start开始,包含end
  1. @for $i from 1 through 3 {
  2. .item-#{$i} {
  3. width: 2em * $i;
  4. }
  5. }

编译后

  1. .item-1 {
  2. width: 2em;
  3. }
  4. .item-2 {
  5. width: 4em;
  6. }
  7. .item-3 {
  8. width: 6em;
  9. }
  • 语法二:@for $var from <start> to <end>
    从start开始,不包含end
  1. @for $i from 1 to 3 {
  2. .item-#{$i} {
  3. width: 2em * $i;
  4. }
  5. }

编译后

  1. .item-1 {
  2. width: 2em;
  3. }
  4. .item-2 {
  5. width: 4em;
  6. }

原文链接:https://www.cnblogs.com/jwyblogs/p/17027385.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号