React and bootstrap css

Adding Bootstrap

While you don’t have to use any specific library to integrate Bootstrap with React apps, it’s often easier than trying to wrap the Bootstrap jQuery plugins. React Bootstrap is the most popular option that strives for complete parity with Bootstrap. reactstrap is also a good choice for projects looking for smaller builds at the expense of some features.

Each project’s respective documentation site has detailed instructions for installing and using them. Both depend on the Bootstrap css file so install that as well:

Alternatively you may use yarn :

Import Bootstrap CSS and optionally Bootstrap theme CSS in the beginning of your src/index.js file:

import 'bootstrap/dist/css/bootstrap.css'; // Put any other imports below so that CSS from your // components takes precedence over default styles. 

Using a Custom Theme​

Note: this feature is available with react-scripts@2.0.0 and higher.

Sometimes you might need to tweak the visual styles of Bootstrap (or equivalent package).

As of react-scripts@2.0.0 you can import .scss files. This makes it possible to use a package’s built-in Sass variables for global style preferences.

To enable scss in Create React App you will need to install sass .

Alternatively you may use yarn :

To customize Bootstrap, create a file called src/custom.scss (or similar) and import the Bootstrap source stylesheet. Add any overrides before the imported file(s). You can reference Bootstrap’s documentation for the names of the available variables.

// Override default variables before the import $body-bg: #000;  // Import Bootstrap and its default variables @import '~bootstrap/scss/bootstrap.scss'; 

Note: You can prefix paths with ~ , as displayed above, to resolve modules from node_modules .

Finally, import the newly created .scss file instead of the default Bootstrap .css in the beginning of your src/index.js file, for example:

Источник

Introduction

Learn how to include React Bootstrap in your project.

Installation​

The best way to consume React-Bootstrap is via the npm package which you can install with npm (or yarn if you prefer).

If you plan on customizing the Bootstrap Sass files, or don’t want to use a CDN for the stylesheet, it may be helpful to install vanilla Bootstrap as well.

npm install react-bootstrap bootstrap 

Importing Components​

You should import individual components like: react-bootstrap/Button rather than the entire library. Doing so pulls in only the specific components that you use, which can significantly reduce the amount of code you end up sending to the client.

import Button from 'react-bootstrap/Button';  // or less ideally import  Button > from 'react-bootstrap'; 

Browser globals​

We provide react-bootstrap.js and react-bootstrap.min.js bundles with all components exported on the window.ReactBootstrap object. These bundles are available on jsDelivr, as well as in the npm package.

script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js" crossorigin>/script>  script  src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"  crossorigin>/script>  script  src="https://cdn.jsdelivr.net/npm/react-bootstrap@next/dist/react-bootstrap.min.js"  crossorigin>/script>  script>var Alert = ReactBootstrap.Alert;/script> 

Examples​

React-Bootstrap has started a repo with a few basic CodeSandbox examples. Click here to check them out.

Stylesheets​

Because React-Bootstrap doesn’t depend on a very precise version of Bootstrap, we don’t ship with any included CSS. However, some stylesheet is required to use these components.

CSS​

  /* The following line can be included in your src/index.js or App.js file */ > import 'bootstrap/dist/css/bootstrap.min.css'; 

How and which Bootstrap styles you include is up to you, but the simplest way is to include the latest styles from the CDN. A little more information about the benefits of using a CDN can be found here.

link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous" /> 

Sass​

In case you are using Sass the simplest way is to include the Bootstrap’s source Sass files in your main Sass file and then require it on your src/index.js or App.js file. This applies to a typical create-react-app application in other use cases you might have to setup the bundler of your choice to compile Sass/SCSS stylesheets to CSS.

/* The following line can be included in a src/App.scss */ @import '~bootstrap/scss/bootstrap'; 
/* The following line can be included in your src/index.js or App.js file */ import './App.scss'; 

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'; 

Advanced usage​

See the Bootstrap docs for more advanced use cases and details about customizing stylesheets.

as Prop API​

With certain React-Bootstrap components, you may want to modify the component or HTML tag that is rendered.

If you want to keep all the styling of a particular React-Bootstrap component but switch the component that is finally rendered (whether it’s a different React-Bootstrap component, a different custom component, or a different HTML tag) you can use the as prop to do so.

The example below passes an anchor to the as prop in a Button component. This ultimately causes a a tag to be rendered but with the same styles as a Button component.

Stack direction="horizontal" gap=2>>
Button as="a" variant="primary">
Button as link
Button>
Button as="a" variant="success">
Button as link
Button>
Stack>;

Below is an illustration of passing a React Bootstrap component. It contains a Badge component and a Button component that have been supplied to the as prop. This finally results in the rendering of a Button component with the same styles as a Badge component.

import Badge from 'react-bootstrap/Badge';
import Button from 'react-bootstrap/Button';
function Example()
return (
div>
h1>
Example heading
Badge bg="secondary" as=Button>>
New
Badge>
h1>
div>
);
>
export default Example;

Themes​

React-Bootstrap is compatible with existing Bootstrap themes. Just follow the installation instructions for your theme of choice.

Because React-Bootstrap completely reimplements Bootstrap’s JavaScript, it’s not automatically compatible with themes that extend the default JavaScript behaviors.

If you would like to add a custom theme on your app using Create React App, they have additional documentation for Create React App and Bootstrap here

Browser support​

We aim to support all browsers supported by both React and Bootstrap.

Источник

Читайте также:  Чем css лучше cms
Оцените статью