Customizing Bootstrap with SCSS in React
Learn how to customize Bootstrap with SCSS to change colors and layout styles.
By Codes With Pankaj
Step 1: Set Up a React Project with Bootstrap
Before we dive into SCSS customization, ensure that you have a React app with Bootstrap already installed. If not, follow these steps:
Create a new React app:
Install Bootstrap and Sass:
bootstrap
: The Bootstrap framework for styling.sass
: To enable SCSS styling in your project.
Step 2: Enable SCSS in Your React App
After installing
sass
, React will automatically support.scss
files.Rename the
src/App.css
file tosrc/App.scss
.Open
src/index.js
and import theApp.scss
file instead of the default CSS:
Step 3: Import Bootstrap SCSS
Open the
src/App.scss
file and add the following line at the top to import Bootstrap's SCSS variables and styles:This will import the full Bootstrap framework, including its default variables, mixins, and components, so we can override them.
Step 4: Customize Bootstrap SCSS Variables
Bootstrap uses Sass variables to define key aspects of its design, such as colors, spacing, fonts, and more. To customize Bootstrap, you can modify these variables before importing Bootstrap's SCSS.
Example 1: Change Primary Color
In the
src/App.scss
file, before the@import "~bootstrap/scss/bootstrap";
line, you can customize the Bootstrap variables. For example, change the primary color:This will change all the elements that use the primary color (e.g., buttons, links, etc.) to
#ff6347
(Tomato color).
Example 2: Change Font and Spacing
To change fonts and default spacing values (like padding and margins), you can modify the following variables:
This will change the base font to Roboto and increase the default spacing used across the layout.
Step 5: Customize Specific Components
You can also override specific Bootstrap components. For example, if you want to change the appearance of the buttons:
Example 3: Customize Buttons
This will change the primary button color to Teal (#00bcd4
).
Step 6: Apply Your Custom Styles
You can also add your custom styles directly in App.scss
after importing Bootstrap:
Example 4: Add Custom Styles for Your App
You can now use your custom SCSS in your React components and ensure that Bootstrap's default styling is overridden as needed.
Step 7: Run Your Project
Start the development server:
Open your browser at
http://localhost:3000
. Your customizations should now be reflected in your app.
Summary of What You Can Customize:
Colors: Modify
$primary
,$secondary
,$success
, etc., to change theme colors.Spacing: Adjust
$spacer
and spacing variables for margins, padding, etc.Fonts: Change
$font-family-base
for default fonts across the app.Components: Customize specific Bootstrap components like buttons, forms, cards, etc.
Layout: Tweak grid behavior, container widths, and breakpoints.
Conclusion
You’ve now learned how to customize Bootstrap with SCSS in your React app, which gives you complete control over the design and layout of your app.
.
Last updated