在 bootstrap sass 中为暗光模式创建新的配色方案 [英] Create new color scheme for dark-light mode in bootstrap sass

查看:56
本文介绍了在 bootstrap sass 中为暗光模式创建新的配色方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为使用引导程序的网站创建暗模式.我必须添加包含所有 boostrap 颜色的新根类.这是我的colors.scss:

I want to create dark mode for a web site which use bootstrap. I have to add new root class which includes all boostrap colors. Here is my colors.scss:

$primary:#065FC6;
$secondary:#263C5C;
$success:#49C96D;
$danger:#FD7972;
$warning:#FF965D;
$light:#F8F8F8;
$body-color: #263C5C;

$custom-colors: (
  "brd-default": $body-color
  );
  

我想创建这样的新类:

:root.dark{
    // override colors and classes for dark mode
    $primary:#012345;
    $secondary:#111111;
    $success:#222222;
}

那么我如何复制和粘贴所有引导颜色以用于新的配色方案?

So how can i copy and paste all bootstrap colors for new color scheme?

如果我可以添加颜色,我将更改 HTML 类,因此我的根(配色方案)将是:

If i can add colors, i will change HTML class so my root(color scheme) will be:

在我的styles.scss中:

in my styles.scss:

@import "./colors";// custom colors
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/utilities";

推荐答案

As 此处解释,没有办法将一个类附加到:root.但是,您不需要它来实现您想要的.

As explained here, there's no way to attach a class to :root. However, you don't need this to achieve what you want.

只需创建一个 dark 类,然后您就可以根据需要将其添加到 html 或 body 标签中.

Simply make a dark class then you can add that as desired to the html or body tag.

内部 .dark{} 中更改所有需要的主题颜色,然后@import bootstrap".当正文中不存在 .dark 时,主题颜色将恢复为 Bootstrap 默认值.

Make all the needed theme color changes inside .dark{}, and then @import "bootstrap". When .dark doesn't exist on the body, the theme colors will return to Bootstrap defaults.

@import "functions";
@import "variables";
@import "mixins";


.dark {

    /* redefine theme colors for dark theme */
    $primary: #012345;
    $secondary: #111111;
    $success: #222222;
    $dark: #000;
    
    $theme-colors: (
        "primary": $primary,
        "secondary": $secondary,
        "success": $success,
        "danger": $danger,
        "info": $indigo,
        "dark": $dark,
        "light": $light,
    );

    /* redefine theme color variables */
    @each $color, $value in $theme-colors {
        --#{$variable-prefix}#{$color}: #{$value};
    }

    /* redefine theme color rgb vars (used for bg- colors) */
    $theme-colors-rgb: map-loop($theme-colors, to-rgb, "$value");
    @each $color, $value in $theme-colors-rgb {
        --#{$variable-prefix}#{$color}-rgb: #{$value};
    }

    $body-color: #eeeeee;
    $body-bg: #263C5C;
    
    --#{$variable-prefix}body-color: #{$body-color};
    --#{$variable-prefix}body-bg: #{$body-bg};
      
    @import "bootstrap";
}

Bootstrap 5 黑暗主题

这篇关于在 bootstrap sass 中为暗光模式创建新的配色方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