简体   繁体   English

Sass 主题 - 是否可以创建带有变量的样式?

[英]Sass Themes - Is it possible to create a style with a variable?

I'm quite new coming over to sass and I'm trying to build my own framework, but I wondered if it's possible to create eg a theme.我是 sass 的新手,我正在尝试构建自己的框架,但我想知道是否可以创建主题等。 I know its possible but I'm unsure if its a variable that I need to use or possibly a @mixin or @extend?我知道这是可能的,但我不确定它是我需要使用的变量还是可能是@mixin 或@extend?

I've tried adding this but it doesn't work.我试过添加这个但它不起作用。

$theme1: h1,h2,h3,h4,h5 {font-family:Arial, Helvetica, sans-serif} 
$theme2: h2,h3,h4,h5 {font-family:Arial, Roboto, sans-serif}

It probably seems useless but if I could do this, then it would sure be handy to target all of these main elements if it came to changing the entire font for the theme instead of targeting them all apart from using (body) generally.它可能看起来毫无用处,但如果我能做到这一点,那么如果要更改主题的整个字体而不是除了通常使用 (body) 之外将它们全部定位,那么定位所有这些主要元素肯定会很方便。

I have tried looking up online but doesn't really cover much on creating my own framework in sass.我试过在线查找,但并没有真正涵盖在 sass 中创建我自己的框架的内容。

Any information would be greatly appreciated!任何信息将不胜感激!

Kindest Regards, B最诚挚的问候,B

you can use your class name as variable like:您可以使用您的 class 名称作为变量,例如:

   .#{$theme1} {
   h1,h2,h3,h4,h5 {font-family:Arial, Helvetica, sans-serif} 
}

but if you want to custom some style base on your theme you can use condition like following code:但是如果你想根据你的主题自定义一些样式,你可以使用如下代码的条件:

    body {
    

    @if $theme1 {
        // Manually use the if/else instead of the mixin
        h1,h2,h3,h4,h5 {font-family:Arial, Helvetica, sans-serif} 
    }
    @else {
        // Otherwise 
        h2,h3,h4,h5 {font-family:Arial, Roboto, sans-serif}
    }

}

for more information you can visit following links:有关更多信息,您可以访问以下链接:

SCSS variable class name , https://css-tricks.com/snippets/sass/use-sass-variable-selector/ SCSS 变量 class 名称https://css-tricks.com/snippets/sass/use-sass-variable-selector/

Here is a basic idea shown on the example of a dark/light theme.这是在深色/浅色主题示例中显示的基本概念。


$theme: light;

@if($theme == light) {
  body {
    color: #000;
    background-color: #fff;
  }
}

@if($theme == dark) {
  body {
    color: #fff;
    background-color: #000;
  }
}

@if($theme != light) and ($theme != dark){
  @error "theme is either null or invalid value";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM