I am trying to apply margins and paddings with React-Bootstrap as props.
I passed the docs through but haven't found any mention adding padding or margin in there as it is in official bootstrap docs (3th and 4th). I know it doesn't support well Bootstrap 4, so tried with both.
I tried to pass params as p={1}
, paddingxs={5}
or mt='1'
but it doesn't recognize any of them. More over tried to find any Spacing element in React-Bootstrap folder, but failed.
Paddings and margins work as classnames. But I feel there must be a way to it without Bootstrap classes. There must be a kind of property.
You can add margin and padding by using default React's style:
Refrenced from here
You'll want to add className="p-5" to the element. This isn't documented in react-bootstrap but it's in the original Bootstrap 4 documentation here: https://getbootstrap.com/docs/4.4/utilities/spacing/#examples
Just try this out once according to your input and still if face any issue you can reach out.In below we have increased the .col padding with .px-md-5 and then countered then with .mx-md-n5 on the parent .row.
JSX:
If you still have any kind of doubt on this then feel free to ask .
First include bootstrap CSS in your
src/index.js
orApp.js
Then you can style your component by passing desired bootstrap CSS class name as
className
prop in React, for example:Above code will add
p-0
CSS class toContainer
.Reference
React - How do I add CSS classes to components?
React-Bootstrap - Stylesheets
None of the jQuery-free implementations of Bootstrap (React Bootstrap, BootstrapVue or ngBootstrap) have implemented utility directives for spacing (
margin
/padding
), simply because Bootstrap have made it unnecessary in the vast majority of cases, by providing a very intuitive set of Spacing utility classes.All you need to do is apply the desired class.
To apply utility classes selectively, based on responsiveness interval (media queries), you could use a
useMedia
hook, as demoed here.In a nutshell:
(Based on
useMedia
from useHooks/useMedia).You can now reuse this hook throughout your app to add media interval based logic.
Example usages:
Important: this particular implementation returns the first matching media query in the list.
If you need to map various media queries, to an object/map with
true/false
values, you'll need to modifygetValue
fn to return the entire list, along these lines:Working example here.
Obviously, you could expand on it and add/remove queries. However, be warned each query adds a separate listener so it could impact performance.
In most cases, the return of the first matching query (first example) is enough.
Note: if the above useMedia hook is not enough for your use case, a more robust and heavily tested solution for media-query listeners in JS is enquire.js. It's easy to use, incredibly light and thoroughly tested cross-browser/cross-device. I have no affiliation with it, but I have used it in various projects over the course of more than a decade. In short, I couldn't recommend it more.
Back to Bootstrap 4: in order to customize the $spacer sizes, follow the guide provided under Bootstrap's theming as it's actually about more than what we typically call theming (changing colors), it's about overriding default values of Bootstrap's SASS defaults, including responsivenss breakpoints, spacers, number of columns and many, many others. The one you're interested in is $spacer.
Simply write the overrides into an
.scss
file and import it in your root component. Example.Note: a (simpler and more intuitive) option to customize Bootstrap is to do it visually, using bootstrap.build but it's typically a few minor versions behind (i.e. Bootstrap is now at
v4.4.1
and the build tool is atv4.3.0
).The build customizer provides intuitive controls and real time visualization.
It allows export as
.css
or.scss
.Usually, I'm able to add custom styles to React Bootstrap components by just adding them to the style param. I've put together a short example below, hope this helps.
You can also pass custom components (including those from react-bootstrap) into the styled-components constructor if you prefer to do it that way.