scss中@mixin和@include

在 Sass 中,@include指令用于在您的样式中包含一个 mixin。mixin 是可重用的样式块,您可以将其包含在样式表的多个位置。
以下是如何使用该@include指令的示例:

@mixin rounded-corners {
  border-radius: 5px;
}

.button {
  @include rounded-corners;
  background-color: blue;
  color: white;
  padding: 10px;
}

.card {
  @include rounded-corners;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

在此示例中,rounded-cornersmixin 是使用border-radius属性定义的。该@include指令用于将 mixin 包含在.button和.card类中。这将生成以下 CSS:

.button {
  border-radius: 5px;
  background-color: blue;
  color: white;
  padding: 10px;
}

.card {
  border-radius: 5px;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

该@include指令还可用于将参数传递给混入。

@mixin rounded-corners($radius) {
  border-radius: $radius;
}

.button {
  @include rounded-corners(5px);
  background-color: blue;
  color: white;
  padding: 10px;
}

.card {
  @include rounded-corners(10px);
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

例如:在此示例中,rounded-cornersmixin 接受单个参数,$radius用于设置border-radius属性。该@include指令用于将不同的值传递$radius给每个类的混合。这将生成以下 CSS:

.button {
  border-radius: 5px;
  background-color: blue;
  color: white;
  padding: 10px;
}

.card {
  border-radius: 10px;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值