如何根据 Bootstrap 5.3 中的明暗主题反转背景颜色?

huangapple go评论77阅读模式
英文:

How to invert background color based on the light/dark theme in Bootstrap 5.3?

问题

在Bootstrap 5.3中,我们可以指定用于网站的主题。默认情况下,有两种支持的主题DarkLight

如果我设置<html data-bs-theme="dark">,我想将网站导航栏设置为浅色。同时,当设置<html data-bs-theme="light">时,我希望导航栏具有深色背景。

在Bootstrap中是否有一个特定的类可用于根据当前主题反转颜色?

我尝试使用body-*类来实现,但那并不符合我的要求。我想选择与当前text-bodybg-body相反的颜色。

<nav class="navbar text-body bg-body">
    
</nav>

在Bootstrap 5.3中如何根据浅色/深色主题反转背景颜色?

英文:

In Bootstrap 5.3, we are able to specify a theme to use for the site. Out of the box there are two supported theme Dark and Light.

If I set my &lt;html data-bs-theme=&quot;dark&quot;&gt;, I want to set the site navbar to a light color. At the same time, when setting &lt;html data-bs-theme=&quot;light&quot;&gt; I want the navbar to have dark background colors.

If there a specific class in Bootstrap that can be used to invert the colors based on the current theme?

I tried using the body-* classes to accomplish, but that does not do what I want. I want to select the opposite of the current text-body or bg-body.

&lt;nav class=&quot;navbar text-body bg-body&quot;&gt;
       
&lt;/nav&gt;

How to invert background color based on the light/dark theme in Bootstrap 5.3?

答案1

得分: 1

你可以通过使用 CSS 变量来实现这一点。

首先,在 :root 中定义主题颜色:

:root {
  --bg-body-dark: #202124;
  --bg-body-light: #fff;
  --text-body-light: #191919;
  --text-body-dark: #f8f9fa;
}

然后,为 .bg-body 类分配一个颜色,使用 "light" 主题:

[data-bs-theme=light] {
  --bg-body: --bg-body-light;
  --text-body: --text-body-light;
}

以及使用 "dark" 主题时 .bg-body 的颜色:

[data-bs-theme=dark] {
  --bg-body: --bg-body-dark;
  --text-body: --text-body-dark;
}

现在,在不同主题之间切换时,nav 的背景和文字颜色应相应更新。

Bootstrap 5.3 定义了自己的主题颜色变量,你可以随意使用它们,而不是我上面声明的那些。

如何根据 Bootstrap 5.3 中的明暗主题反转背景颜色?

希望对你有帮助。

英文:

You can achieve this using css variables.

First you define your main theme colours in :root

:root {
  --bg-body-dark: #202124;
  --bg-body-light: #fff;
  --text-body-light: #191919;
  --text-body-dark: #f8f9fa;
}

Then you assign a colour to use for the .bg-body class with the "light" theme:

[data-bs-theme=light] {
  --bg-body: --bg-body-light;
  --text-body: --text-body-light;
}

And which colour to use for .bg-body with the "dark" theme:

[data-bs-theme=dark] {
  --bg-body: --bg-body-dark;
  --text-body: --text-body-dark;
}

Now when you toggle between themes, the nav background and text colour should update accordingly.

Bootstrap 5.3 defines it's own theme colour variables:

如何根据 Bootstrap 5.3 中的明暗主题反转背景颜色?

Feel free to use these instead of the ones I declared above.

I hope this helps.

答案2

得分: 1

J 先生的方法将全局反转 .bg-body.text-body。我不确定这有多有用,因为在这种情况下,你可以(应该)只是使用相反的主题。


嵌套颜色模式(JS)

我会使用 salif 的建议,Bootstrap 称之为 嵌套颜色模式

<body data-bs-theme="dark">
  <nav id="nav" data-bs-theme="light" class="navbar text-body bg-body">

然后在你的 JS 颜色模式切换器中,翻转两个 data-bs-theme 属性:

window.onload = function() {
  document.getElementById('themer').addEventListener('click', () => {
    const current = document.documentElement.getAttribute('data-bs-theme')
    const inverted = current == 'dark' ? 'light' : 'dark'
    document.documentElement.setAttribute('data-bs-theme', inverted)
    document.getElementById('nav').setAttribute('data-bs-theme', current)
  })
}

反转类(仅 CSS)

如果你只想使用 CSS 完成,定义 -inverted 类来交换前景和背景:

  • .bg-body-inverted(反转主题背景)映射到 --bs-body-color(主题前景)
  • .text-body-inverted(反转主题前景)映射到 --bs-body-bg(主题背景)

这些 -inverted 版本将在不考虑颜色模式的情况下反转前景/背景:

.bg-body-inverted {
  background-color: var(--bs-body-color) !important;
}

.text-body-inverted {
  color: var(--bs-body-bg) !important;
}
英文:

Mr. J's approach will invert .bg-body and .text-body globally. I'm not sure how useful that is because you could (should) just use the opposite theme in that case.


Nested color modes (JS)

I would use salif's suggestion, which Bootstrap calls nested color modes:

&lt;body data-bs-theme=&quot;dark&quot;&gt;
  &lt;nav id=&quot;nav&quot; data-bs-theme=&quot;light&quot; class=&quot;navbar text-body bg-body&quot;&gt;

Then in your JS color mode toggler, flip both data-bs-theme attributes:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.onload = function() {
  document.getElementById(&#39;themer&#39;).addEventListener(&#39;click&#39;, () =&gt; {
    const current = document.documentElement.getAttribute(&#39;data-bs-theme&#39;)
    const inverted = current == &#39;dark&#39; ? &#39;light&#39; : &#39;dark&#39;
    document.documentElement.setAttribute(&#39;data-bs-theme&#39;, inverted)
    document.getElementById(&#39;nav&#39;).setAttribute(&#39;data-bs-theme&#39;, current)
  })
}

<!-- language: lang-html -->

&lt;html data-bs-theme=&quot;dark&quot;&gt;

&lt;head&gt;
  &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;nav data-bs-theme=&quot;light&quot; id=&quot;nav&quot; class=&quot;navbar text-body bg-body&quot;&gt;
    &lt;div class=&quot;container-fluid&quot;&gt;
      &lt;span class=&quot;navbar-brand mb-0 h1&quot;&gt;Inverted navbar&lt;/span&gt;
      &lt;a id=&quot;themer&quot; href=&quot;#&quot; class=&quot;btn btn-primary m-3&quot; role=&quot;button&quot; data-bs-toggle=&quot;button&quot;&gt;Toggle theme&lt;/a&gt;
    &lt;/div&gt;
  &lt;/nav&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->


Inverted classes (CSS only)

If you want to do it with just CSS, define -inverted classes to swap foreground and background:

  • .bg-body-inverted (inverted theme background) gets mapped to --bs-body-color (theme foreground)
  • .text-body-inverted (inverted theme foreground) gets mapped to --bs-body-bg (theme background)

These -inverted versions will invert fg/bg regardless of color mode:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.bg-body-inverted {
  background-color: var(--bs-body-color) !important;
}

.text-body-inverted {
  color: var(--bs-body-bg) !important;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;

&lt;body data-bs-theme=&quot;dark&quot;&gt;
  &lt;nav class=&quot;navbar text-body-inverted bg-body-inverted&quot;&gt;
    &lt;div class=&quot;container-fluid&quot;&gt;
      &lt;span class=&quot;navbar-brand text-body-inverted mb-0 h1&quot;&gt;Inverted navbar&lt;/span&gt;
    &lt;/div&gt;
  &lt;/nav&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月21日 03:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定