Tips for customizing Bootstrap theme color schemes in a React/Redux application that has been bootstrapped

As a newcomer to REACT, I am facing an issue with changing the theme colors in my freshly bootstrapped react application.

I have gone through some solutions suggested by others but none of them seem to work for me.

My entry point looks like this:

import React from 'react';
import ReactDOM from 'react-dom';
import { App } from './components/App';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap';
import './styles/app.scss';

ReactDOM.render(<App />, document.getElementById('app'));

and I have app.scss stylesheet in src/styles:

@import '_settings.scss';
@import '_base.scss';
@import './components/_header.scss';

I attempted to modify the theme colors here, but they do not reflect any changes:

@import "node_modules/bootstrap/scss/bootstrap";

$theme-colors: (
  "primary": #991196,
  "danger": #ff4136
);

Can anyone guide me on how to make this work? The existing answers and tutorials I've looked at use different methods.

The version of Bootstrap I am using is:

"bootstrap": "^4.0.0"

Answer №1

Check out this resource for using Bootstrap with webpack in ReactJS: Bootstrap webpack

This method shows how to import the compiled version of Bootstrap CSS into your project.

Instead of simply importing 'bootstrap/dist/css/bootstrap.min.css', follow these steps:

import 'bootstrap/dist/css/bootstrap.min.css'; //Avoid this

Instead, in your app.scss file, define your color variables first:

$primary: #13C7CD;
$danger: red;

Then import the SCSS version of Bootstrap using:

@import '~bootstrap/scss/bootstrap';

The use of ~ signifies that webpack will locate it in the node_modules directory.

After this, import the app.scss file in your main JS file (e.g., index.js or app.js):

import './styles/app.scss';

How Does This Work?

By defining your color variables before importing Bootstrap SCSS, the resulting compiled CSS will include your specified color values.

For Vite users, make sure to update your vite.config.js as follows:

const path = require('path')

export default {
  root: path.resolve(__dirname, 'src'),
  resolve: {
    alias: {
      '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
    }
  },
  server: {
    port: 8080,
    hot: true
  }
}

Then in your SCSS file:

@import ""~bootstrap/scss/bootstrap"";

Answer №2

To apply overrides and incorporate Bootstrap fully into your project, you can do so within the app.scss file. It's crucial to import Bootstrap after making any theme color adjustments to override the default colors defined in bootstrap _variables.scss.


    @import '_settings.scss';
    @import '_base.scss';
    @import './components/_header.scss';

    /* Import only the necessary Bootstrap files for customization */
    @import "node_modules/bootstrap/scss/functions";
    @import "node_modules/bootstrap/scss/variables";

    /* Implement customizations */
    $theme-colors: (
      "primary": #991196,
      "danger": #ff4136
    );

    /* Import bootstrap to apply changes */
    @import "node_modules/bootstrap/scss";

Once the react app is built and SASS is compiled, the resulting CSS will reflect your modifications along with Bootstrap, eliminating the need to manually import Bootstrap into react separately.

Check out a demo here:

Answer №3

https://i.sstatic.net/VpGDM.pngThe easiest method...

Ensure to include your custom styles prior to adding BOOTSTRAP styles. And that's all there is to it! 'Enjoy coding'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JQuery ID Media Queries: Enhancing responsive design with targeted

Is it possible to integrate a media query into the selection of an ID using JQuery? For example: $('#idname') $('@media (max-width: 767px) { #idname }') In essence, can you target the #idname with that specified media query in JQuery ...

Placing horizontal text over a Bootstrap 5 grid

Currently working my way through Boostrap5 while developing my website and I am extremely pleased with the progress so far. However, I have hit a snag - I want the name to appear vertically when hovering over the images on the bottom left, but for some rea ...

How to use the Enter key to submit a form in react.js with the help of "@material-ui/core/Button"

Here is the form I have created using the render method for user sign-in. <form onSubmit={this.handleSubmit}> <Avatar className={classes.avatar}> <LockOutlinedIcon /> </Avatar> <Typography component="h1" varia ...

Tips on expanding properties for Material-UI components with Typescript?

My goal is to enhance the props of the Button component from Material-UI using typescript in order to pass additional props to its children. import { NavLink } from 'react-router-dom'; import { Button } from 'material-ui'; <Button ...

