css - 您可以在@media 查询中重新定义@mixin 吗?

标签 css sass media-queries

我在我的 SCSS 中定义了以下 @mixin 以创建像素和 rems 的字体大小:

@mixin font-size($sizeValue: 1){
font-size: ($sizeValue * 10) + px;
font-size: $sizeValue + rem;
}

然后我有以下内容:

h1 { @include font-size(1.6) }

h2 { @include font-size(1.4) }

h3 { @include font-size(1.2) }

但是当屏幕低于 480px 时,我想将字体大小减小 80%,但到目前为止以下代码不起作用:

@media(max-width:480px){ 
@mixin font-size($sizeValue: 1){
font-size: (($sizeValue * 10) * 0.8) + px;
font-size: ($sizeValue * 0.8) + rem;
}} 

是否可以在@media 查询中重新定义@mixin?执行此操作的最佳方法是什么?我想这样做而不必再次包含 h1、h2、h3 规则。

最佳答案

为你的混入添加一个额外的参数:

@mixin font-size($sizeValue: 1, $ratio: 1){
  font-size: (($sizeValue * 10) * $ratio) + px;
  font-size: ($sizeValue * $ratio) + rem;
}

然后,将 header 值保存为变量:

$header-1: 1.6;
$header-2: 1.4;
$header-3: 1.2;

最后:

h1 {
  @include font-size($header-1);
}

@media(max-width:480px) {
  h1 {
    @include font-size($header-1, .8);
  }
}

这将生成:

h1 {
  font-size: 16px;
  font-size: 1.6rem;
}

@media (max-width: 480px) {
  h1 {
    font-size: 12.8px;
    font-size: 1.28rem;
  }
}

关于css - 您可以在@media 查询中重新定义@mixin 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13216675/

相关文章:

javascript - 有人可以向我解释如何设置滚动功能可以向下/向上滚动以使用 jquery 的像素数量吗?

带注释的 SASS 编译

css - -ms-filter 和过滤器 CSS 规则允许的语法

css - 大型桌面的媒体查询是什么?

css - 两种不同尺寸之间的响应式设计

html - 重叠 2 个图像而不丢失居中

css 结构选择器 :first-child

css - 我怎样才能快捷颜色#F4F4F4?

css - SCSS :nth-child mixin with array

css - 荒谬的 CSS 媒体查询问题