Per the Bootstrap docs,
Every Sass variable in Bootstrap 4 includes the !default flag allowing
you to override the variable’s default value in your own Sass without
modifying Bootstrap’s source code. Copy and paste variables as needed,
modify their values, and remove the !default flag. If a variable has
already been assigned, then it won’t be re-assigned by the default
values in Bootstrap.
So they recommend you create your own theme like this:
// Your variable overrides
$body-bg: #000;
$body-color: #111;
// Bootstrap and its default variables
#import "node_modules/bootstrap/scss/bootstrap";
What I would like to do is have two sets of variables, and uses this to switch between a light and dark theme:
// Your variable overrides
body.light-mode {
$body-bg: #fff;
}
body.dark-mode {
$body-bg: #000;
}
// Bootstrap and its default variables
#import "node_modules/bootstrap/scss/bootstrap";
But this doesn't seem to be working, I think because of how variables are scoped.
Is this possible? Ideally I'd like to have light and dark variables in their own file.
For a little context; I am using bootstrap in a web app created by create-react-app, and they recommend to import bootstrap as detailed here. If I just generated 2 separate style sheets I'd have copy those to my /public folder manually and reference, and they wouldn't be part of the webpack bundling.
This was the solution I went with:
// Default theme
$body-bg: #fff;
// etc...
// Bootstrap and its default variables
#import "node_modules/bootstrap/scss/bootstrap";
// dark theme, turned on by adding class dark to document.documentElement (html tag)
.dark {
$body-bg: #000;
// etc...
// Import just the things you customize
#import "~bootstrap/scss/functions";
#import "~bootstrap/scss/variables";
#import "~bootstrap/scss/mixins";
#import "~bootstrap/scss/tables";
#import "~bootstrap/scss/button-group"
// etc....
}
That won't work. Why not make two different themes - dark/light:
light theme (light-theme.scss):
// Your variable overrides
$body-bg: #fff;
// Bootstrap and its default variables
#import "node_modules/bootstrap/scss/bootstrap";
dark theme (dark-theme.scss):
// Your variable overrides
$body-bg: #000;
// Bootstrap and its default variables
#import "node_modules/bootstrap/scss/bootstrap";
and then pull the appropriate style sheet into the page as needed?
Related
Currently I'm building a template that will be used by multiple people on different projects. So making this work instantly without changing things per project install is crutial.
For this instance I want to change the $spacer variable that is used for all the margings and paddings classes that Bootstrap offers. But I cant seem to figure out how to change the $spacer variable outside of the /node_modules. I have an own _variables.scss that creates variables for the theme but an !important or anything else wont work eventhough the custom _variables.scss is loaded later that the bootstrap from the node modules.
Is there a way to send a scss file to the node_modules file so it changes the variables from within? or is there a different way to overwrite a variable from the node modules?
I always work like this and no problem:
// File: my-bootstrap-and-styles.scss
// your overrides
$primary : #FEBC35;
// include bootstrap.scss
#import "../node_modules/bootstrap/scss/bootstrap";
// Then add your additional custom code here
.my-primary-div {
background: $primary;
border: 1px solid black;
}
// also don't forget to take advantage
// of bootstrap's variables and mixins
#include media-breakpoint-down(md) {
.my-class {
overflow-y: auto;
}
}
I would like to create bootstrap blocks based on bootstrap. For this I would write a custom plugin which registers the blocks in the backend.
Each block gets its own individual stylesheet as specified by WordPress documentation. However, in order to display correctly, the blocks still need a stylesheet with global settings such as the sizes for the header, settings regarding the border box and so on. Since I work with SASS, most of this is controlled by the reboot.scss file I import.
So I have two stylesheets: One for the individual block and one global.
The stylesheet for the individual block (block.scss):
#import "../assets/scss/variables";
.myblock {
padding: 120px 0;
color: $body-color;
.myblock__inner {
color: red;
}
}
The global styleheet (global.scss):
#import "../node_modules/bootstrap/scss/functions";
#import "../node_modules/bootstrap/scss/variables";
#import "../node_modules/bootstrap/scss/mixins";
#import "../node_modules/bootstrap/scss/utilities";
#import "../node_modules/bootstrap/scss/utilities/api";
#import "../node_modules/bootstrap/scss/containers";
#import "../node_modules/bootstrap/scss/grid";
#import "../node_modules/bootstrap/scss/root";
#import "../node_modules/bootstrap/scss/reboot";
To see the correct preview of the blocks in the backend, I tried to include the global stylesheet via the hook enqueue_block_editor_assets:
add_action('enqueue_block_editor_assets', 'editor_style');
function editor_style() {
wp_enqueue_style('global', get_template_directory_uri() . '/assets/css/global.css',false,false,'all');
}
The problem now are the various global styles, which were defined by Bootstrap and destroy other blocks in the backend or make editing impossible.
Is there a way to make the global bootstrap styles work only for my blocks and not for all blocks and for the whole Gutenberg editor?
Try this
function cre8_add_editor_styles() {
add_theme_support('editor-styles');
add_editor_style([
'https://cdn.jsdelivr.net/npm/bootstrap#5.2.0-beta1/dist/css/bootstrap.min.css',
'style.css'
]);
}
add_action('after_setup_theme', 'cre8_add_editor_styles');
Source: https://cre8.agency/blog/augmenting-the-wordpress-block-editor-with-bootstrap-utilities/
I'm trying to change some variables for my custom version of Bootstrap. Right now I have the Bootstrap SCSS files and my own variables file. My file looks like this:
#import "/path/to/bootstrap";
$blue: #42a5f5;
$indigo: #5c6bc0;
more styles here…
However, when I run sass /path/to/custom.scss /path/to/output.css, it still outputs the default Bootstrap files like this:
:root {
--blue: #007bff;
--indigo: #6610f2;
more styles here…
Why does this happen?
According to Bootstrap 4:
Variable overrides within the same Sass file can come before or after the default variables. However, when overriding across Sass files, your overrides must come before you import Bootstrap’s Sass files.
Since you create your own custom.scss file, it should look like this:
// Your variable overrides
$blue: #42a5f5;
$indigo: #5c6bc0;
// Bootstrap and its default variables
#import "/path/to/bootstrap";
I've tried using in my main.scss
#import '~ag-grid-community/src/styles/ag-grid';
#import '~ag-grid-community/src/styles/ag-theme-balham/sass/ag-theme-balham';
#import '~ag-grid-community/src/styles/ag-theme-balham-dark/sass/ag-theme-balham-dark';
and then conditionally, I want to apply ag-theme-balham or ag-theme-balham-dark to the div containing ag-grid.
However, if both themes are loaded via scss, since they both build on some common theme files with variables it seems that one overrides the other?? My grid is ending up light even when I can verify through chrome inspector that the surrounding div has class="ag-theme-balham-dark" (even though dark is imported last).
Removing the import of the light theme first succesfully makes for a dark grid.
If I switch to use ag-grid's CSS instead of SCSS that does work, but the whole point of SCSS is so that I could override some theme variables?
To solve this, I ended up not importing their pre-built themes, and created custom themes. Creating the custom themes is still simple to do, since you can use the default variables as a base.
#import '~ag-grid-community/src/styles/ag-grid';
#import '~ag-grid-community/src/styles/ag-theme-balham/vars/ag-theme-balham-vars';
#import '~ag-grid-community/src/styles/ag-theme-balham/sass/ag-theme-balham-common';
.ag-theme-balham {
// $ag-params contains all values for the light theme
#include ag-theme-balham($ag-params);
}
.ag-theme-balham-dark {
$background: #2d3436;
$foreground: #F5F5F5;
// override the default params with the dark values
$dark-params: map-merge($ag-params, (
background-color: $background,
foreground-color: $foreground,
secondary-foreground-color: $foreground,
odd-row-background-color: darken($background, 3),
header-background-color: darken(#636e72, 30),
header-foreground-color: $foreground,
header-cell-hover-background-color: lighten($background, 5),
header-cell-moving-background-color: lighten($background, 5),
border-color: #424242,
hover-color: lighten($background, 7)
));
#include ag-theme-balham($dark-params);
}
https://stackblitz.com/edit/ag-grid-scss-themes?embed=1&file=src/styles.scss
Depending on what features you use in ag-grid, you may have to override more of the $ag-params values. I only overrode the ones that were needed for the example.
I've inherited a project with a ton of CSS and been assigned the task of modifying it so the color palette can easily be changed.
I've immediately thought of using a CSS preprocessor, tried less and easily switched the colors for variables, so I just have to define a base color and can switch the color theme.
The problem is, every time I switch the color theme I have to either overwrite colors.less with the new color settings or modify the colors.less import in a ton of files.
What I want is to end up with a single file with a lot of imports (basically one per component or set of components), and on that file when I import colors-red.less instead of colors-blue.less all the components imported right after use the red palette so the theme compiled is red instead of blue, for example.
The problem I am having is that the component files do not get the "globals" with the color definitions so I can't compile the base file that imports those files.
I've read there is the possibility of using "partials" (files starting with _ that won't get compiled independently but imported and then compiled), but my compiler seems to be ignoring this feature, and the eclipse plugin I use for editing and verifying less files also complains about the color variables not being defined on those partials.
How can I can get the partials to work? Is there a better approach to do this task?
Stil, they won't be defined on the imported files, just on the main file, so >compilation will break on the imported files. You see what I mean?
Nope? example:
mixins.less:
.mixin()
{
color: #color;
}
variables.less:
#color: orange;
project.less:
#import "mixins";
#import "variables";
p {
.mixin();
}
Now running lessc project.less outputs:
p {
color:orange;
}
Now i change to content of project.less as follows:
#import "mixins";
#import "variables";
p {
.mixin();
}
#color: red;
Then running lessc project.less outputs:
p {
color:red;
}