The size of the cursor varies depending on the line it's placed on

I have a content editable div where three lines are separated by BR tags. When I click on the second line, the cursor becomes bigger than that in the first line. .content { line-height: 35px; } <div class="content" contenteditable="true"> ...

Error during Next.js compilation, undefined property

On the index page, there are components that display ads. const App = ({ads}) => ( { (ads.items===undefined) ? <></> : ads.items.map(item => <Ad item={item} key={item.postingId} id={item.postingId}/>) } ) export async function g ...

What is the best way to create a continuous typewriter effect in Next.js?

I have a unique project that features a typewriter effect, but I'm encountering an issue where it only types the text once. What I really want is for the typewriter effect to continuously loop, starting over and typing out the words again. I've b ...

The Safari browser displays the mobile-friendly version of the website

Everything appears correctly when looking at the website in Firefox and Chrome. However, Safari is displaying the mobile version of the site instead. I did not make any CSS changes specifically for desktop screens while working on the responsive design for ...

There is an unnecessary gap between the parent div and the image contained within it

Snippet of HTML code showcased below: <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/style.css" rel="style ...

Rendering only a portion of a component when the state updates using MobX or SWR Mutation

Issue: I am facing a problem where updating the state in MobX or calling a mutation in SWR causes all components to re-render, but I want only one element to update. For example, in the React sandbox, rendering happens twice when selecting an element (lik ...

Exploring the possibilities offered by utilizing semantic-ui-react within a React

I am looking to utilize the same component library for both web and mobile applications with React. I have had success with using semantic-ui-react for web projects, but when I tried to implement it in a react-native project, I encountered some difficult ...

flex child moves outside parent when available space shifts

My dilemma involves a flex container (A) containing two or more flex items stacked in a column. The top flex item (B) is a Bootstrap Collapse panel, while the bottom item (C) consists of two horizontal divs (D and E). The problem arises when the collapse p ...

Adding Information to a Material Design Card

Currently, I am delving into frontend development and honing my CSS skills. In my latest project, I have incorporated a mat card which can be viewed in the linked image https://i.sstatic.net/TEDcg.png. Below is an excerpt of my code: <div class=&quo ...

Tips for emphasizing a row of various list boxes when hovering in asp.net

I am facing an issue with two listboxes on my website. Currently, I can only highlight one item at a time by mousing over the listbox. However, I would like to be able to highlight items in both listboxes simultaneously when I mouseover either listbox1 or ...

What is the best way to create a <div> that will automatically start a new line based on the user's input?

Is it possible to create a div that automatically inserts a new line for each new line in the code it detects when typing? For example, if I type: <div> Hello, World! How are you doing today? </div> This would normally be displayed as ...

Whenever I attempt to type a command in Ubuntu, it shows me an error message saying "/usr/bin/env: ‘bash ’: No such file

Every time I try to type something in Ubuntu, it keeps showing the error message /usr/bin/env: ‘bash\r’: No such file or directory This issue first came up when I attempted to create a react app using the command: npx create-react-app app_name A ...

What could be causing the error message `this.props.data is undefined` to appear for me?

I followed a tutorial and encountered an error while executing it: CLICK The error message I received was: this.props.data is undefined. I was implementing the tutorial in my test application, alongside testing various React tools. I didn't copy-pas ...

What is the best way to set up Webpack for a React application that includes Express.js, GraphQL, and separate client and server folders

I'm seeking guidance and suggestions for setting up and configuring webpack in a React application that consists of both client and server folders, utilizing Express.js and GraphQL. https://i.stack.imgur.com/dRXzp.png Someone suggested running webpa ...

Whenever a user logs in or logs out from the child component, the app.js is not being re-rendered

I'm having trouble figuring out how to re-render the app.js function. It loads initially, but when I click the login or logout button, I need to call a function from the helper again to check the user status. Here is the code snippet for App.js: impor ...

What is the best way to dynamically change the color of my component depending on the prop passed to it?

I am facing an issue with the color of my component changing based on the value of the prop 'level'. Despite using states to set the backgroundColor, all components end up having the same color due to the state being altered for every comment. I ...