I am trying to customize a react app in bootstrap.
inside src/custom.scss
#import "~bootstrap/scss/bootstrap";
however, is there any way to import react-bootstrap
Customize Bootstrap
If you wish to customize the Bootstrap theme or any Bootstrap variables you can create a custom Sass file:
/* The following block can be included in a custom.scss
/* make the customizations */
$theme-colors: (
"info": tomato,
"danger": teal
);
/*import bootstrap to set changes */
#import "~bootstrap/scss/bootstrap";
... And import it on the main Sass file.
/* The following line can be included in a src/App.scss */
#import "custom";
Related
I use Bootstrap with SASS in my React (Next.js) project. I would now like to use the SASS variables of Bootstrap in the SASS modules (scoped) of the components.
/styles/style.scss
#import "~bootstrap/scss/functions";
#import "~bootstrap/scss/variables";
#import "~bootstrap/scss/mixins";
#import "~bootstrap/scss/maps";
#import "~bootstrap/scss/utilities";
#import "~bootstrap/scss/utilities/api";
#import "~bootstrap/scss/containers";
#import "~bootstrap/scss/grid";
#import "~bootstrap/scss/root";
#import "~bootstrap/scss/reboot";
/pages/_app.tsx
import '../styles/style.scss'
/components/text/style.module.scss
.text {
font-size: $font-size-lg;
}
Of course, I now get an error because $font-size-lg was not defined.
How do I provide the bootstrap variables in the module files so I can use them?
What is the problem ?
You are importing the Bootstrap SASS variables file in your global style.scss file. However, the SASS modules (scoped) of your React components are not aware of the variables defined in the global file.
Solution
One way to make the variables available in your SASS modules is to define them again in the module files, this way you'll have to import the variables file in every SASS file.
To avoid importing variables file in every SASS file is by defining them globally in a way that they are shared among all the SASS files. You can do this by using the CSS :export directive.
For example, in your style.scss file, you can define the variables using the :export directive:
#import "~bootstrap/scss/variables";
:export {
font-size-lg: $font-size-lg;
// other variables
}
Then in your SASS modules (scoped) of your React components, you can import these variables by using the :import directive:
:import {
font-size-lg
} from '../styles/style.scss';
.text {
font-size: font-size-lg;
}
Extra
You can also use a library like sass-module-dts-generator that will generate a typeScript file with all the exported variables, this way you'll be able to use them in your JavaScript code.
I have checked the "How to Use Sass with CSS Modules in Next.js" guide at freecodeCamp and it seems that you should import the bootstrap variables directly in your scss module.
Will it not work, if you import your /styles/style.scss in the /components/text/style.module.scss with the following import command?
#import "../../styles/style.scss"
Unfortunately, I cannot check it myself.
I'm trying to build a component library for angular and react. That's why I want to use the same styles(sass) for both libs. I created a separate folder for my styles and included my main sass inside the component. but when I try to make a test npm build and tried to use inside an angular project I paced this problem
how can I solve this?
as found in this blogpost.
create a lib for styles
nx generate #nrwl/angular:library ui
The problem now, is the #import in all the scss files.
How to make them recognize the correct files?
On angular.json on every project the path will have to be included.
"projects": {
"ds-project": {
"projectType": "application",
...
"architect": {
"build": {
...
"stylePreprocessorOptions": {
"includePaths": [ "libs/ui/src/lib/styles" ]
},
"extractCss": true,
...
Now you can import the mixins on the scss files of your project just like if they were still part of the project:
#import "mixins/list_mixin";
#import "variables";
#include list_layout;
Even the base style, like font-family are importable.
Inside the style.scss of the project to became the global styles (for this case the module contains the global styles).
// styles.scss
/* You can add global styles to this file, and also import other style files */
#import 'module';
This question already has answers here:
customizing boostrap with Sass; where exactly should I import bootstrap in my scss file?
(1 answer)
Add new utilities with Bootstrap 5
(5 answers)
How to extend/modify (customize) Bootstrap with SASS
(4 answers)
Closed 1 year ago.
I am trying to customize the predefined Bootstrap colors with Sass following the documentation.
I created a CustomBootstrap.scss file with the following contents:
#import "~bootstrap/scss/bootstrap";
$primary: #2c82c9;
This should normally override the $primary theme color with #2c82c9.
All variables in the $theme-colors map are defined as standalone variables.
But the color stays the same when using it like for example: bg-primary.
I am using React TypeScript, "bootstrap": "^5.0.1", "node-sass": "^6.0.0", other styles work so everything should be imported correctly.
The problem was that the variables should be set before the bootstrap import that way it overrides them, I use the following setup now.
I have a _variables.scss file where I keep my override variables and custom variables.
// overrides
$primary: #2191fb;
// custom
$success-light: #57cc99;
In my custombs.scss file I import the variables file and also do the required imports for adding extra custom variables and do the color setting and map merging.
// import custom variables
#import "./variables";
// required imports
#import "~bootstrap/scss/functions";
#import "~bootstrap/scss/variables";
#import "~bootstrap/scss/mixins";
// set custom colors
$custom-colors: (
"success-light": $success-light,
);
// merge maps
$theme-colors: map-merge($theme-colors, $custom-colors);
I have a styles.scss file for my other styling.
body {
font-family: "Montserrat", sans-serif;
}
And in my Main.scss I import all the needed files, with bootstrap at last (I believe this is correct).
// font
#import url("https://fonts.googleapis.com/css2?family=Montserrat:wght#500&family=Newsreader:wght#200;300;500&display=swap");
// styles
#import "./styles";
// bootstrap customization
#import "./custombs";
#import "~bootstrap/scss/bootstrap";
This way I only need one import in my index file.
import "./styles/Main.scss";
I would like some feedback on this setup, if it is correct, better ways... I am new to scss.
I try to customize the Bulma default yellow colors for my Next.js project.
I have one global Sass file where I am changing the default $yellow and $warning color. However, when I import this file in my _app.js, nothing happens.
#import "../node_modules/bulma/sass/utilities/initial-variables"
$yellow: #dc7185
$warning: $yellow
#import "bulma/css/bulma.css"
Since you want to overwrite Sass variables you'll want to import the Sass file rather than the CSS one.
/* You global Sass file */
$yellow: #dc7185;
$warning: $yellow;
#import "~bulma/bulma.sass";
I'm trying to use React Material Design (react-md) within my Gatsby JS project and am finding that the CSS rules are not being applied. So when I import a component like a Button and render the component, it has no styling on it whatsoever.
Here is what I did to install react-md:
In my gatsby-config.js I added the following
plugins: [
'gatsby-plugin-sass'
]
And then, under layouts>index.scss I have
#import '~react-md/src/scss/react-md';
And then I import that into my layouts>index.js as
import './index.scss'
When I render a Button component, it does not have any styling. I inspected the component and it has all the correct CSS classNames applied to it, but the rules do not seem to be working.
Add to your index.scss in layouts folder
#import url('https://fonts.googleapis.com/cssfamily=Roboto:400,500,700|Material+Icons'); //optional
#import '~react-md/src/scss/react-md'; // Required
$md-primary-color: $md-teal-500; //optional colors
$md-secondary-color: $md-purple-a-400; //optional colors
#include react-md-everything; // Required
NOTE: with #include react-md-everything; you get whole react-md css library.
If you want to minimize CSS prefer to use #import per respective react-md-component based on the following format:
#include react-md-components
e.g. use #include react-md-buttons to the scss-file of your react component where you want to import and use a <Button/> from react-md
Documentation: Minimizing Bundle - react-md docs