scss基本语法---嵌套、循环、条件(@for,@if),混入@mixin,继承@extend,导入@import,

scss是css预编译器,可以简化css代码的书写,并可以编译成css文件使用;

有关scss的安装使用可以参考:Sass语法---sass的安装和引用_引入sass-CSDN博客

嵌套、循环、条件(@for,@if)

嵌套

scss支持选择器的嵌套写法,具体规则如下:

// scss:

body{
    color:#ccc;
    .box{
        color: #666;
    }
}
// css:

body {
  color: #ccc;
}
body .box {
  color: #666;
}

属性内部可以嵌套其他 选择器,内部的选择器会和外部的选择器形成子代选择器

//scss:

body{
    color:#ccc;
    .box{
        color: #666;
    }
    &:hover{
        color:white;
    }
}
//css:
body {
  color: #ccc;
}
body .box {
  color: #666;
}
body:hover {
  color: white;
}

嵌套伪类和伪元素时需要带上'&',否则伪类和伪元素会被识别成标签选择器

循环---@for

@for $i from start to end{}

@for $i from start through end{}

以上时@for的写法,它们的的差别在于:to不包含end,through包含end,

转换成js的循环就像:

// to:
for(let i = end;i<start;i++){
    
}

// through:
for(let i = end;i<=start;i++){
    
}

@for循环 常用来给数字差异的类名选择器子元素伪类添加差异化的属性 

// scss:

@for $i from 1 to 3{
    .box#{$i}{
        font-size: #{$i}em;
    }
}

.box{
    @for $i from 1 through 3{
        .box:nth-child(#{$i}){
            font-size: #{$i}em;
        }
    }
}
// css:

.box1 {
  font-size: 1em;
}

.box2 {
  font-size: 2em;
}

.box .box:nth-child(1) {
  font-size: 1em;
}
.box .box:nth-child(2) {
  font-size: 2em;
}
.box .box:nth-child(3) {
  font-size: 3em;
}

以上的scss代码使用了模板语法,#{}类似js的${}可以将scss变量转成css的字符;

注意 :这里的css字符是指 100px 这种,而不是 'red' 

条件---@if ,@else if, @else

和js的if语句一样,这里要注意scss里的布尔运算符(and ,or, not--- &&,||,!)

// scss:
.box {
    @for $i from 1 through 3 {
        @if ($i == 2) {
            .box:nth-child(#{$i}) {
                color:#ccc;
            }
        }

        .box:nth-child(#{$i}) {
            font-size: #{$i}em;
        }
    }
}
// css:

.box .box:nth-child(1) {
  font-size: 1em;
}
.box .box:nth-child(2) {
  color: #ccc;
}
.box .box:nth-child(2) {
  font-size: 2em;
}
.box .box:nth-child(3) {
  font-size: 3em;
}

混入@mixin,@include

@mixin混入可以定义一个css属性块,类似于js定义函数,使用@include可以将属性块的内容混入到选择器的属性中,

tips:@mixin可以携带参数

scss:

@mixin size($arg){
    width: $arg;
    height: $arg;
}
@mixin color{
    color:#ccc;
}

.box{
    @include size(100px);
    @include color;
}

css: 

.box {
  width: 100px;
  height: 100px;
  color: #ccc;
}

继承@extend

@extend 可以将其他选择器中的属性混入到自身中,

scss:

.box{
    @include size(100px);
    @include color;

}

%size{
    width: 100px;
    height: 200px;
}

.item{
    @extend .box;
    @extend %size;
}

css:

.box, .item {
  width: 100px;
  height: 100px;
  color: #ccc;
}

.item {
  width: 100px;
  height: 200px;
}

注意:当使用%标记选择器时,scss不会将其编译成css选择器,这样就变成了一个类似于@mixin混入的效果

导入@import

在scss中,可以使用@import导入(引用)其他scss文件(不同于js,scss文件并不需要导出),通常这会用来单独存放定义好的scss变量

@import 'xxx.scss'
// _root.scss:
// 使用'_'命名的scss文件不会被编译成css,
// 此文件可以作为scss模块进行导入

$bgLeft: rgb(255, 185, 185);
$bgRight: rgb(255, 228, 170);
$bg:#ccc;


// style.scss:
@import '_root.scss';

注意:使用'_'命名的scss文件不会被编译成css,

补充:sass和scss

sass和scss是同一个css预编译器,只是文件名称和写法上稍有差异,(.sass  .scss)

可以看到在写法上,scss采用的是和css一样的{}包裹块,而sass使用的是类似python这样的缩进语法,其他并无差别

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`postcss-px-to-viewport` 是一个 PostCSS 插件,用于在响应式设计中将像素单位(px)转换为视口相关的单位,如vw (viewport width) 或 vh (viewport height),以实现更灵活的布局和适应不同屏幕尺寸。当你在 Nuxt.js 项目中使用 SCSS,你可以利用 Nuxt 的官方支持或创建自定义的@mixin来整合这个功能。 首先,你需要安装 `postcss-px-to-viewport` 和可能需要的其他依赖,例如 `autoprefixer`: ```bash npm install postcss postcss-px-to-viewport autoprefixer --save-dev ``` 然后,在 Nuxt 的配置文件 `nuxt.config.js` 中启用 postcss 并添加 `postcss-px-to-viewport`: ```javascript export default { // ... build: { postcss: { plugins: { autoprefixer: {}, pxToViewport: { unitToConvert: 'px', // 要转换的单位,默认为 'px' viewportWidth: 1920, // 视口宽度,可根据实际需求设置 viewportHeight: null, // 视口高度,一般设为null自动计算 aspectRatio: 16 / 9, // 原生比例,可根据需要调整 fontFactor: 1, // 字体转换因子 unitPrecision: 5, // 小数位数 propList: [], // 需要转换的属性列表,如['*'] 或 ['all'] viewportUnit: 'vw', // 输出单位,默认为 'vw' selectorBlacklist: [], // 避免转换的类名或元素 minPixelValue: 1, // 最小转换值 mediaQuery: true, // 是否处理 media queries exclude: [], // 忽略某些选择器 }, }, }, }, // ... } ``` 接下来,在你的 SCSS 文件中,可以创建一个@mixin 来应用这个转换: ```scss // 在 Nuxt SCSS 模块中 @mixin px-to-viewport($width: 1920px) { @apply postcss-px-to-viewport(--viewport-width, $width); } // 使用 mixin .some-class { width: @include px-to-viewport(100px); height: 50px; padding: 10px; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值