Connecting a remote CSS file to a React component: Step-by-step guide

I am currently working on a React component and I want to incorporate Google's MDL (Material Design Lite) into it. I followed the instructions provided on their website here, which state that I need to link the CSS from their server. However, when I added the links to the main index.html file and referenced the styles in my component, it doesn't seem to be applied. Can someone guide me on the correct way to do this? Keep in mind that I am new to working with React.

Below is the code for my component:

import React from 'react';

class App extends React.Component {
   render() {
      return (
       <div>
           <button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">
                   New button
           </button>
       </div>

      );
   }
}

export default App;

This is the content of index.html:

<!DOCTYPE html>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>

<html lang = "en">

   <head>
      <meta charset = "UTF-8">
      <title>React App</title>
   </head>

   <body>
      <div id = "app"></div>
      <script src = "index.js"></script>
   </body>

</html>

Answer №1

In React, it is important to use className instead of class when referring to CSS classes. This is because class is a reserved word in ES6 and since JSX involves writing JavaScript rather than HTML, using className is the correct approach. Here is an example:

<button className="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">
    New button
 </button>

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

The list marquee in HTML, JS, and CSS is not being properly rendered on

I recently made changes to a code that showcases a marquee scrolling a basic HTML list. Check it out here: I am facing 3 issues that I am struggling to resolve: JS: I want the marquee to continuously show text re-entering from the right just after it ...

Navigating with React: How can I automatically route to the corresponding tab based on the URL?

I've been grappling with this issue for days now - I can't figure out how to automatically navigate to a specific tab when entering its URL. You can check out the code here In the code, I created a tab using Material-UI tabs component. By appen ...

Make sure to only update the state in useEffect after retrieving the data from the localStorage

Trying to troubleshoot the continuous looping and crashing issue in my app caused by passing variables in the dependency array of the useEffect hook. These variables are state variables from the context provider. The goal is to make the useEffect hook run ...

What is the best way to add a value to the value attribute of a div using JavaScript?

Is there a way to insert a value into the value attribute of a div using Internet Explorer 9? I have attempted various JavaScript functions, but so far I can only modify the content inside the div and not the value attribute itself. <div id="mydiv" val ...

Tips for merging two text boxes in a form with CSS

Is it possible to combine two text boxes in a form using CSS, similar to the design on Tumblr's login page at ? If CSS is not the solution, what alternative methods can be used? Any assistance would be greatly appreciated. I am aiming to enhance the ...

Resize the Bootstrap select element based on its contents

My HTML code includes a <select> element that is styled using Bootstrap 5. <div class="col-md-12"> <div class="mb-3"> <label asp-for="Schedule.Minutes" class="control-label"></l ...

Increase the background image size for a <li> element

I am working with an unordered list and have set it up to display a background image on the li element when the cursor hovers over it: <div class="sidebar"> <ul> <li>test</li> <li>test</li> ...

I am attempting to develop a button that will navigate me to the SettingsScreen.tsx 'section'

I'm encountering persistent errors no matter how I approach it. (Apologies for the slightly messy code) import React, { useEffect, useState } from "react"; import { View, Text, StyleSheet, Image, Animated, Switch, TouchableOpacity, Button, ...

Adding Material-UI icons dynamically in a React TypeScript project requires understanding the integration of imported icons

I have a collection of menu buttons with icons, stored in an array of objects. The icon names are saved as strings that correspond to Material UI icons: interface MenuButton { text: string, onClickFunction: Function icon: string } export defau ...

What is the default DTD used if it is not specified in the doctype declaration?

On my webpage, the doctype is specified as: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> No DTD is explicitly set. I'm curious which DTD will be defaulted in IE? It seems that it doesn't function the same way as "http ...

Gleaming Dashboard tabBox with tabPanel Styles

Is there a way to customize the background color and other CSS settings when using tabBox and tabPanel in a Shiny dashboard? I've attempted to use .tab-pane, but it ends up changing the background color of the entire dashboard instead of just the pane ...

Angular Universal causes SASS imports to malfunction

Check out this sample app that you can download to see the issue in action: https://github.com/chrillewoodz/ng-boilerplate/tree/universal I am currently working on implementing angular universal, but I'm encountering an error with a SCSS import in o ...

The error message indicates that the argument cannot be assigned to the parameter type 'AxiosRequestConfig'

I am working on a React app using Typescript, where I fetch a list of items from MongoDB. I want to implement the functionality to delete items from this list. The list is displayed in the app and each item has a corresponding delete button. However, when ...

Customizing the Switch component individually for each item fetched from an API in React Native

I'm struggling with setting the switch button individually for each item in my API. Despite trying multiple solutions, none of them seem to work for me. const results = [ { Id: "IySO9wUrt8", Name: & ...

A guide to utilizing CSS to showcase the content of all four div elements in HTML body simultaneously

Is there a way to use CSS to display the content of all 4 divs in the same place on the HTML body, based on the URL clicked? Only one div should be displayed at a time in the center of the page. For example, if URL #1 is clicked, it should show the conten ...

Which tools are essential for setting up a React and Babel development environment?

Currently in the process of setting up react with Babel. I'm confused about the various installation options available. Can someone please explain the differences and recommend which ones I should select? npm install --save-dev babel-preset-react npm ...

Importing Data on the Fly into Django Model

I'm currently exploring ways to dynamically load information into a modal for a quick preview on my ecommerce platform. Any guidance or suggestions would be greatly appreciated as I'm a bit uncertain about the best approach to take. I've exp ...

Tips for correctly linking React code with HTML (I keep getting the "Target container is not a DOM element" error)

I've been struggling to set up a react web app with webpack 4, and I keep getting the error message "Uncaught Invariant Violation: Target container is not a DOM element." in the console. I've spent the last two days trying everything and reading ...

The MaterialUI theme maintains a consistent typography color throughout

I've been trying to update the Typography color through a theme, but without success. The text "Hello World 1" remains #3f51b5 in color, and "Hello World 2" is still rgba(0, 0, 0, 0.87). I'm open to any solutions that do not involve styling or C ...

Grid collapse causes a breakdown in the grid structure

Currently, I am in the process of learning how to build a website using Material-ui and React. My goal is to implement a collapsible grid where certain rows can be expanded or collapsed based on the page's state. However, I have encountered an issue ...