The Ancillary Guide to Dark Mode and Bootstrap 5
A continuation of the v4 Dark Mode POC, but this time for v5
This is a follow up to The Definitive Guide to Dark Mode and Bootstrap 4 body of work, and pertains to Bootstrap 5 (Github repo.).
If you're after the same work for Bootstrap 4 please visit the vinorodrigues/bootstrap-dark repo.
A note on current state (Bootstrap 5.2 Betas):
Development efforts back at Bootstrap 5's core repository are currently geared at converting all color to CSS variables. Obviously, this greatly modifies the methodologies used in this POC and would in essence require a major re-write of the code. The addition of CSS variables also brings in some additional complexity in some of the variants (this project shows 6 methods of attaining dark mode with Bootstrap 5) - specifically the '-nightshade' and '-blackbox' variants have major issues in accommodating for CSS variables. Specifically around the
:root
CSS vars, as one cannotbody.dark :root{ }
('-nightshade'), nor[data-bs-color-scheme="dark"] :root {}
('-blackbox'), or even for that matter core's own[data-theme="dark"]
(sounding a lot like '-blackbox'). In essance the fix for this is to declare 2 sets of vars in the:root
... i.e.--bs-color-bg: ..
and--bs-color-bg-dark: ..
, and then in the remaining SCSS, code in the switch over ... thus negating the need for CSS vars in the first place as you'd just be duplicating thebackground-color: var(..)
each time it's used (or needs to switch mode) and making for a much larger CSS file.Other issues in Beta pertain to inconsistent use of CSS vars in the
_variables.scss
file, with some SCSS variables ($xxx
) used as constants in some cases with the underlying components converting that to CSS vars, and other_variables.scss
variables used asvar(--bs-*)
declarations, and the underlying components not converting to CSS vars. (This has been reported as an issue; bootstrap#36458.)Yet more issues in the Beta are that not all components have been converted to CSS vars (yet?), the largest of these is all the form components - this adding more inconsistencies to the core. (This also reported; bootstrap#36454 ... and subsequently closed.) Obviously, this makes this effort hyper-complex in that I need to account for several discrepancies and each component SCSS file is looking to become a spaghetti bowl of if-else's and switching variables. Not something I'm keen to do.
As such - I will not be updating this project's
main
branch (thus it remains tied to the v5.1.3 release) until such time as it becomes clear in what direction Mark Otto et.al. are gearing their project towards. The intent is clear - with CSS variables (for the color elements) - theming is much simpler. Dark Mode capability, in essence a dual theme driven by browser/OS preference, remains to be seen.
⚠ In the meantime, any work aligning to BS current core repo (a.k.a. 5.2 Beta) will moved over to thedev/v1.2.0
branch of this project. Note that I will attempt to align 'bootstrap-dark' variant only.What's really disapointing is that when I started this -- Bootstap-Dark was first published on the 14th May 2020 -- it was with the hopes that Dark Mode could be incorporated into BS. If not mine, then at least a functional variant. And yet ... more that 2 years later, still we have an incoherent non-starter.
I'm tempted to just archive this and walk away - I'm not a dev after all.
· · ·
And now back to the regular programming...
About
This code will make little sense if you don't read The Definitive Guide to Dark Mode and Bootstrap 4 first.
What do you get?
The code compiles the four methods (and variants - six in total) of the original body of work, but compiled for and sourcing Bootstrap 5. These are:
Method 1
bootstrap-night
: This is simply a dark bootstrap theme. It can however be used with the Bootstrap core CSS to deliver a 2-file dark mode functionality.
Method 2
bootstrap-nightfall
: This is simply the "color only" CSS of all the components of Bootstrap core, but dark, and is intended to be used as an add-on. It can also, with a simple media query, drive automatic dark mode switching.
Method 3
-
bootstrap-nightshade
: This is a modification of the Bootstrap core and adds dark color CSS for all the components, but dark, nested in ahtml.dark
class wrapper. By itself it cannot offer dark mode switching, but add the includeddarkmode.js
library and you have an interactive dark mode switching variant of Bootstrap with built in "toggle" button support. -
bootstrap-blackbox
: This variant is essentially the same as the "nightshade" variant, but instead of using a HTML tag class, it uses a HTML tag data attribute;data-bs-color-scheme
. The samedarkmode.js
library drives this one, all you need to do is add the data attribute to your HTML tag.
Method 4
-
bootstrap-dark
: This is the recommended method; one CSS with both light and dark themes, toggle-able only with the OS or browserprefers-color-scheme
media query. -
bootstrap-unlit
: This variant is essentially the same as the "dark" variant, but with "dark" scheme as the primary/fallback and "light" as optioned in color scheme.
Get started
1. bootstrap-night |
2. bootstrap-nightfall |
3. bootstrap-nightshade |
3b. bootstrap-blackbox |
4. bootstrap-dark |
4b. bootstrap-unlit |
---|---|---|---|---|---|
Quick Start Guide | Quick Start Guide | Quick Start Guide | Quick Start Guide | Quick Start Guide | Quick Start Guide |
darkmode.js Reference |
darkmode.js Reference |
||||
See Example | See Example | See Example |
The Proof is in the Pudding
Some of the test pages have been set up at vinorodrigues.github.io/bootstrap-dark-5
Can you use this?
Yes.
Licence is MIT. i.e. use as you whish as long as you credit the original authors and leave the copyright in situ.
If you're a theme builder or want to use its principles in your own project you'll need to have Git and Node installed.
- Fork or download the repository:
git clone https://github.com/vinorodrigues/bootstrap-dark-5.git
- Install Node dependencies:
npm install
(See note below.) - Modify
_variables.scss
and_variables-alt.scss
in thescss
sub-folder. - Run
npm run build
to build your theme. - The compiled code will be in the
dist
folder.
The build system is based on NPM Scripts. Most of the build tools (NPM modules) will need to be installed as "global" to ensure the scripts are executable from the command line.
npm i -g autoprefixer documentation browser-sync clean-css-cli cross-env eslint eslint-config-xo eslint-plugin-import eslint-plugin-unicorn find-unused-sass-variables imagemin-cli nodemon npm-run-all postcss-cli rtlcss sass stylelint stylelint-config-twbs-bootstrap svgo terser typescript
Note 1:
This code is just a rehash of Bootstrap 5 core code to add "dark mode" functionality and is declared as a "Proof of Concept" (PoC) - that means it's not intended as a production source, but merely an exercise to prove that dark-mode is attainable via various methods, i.e. it is an educative piece. It also means that it's author (Vino Rodrigues) is not compelled to support it.
Note 2:
This project will not compile with Node-Sass. Refer to the original Bootstrap 5 docs, especially here (https://getbootstrap.com/docs/5.1/getting-started/build-tools/#sass), and you'll note that they have deprecated the use of Node-Sass in favour of Dart-Sass.
They in turn cite this (https://sass-lang.com/blog/libsass-is-deprecated) article wherein the Sass curators state they will no longer support LibSass, that Node-Sass is dependant on.
NPM
Developers can include the scss
and dist
folders into your own project with:
npm install bootstrap-dark-5
CDN
You can also hotlink the theme via CDN with jsdelivr.com.
You can access the theme CSS file from the GitHub release:
-
bootstrap-dark
- the @mediaperfers-color-scheme
variant- Production / minified variant:
- Development / Debug variant:
- Also, read the Quick Start Guide.
-
bootstrap-nightshade
- thehtml.dark
css class + JS library variant- Production / minified variants:
- Development / Debug variants:
- Also, read the Quick Start Guide and the
darkmode.js
Reference.
-
bootstrap-night
- that dark theme only variant- Production / minified variant:
- Development / Debug variant:
- Also, read the Quick Start Guide.
Further Reading
Must reads for all developers wanting to write for dark mode:
-
...obviously, the original content: The Definitive Guide to Dark Mode and Bootstrap 4
-
web.dev, Thomas Steiner (@tomayac), Jun 27, 2019 (updated Jun 9, 2020), "prefers-color-scheme: Hello darkness, my old friend"
-
web.dev, Thomas Steiner (@tomayac), Apr 8, 2020 (updated Jun 16, 2020), "Improved dark mode default styling with the color-scheme CSS property and the corresponding meta tag"
-
CSS-TRICKS, Adhuham, Sep 9, 2020 "A Complete Guide to Dark Mode on the Web"
-
My bit about images and other considerations in my ".. Definitive Guide .." piece.
Feedback
If you have useful feedback drop me an "Issue" on the GitHub Issues page.
© 2022