Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import bootstrap but only into one component

I have a React web site using a local scss compiled to css file. But now I want in one and only one of the components to use Bootstrap.

I've followed https://reactstrap.github.io/ and got it working. But bootstrap is now applied to all the site, because I have this

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

in my index.js file

If I then take the import out of index.js and place it in my component, bootstrap is still applied to the entire site.

Do I have to import bootstrap into this component and only this component using some form of React css-module loading ? Is there a way of restricting the scope of bootstraps entire css file ?

I know that in javascript something like this could be done dynamically, pseudo code below

var sheet = document.getElementById(styleSheetId);
sheet.disabled = true;
sheet.parentNode.removeChild(sheet);

then it can be added again

like image 296
Rory Avatar asked Nov 23 '17 16:11

Rory


People also ask

How do I add bootstrap to an existing project?

Under the folder, copy the extracted files downloaded from bootstrap. Under the head tag of the HTML file, the CSS needs to be linked. The jQuery downloaded should also be copied under the JS file. Make sure that under the project file, the downloaded files and HTML page must be present in that folder.

Can we use both bootstrap and css together?

Yes. You will want to put your additions in a separate file and link it below the bootstrap. css link. Then you can add your own classes and/or overwrite the Bootstrap classes in your own file.


1 Answers

You could just customize bootstrap to your needs?

You have that option in the bootstrap web page.. Just select the components you need to apply to the component you need.

https://getbootstrap.com/docs/3.3/customize/

I found this post too, which Im not quite sure if it is the same problem as yours but is quite similar:

How to make React CSS import component-scoped?

Another similar post:

Limit the scope of bootstrap styles

You might want to check it out.

like image 85
Yorki Bonilla Avatar answered Sep 19 '22 16:09

Yorki Bonilla