Using Styling Libraries with React
Learning Objectives
Understand what React-Bootstrap is
Implement React-Bootstrap into React Applications
Using React-Bootstrap pre-styled components
React Bootstrap Initialisation
React Bootstrap is a popular frontend styling library, it contains pre-built style and functionalities for out of the box easy implementation. This tool is integrated well with ReactJs and is geared for beginners to test out and implement styling libraries. Developers are even able to customize Bootstrap within custom scss stylesheets, feel free to learn more about it here.
To implement React Bootstrap into a React application open a CLI interface and change directory to your current project. Here you can run this command:
This will install React Bootstrap and the bootstrap package within the application, there is still one more step that must be taken before you can utilise all of React-Bootstrap in your app.
Go to the main.jsx within your React application and this line at the top of the file:
This line imports the CSS for bootstrap into your application, any child components of this file will be able to access and utilise React-Bootstrap styling, pre-made components, utilities and alignment system.
Now that Bootstrap has been implemented in your application it might be a good idea to look at some of the pre-styled components you can implement, look at some examples and usage here.
To showcase implementing React Bootstrap into a React Application we will implement some new styled buttons on our previously edited boilerplate code from our previous section.
React Bootstrap Component
We will import Button from react-bootstrap such that we get pre-styled buttons without much effort. Alter the App.js file to reflect code below:
The output on the browser is as follows:
From the image above we can see a new blue button has been added into the application, we didnt style this button, it was all React-Bootstrap. This is how you can utilise React-Bootstrap within a React application. Utilise more complex components using this styling library to save yourself time during development.
Checkout more React-Bootstrap Components here. Use a combination of pre-styled and custom components to quickly develop applications that can be tailored to your need, just be aware of the props that you can pass. Constantly run your React application to see whether or not your application is being styled appropriately. And remember when styling with React-Bootstrap you just need to import the component's that you need at the top of the page, then you can use them like normal HTML tags in your JSX.
React Bootstrap Grid System
One of the most powerful features of React-Bootstrap is its grid system, which facilitates mobile responsive websites without having to restyle every single element on your page. Learn more about the grid system:
When implementing the grid system be sure to:
Use a react-bootstrap
Container
ComponentUse
Row
andColumn
Components within theContainer
to display your informationNote that each row can be broken into 12 columns
Each section can take up multiple spaces
Uncover and understand the Breakpoint system in react-bootstrap
Wire-framing and planning out how your component will look will make using grid easier
Here is a simple implementation of the react-bootstrap grid system
It is possible to make the Col
Components respond to the windows width, based off the breakpoints that were pointed out earlier. This allows columns to wrap and resize their content to ensure that data is shown exactly as intended, to do this we need to make use of Bootstraps breakpoint properties on our Col
Components as shown below. You are able to apply as many breakpoint properties on a Col
as are needed.
Below is the output of the code above, depending on the size of the window.
The Grid system in react-bootstrap
affords developers an easy way to dynamically resize content dependant on their users screens. This system of Bootstrap Components comes together to build fully responsive webpages, it should be noted that the underlying system used here is flexbox. This system is a fantastic way to ensure that an applications content can be visible to users across multiple device sizes without the need to add in hundreds of @mediaqueries.
Last updated