根据身体类别覆盖Bootstrap 4中的主要颜色?

nkk*_*law 5 css sass twitter-bootstrap bootstrap-4

我有一个网站,每个部分都有自己的主要颜色.

我想在Bootstrap 4的Sass代码中加入一些东西,根据我设置的body类重写主色.

我有这个,但到目前为止它没有工作:

$theme-colors: () !default;
$theme-colors: map-merge((
    // primary: $blue,
    // secondary: $gray-600,
    success: $green,
    info: $cyan,
    warning: $yellow,
    danger: $red,
    light: $gray-100,
    dark: $gray-800
), $theme-colors) !default;

// override according to portal
html {
    &.portal-1 {
        $theme-colors: map-merge((
            primary: #f1b36d
        ), $theme-colors);
    }
Run Code Online (Sandbox Code Playgroud)

如何在Bootstrap 4的Sass文件中实现?

Nav*_*uja 2

您无法在客户端动态更改已转换为静态 CSS 的 sass 变量。但是,要构建主题系统,您可以应用以下选项之一:

1.生成独立主题

在构建系统中放置一个主题参数,它将生成不同的主题 CSS Ex:grunt build-sass --theme=brown

var themes = {
    "default": "https://bootswatch.com/flatly/bootstrap.min.css",
    "cerulean" : "https://bootswatch.com/cerulean/bootstrap.min.css"
}

// Use something like this to add theme: (You can also append instead of overriding whole head html)
var headHTML = document.getElementsByTagName('head')[0].innerHTML;
headHTML    += '<link type="text/css" rel="stylesheet" href="' + themes.cerulean +'">';
document.getElementsByTagName('head')[0].innerHTML = headHTML;
Run Code Online (Sandbox Code Playgroud)

2.根据父类改变属性

您可以拥有一个定义通用 CSS 的基本 CSS。然后你可以根据父级拥有单独的 CSS 属性

在以下示例green-theme中将类更新为blue-theme,反之亦然

var themes = {
    "default": "https://bootswatch.com/flatly/bootstrap.min.css",
    "cerulean" : "https://bootswatch.com/cerulean/bootstrap.min.css"
}

// Use something like this to add theme: (You can also append instead of overriding whole head html)
var headHTML = document.getElementsByTagName('head')[0].innerHTML;
headHTML    += '<link type="text/css" rel="stylesheet" href="' + themes.cerulean +'">';
document.getElementsByTagName('head')[0].innerHTML = headHTML;
Run Code Online (Sandbox Code Playgroud)
div[class^=component] {
  display: inline-block;
  width:150px;
  height: 150px;
  margin: 1em;
  border: 1px solid black;
  box-shadow: 1px 1px 5px gray;
  border-radius:1em;
}

/* Colors*/
$blue1: #3066BE; 
$blue2: #090C9B;
$green1: #5C946E; 
$green2: #80C2AF;

/* Blue Theme */
.blue-theme {
  .component1 {
    background-color: $blue1;
  }
  
  .component2 {
    background-color: $blue2;
   }
}
/* Green Theme */
.green-theme {
  .component1 {
    background-color: $green1;
  }
  
  .component2 {
    background-color: $green2;
   }
}
Run Code Online (Sandbox Code Playgroud)

*运行片段将无法工作,因为我们使用的是 SCSS *