React ignores CSS rule

I am currently utilizing React to design some buttons and using CSS to style them accordingly. One of the classes I have created is called Button:

export default class Button extends React.Component{
  public className: string;

    constructor(props){
      super(props);
      this.className = props.className ? props.className : "";
    }

    public render() {
      return (
        <button className = {this.className}>
          {this.value}
        </button>
      );
    }
  }

This is the CSS code I have implemented:

.my-css {
    width: 300px;
    height: 200px;
    border-radius: 8px;
}

And here is how I am using it:

<Button className = "my-css" value = "Test" />

I am puzzled as to why my rule does not seem to be loaded when inspecting the page. Can anyone provide an explanation for this?

Any help would be greatly appreciated! :-)

Answer №1

It's important to reference your props directly within the render method:

export default class CustomButton extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <button className={this.props.className}>
        {this.props.text}
      </button>
    );
  }
}

Answer №2

Make sure you have included the css file in your project. If you missed adding it, you can do so by importing it into your main index file using "import 'path to your css file'". If the css file is already loaded, then you can proceed with updating your code as shown below.


export default class CustomButton extends React.Component {
    constructor(props) {
        super(props);
   }

public render() {
   const { className = '', value = ''} = this.props;
   return (
       <button className={className}> 
           {value}
       </button>
   );
}
}

Answer №3

One way to style your button: If you want to customize the appearance of a button, you can define a CSS object in your code.

const myButtonStyle = {
  'width': '300px',
  'height': '200px',
  'borderRaduis': '8px'
};

You can then apply this style to your button element like so:

<button style={myButtonStyle} />

Another method:

  • Create a separate CSS file for your styles
  • Import the CSS file into your JavaScript file using
    import classes from 'your-file-location.css'
  • Apply the style to your button using the imported class name:
    <button className={classes.my-button-style} />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to address unusual actions from Next/Link when used within a button

I am encountering some issues with the next/link functionality in my application. There is a reusable component that displays a button, which is used twice on the page with different URLs each time. While the button works perfectly when the page is viewe ...

Retrieve a markdown file from the system and render it as a string using React.js

Is there a way to load a markdown file from the current directory as a string in my code? This is the scenario: import { State } from "markup-it" ; import markdown from "markup-it/lib/markdown"; import bio from './Bio.md' const m ...

Tips for decreasing the height and dimensions of a Material-UI<Select> input field within a Material UI and formik interface

I'm currently working on a web project utilizing React, Material, Formik, and formik-material-ui. The issue I'm facing is with the input screen of my form. The select input has a larger height compared to the textfield inputs. https://i.sstatic ...

Tips on maintaining the original text hues that were established using setForeground() when a QTableWidgetItem is picked

Issue at Hand: Currently, when the first row is selected as shown in Figure 2 below, all text colors are changed to black, instead of maintaining their original style displayed in Figure 1. Is there a way to retain the original colors when a row is selec ...

Using jQuery and Bootstrap in an ASP.NET Core project

I am encountering an issue with the configuration of bootstrap and jquery within my project, causing these tools to fail to load properly. The problem seems to be that bootstrap is loading before jquery, resulting in error messages appearing when I check ...

Utilizing Three.js to Upload Images and Apply Them as Textures

While attempting to upload an image via URL and set it as a texture, I encountered an issue. The error message THREE.WebGLState: DOMException: Failed to execute 'texImage2D' on 'WebGLRenderingContext': Tainted canvases may not be loaded ...

using flexbox in react does not automatically resize

I am attempting to create a layout where the screen is split into two sections - one green block taking up 1/4 of the screen and one yellow block taking up 3/4 of the screen using react.js. However, when I tried implementing the code below, both blocks end ...

Tips for merging various classNames in React?

I am currently developing a small React application using Create-React-App and incorporating tachyons-css for simple styling tweaks. However, I have encountered recurring CSS styling issues which led me to transition from traditional CSS styling to CSS mod ...

How to use AngularJS to collapse various panels with unique content

Hey everyone, I'm working on developing a collapsible panel using Angular. The panel should consist of a header and body to display the content. The desired behavior is that when a button is clicked, the content collapses down, and clicking the same b ...

There was an error loading the resource as the Object does not have the method 'adGallery'

For the past two days, I have been attempting to implement the jQuery plugin "ad-gallery" on my website without success. I must mention that my knowledge in JavaScript and CSS is limited, so the issue could be something simple. The peculiar thing is that ...

I'm having trouble with axios.get when trying to search for a product by name

server-side const ProductListFilter = async (req, res) => { try { const productList = await productModel.find(req.query).collation({ locale: "en", strength: 2 }); return res .status(201) .send({ status: true, ...

Select multiple checkboxes one at a time with the help of React Hook Form

Hello everyone, I have a question regarding using react hook form for multiple checkboxes. Currently, all checkboxes can be selected at once. Does anyone have any suggestions on how to make it so only one checkbox can be selected at a time? const formMe ...

What is the process for submitting information to my Next.js API endpoint?

As part of my learning journey with Next.js, I am currently developing a test e-commerce website. To enhance the functionality, I am integrating a checkout system using Stripe. Initially, I was able to successfully implement the checkout feature with stati ...

Mastering a personalized domain integration with React on Github Pages

Despite carefully following all the instructions to direct a custom domain to my Github project page on both ends, nothing seems to be working as expected. The Github Pages hosting for the project was functioning without any issues prior to setting up the ...

What is the best way to update and override multiple nested NPM dependencies with various versions?

Apologies for not being fluent in English Here is my NPM dependency: dependency tree +-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c1e090d0f184108091a4105021f1c090f18031e2c5d4255425c">[email protected]</a> ...

Arranging shapes for optimal placement on a web page

I recently created a vibrant red SVG shape that resembles this: https://i.sstatic.net/E3P3O.jpg My goal is to incorporate these shapes into a design that looks like the following: https://i.sstatic.net/8Yf8L.jpg I attempted to embed the SVG file in an ...

What is the method for enlarging an element without regard to surrounding elements?

I am working on a code where I want the element to zoom in when hovered, completely disregarding its normal flow. By "ignoring its flow," I mean that other elements like tags might obstruct parts of its content. https://i.sstatic.net/NBoez.png https:// ...

Access Django admin without the need for traditional login by implementing an alternative authentication method

Is it possible to seamlessly redirect users to a 3rd party authentication page when accessing the Django admin site, and then redirect them back to the Admin site without requiring them to log in again? Essentially, I am looking to replace the Django admi ...

Ways to access the npm script arguments within a ReactJS project

Is there a way for me to retrieve the values of these flags? npm run start --appenv=development --build=mobile I'm looking to obtain the values of appenv and build in my React code. ...

Creating a capsule-shaped button using HTML and CSS:

I'm trying to create a button that mimics the design in the code snippet. Is this method the most effective way, or is there a simpler approach I should consider? It seems like a lot of work just to achieve rounded corners. #p1,#p2{ height:25px; ...