1

I am fairly new to React and JavaScript in general.

I am using Button from react-bootstrap from https://react-bootstrap.github.io/components/buttons/ but I want to style the Button on top of it in my React app from my css file but it does not seem to apply.

my Home.js file looks like

import React from "react";
import '../App.css'; // Reflects the directory structure

export default function Home() {

  return (
    <div>
      <h2>Home</h2>
      <Button variant="light" className="formButtons">
    </div>
  )
}

My App.css file looks like

.formButtons {
  margin: 10;
  overflow-wrap: break-word;
  color: red;
}

I can tell it does not apply since the text color isn't red.

Thanks in advance!

1
  • Have you used your document inspector to see if that rule is there and overridden by something else? It's a bit hard to diagnose without a demo. Commented Jan 8, 2020 at 19:53

1 Answer 1

3

First of all you need to import the Button element from react-bootstrap. You can write something like this:

import Button from 'react-bootstrap/Button'

After that, you can remove the className attribute because React Bootstrap builds the component classNames in a consistent way that you can rely on. You can base your styles on the variant attribute, so try something like this:

Home.js

import React from "react";
import Button from 'react-bootstrap/Button'
import '../App.css'; // Reflects the directory structure

export default function Home() {

  return (
    <div>
      <h2>Home</h2>
      <Button variant="light">TEXT</Button>
    </div>
  )
}

App.css

.btn-light {
   margin: 10;
   overflow-wrap: break-word;
   color: red;
}
Sign up to request clarification or add additional context in comments.

3 Comments

My problem is that if I do <Button variant="light" style={{margin: 10}}> versus <Button variant="light"> with .btn-light { margin: 10px; } the .btn-light doesn't register the styling
@jimmyhuang0904 if you inspect the Button element which classes you see?
after using .btn-light it works! When I inspect the Button element I see both classes, but I think className properties I set in formButtons were being overridden somehow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.