When a React CSS class is deployed to a production server, it may be automatically

I've encountered a strange CSS issue while working on my React project. One specific section of the JSX <div> has a class with style properties defined in the main .css file. During local development, everything appears as expected. However, after building and uploading to the production server, the styling of that particular section changes and becomes distorted.

For example:

Original JSX

// Code snippet

Original DOM:

// Example DOM

The CSS for this JSX is:

// Styling code for diabMetr component

The DOM changes after build and uploaded to the server:

// Changed DOM structure

The CSS for the class .jss16 is:

.jss16 {
    width: 450px;
}

Issue to notice Description of the observed issue.

Attempted solutions:

// Tried CSS modifications

Facing challenges:

// Continued issues despite attempted fixes

Seeking assistance:

// Request for help

Answer №1

There are numerous issues with the code provided. Firstly, in the jsx, the CSS class is incorrectly labeled as className, as pointed out by @Max in their response.
Another problem arises from how @material-ui's makeStyles function operates. The classNames generated by makeStyles get transformed into random names during the build process to maintain uniqueness. This behavior is a feature of @material-ui. I recommend referring to the documentation on makeStyles for more clarification. Additionally, you can find a code example here.

To effectively use the makeStyles classes, they need to be integrated into your component.

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles({
  root: {
    backgroundColor: 'red',
    color: props => props.color,
  },
});

export default function MyComponent(props) {
  const classes = useStyles(props);
  return (
    <div className={classes.root}>
      Lorem ipsum dolor
    </div>
  );
}

Update

In line with your jsx code, incorporate the styles from the CSS class .makeStyles-root-1 within the useStyles object. This will apply the specified styles to the respective element.
Upon addition of these CSS styles to the useStyles object, it should resemble the following:-


const useStyles = makeStyles((theme) => ({
  root: {
    width: 'calc(100% - 220px) !important',
    float: 'right',
    marginTop: '-22px'

  },
  margin: {
    height: 100,
  },
}));

The root class will now include these styles and automatically apply them without the need for separate styling from an external CSS file.

Answer №2

Solution

If you come across a div with the class name "jss16" and are unsure about which component generates it, let's assume it is produced by ExternalComponent.

To customize this div, consider adding a custom className (assuming ExternalComponent handles this properly):

<ExternalComponent className="myclass">
   ...
</ExternalComponent>

This will result in the following DOM structure:

<div class="jss16 myclass">
    ...
</div>

You can then style the myclass using CSS as follows:

.myclass {
  width: calc(100% - 220px) !important;
  float: right;
  margin-top: -22px;
}

Explanation

Since ExternalComponent utilizes jss to dynamically create CSS classes, you cannot always predict the name of the generated class. In scenarios where components have custom classes, they should concatenate props.className to the generated jssClassname like this:

return (
    <div className={jssClassname + props.className ? ' ' + props.className : ''}>
        {children}
    </div>
);

Answer №3

Due to some syntax issues, I was unable to reproduce the error. However, by addressing the following fixes, the build should function correctly:

  • Ensure closing / tag for input
  • Use objects for styling, like
    style={{left: '0%', width: '83.3333%'}}
  • Change class to className
  • Change tabindex to tabIndex

If those adjustments do not resolve the issue, consider modifying your CSS selector from .diabMetr + span + div to:

.diabMetr > span + div {}

or

.diabMetr > div {}

Your current selector is not targeting the desired child element.

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

I am a beginner in the world of Typescript/Angular, and I have encountered a compiling error TS2339. It seems that even when the condition *ngIf="x.length > 0;" should be false, the

I'm currently enrolled in a Typescript/Angular course where I am learning about the implementation of "*ngIf". During one of the lessons, the instructor provided an example using an empty array to demonstrate how it fails the condition and results in ...

Display or conceal form elements depending on the JSON response

Upon making an api call, a json Response is received with the following structure: {"Field":"Amount","FieldName":"Amount","FieldType":"Numeric","MaximumLength":128,"MinimumLength":0,"Options":"Mandatory"} This api call yields between 5-10 of these object ...

Guaranteeing the sequential execution of JavaScript functions

For weeks, I've been struggling with a program that utilizes ajax post methods and dataTables. It has become clear to me that my understanding of how javascript operates is lacking. Below is the javascript code: $('#SaveTimeSheet').click(fu ...

Incorporating URL addresses and pagination features using React.Js and Material-UI components

I have a functional component-page on my website, where I display a table that fetches data from an API. To improve user experience, I implemented pagination using the Material-UI library. While the pagination functionality works fine in updating the table ...

Filter an array of objects based on a provided array

I have an array of objects that I need to filter based on their statuses. const data = [ { id:1, name:"data1", status: { open:1, closed:1, hold:0, block:1 } }, { id:2, name:"data2", ...

Determine whether the Bootstrap data-style attribute is enabled or disabled

I have a bootstrap checkbox that I would like to trigger an alert dialog when it is switched off. <div class="row"> <link href="css/bootstrap-toggle.min.css" rel="stylesheet"> <script src="javascript/bootstrap-toggle.min.js ...

Can Javascript be used to obtain someone's UDID?

Is it feasible to retrieve individuals' UDIDs when they visit your website? If this is achievable, could you recommend a helpful tutorial for me to follow? ...

keen-slider may encounter compatibility issues with next.js

I have been attempting to transform the code shown in the screenshot below into a slider format using keen-slider. https://i.stack.imgur.com/M62iz.png <div className="card-con mt-6"> {!loadingImages && ...

Does embedding an Iframe for external files from your server enhance the loading speed of the current page compared to directly loading it on the page?

I'm facing an issue with the loading time of a Facebook post on my webpage index.php. The current method of using the embedded post provided by Facebook is taking too long to load. My solution is to create a separate page, post.php, where only the Fac ...

Unordered list in Bootstrap Collapse not responding after initial click

I recently created a filetree page on my website and incorporated some bootstrap collapse elements that seem to function as intended. However, I am encountering an issue where the folder content does not collapse upon clicking the folder symbol (button) fo ...

New to JavaScript and Bootstrap - eager to learn by using Bootstrap 4 Chart Template, just need help with a small issue in the code

I am eager to dive into Bootstrap 4 and data visualization charts. I want to replicate the visualizations found in the link below. However, after copying and pasting the code into Atom, it's not functioning as expected. I ensured that I copied the HT ...

Challenges with using async await alongside synchronous functions

I'm currently navigating through a library that utilizes async functions and feeling a bit overwhelmed. I'm attempting to call a function that should return a string, but I'm hitting some roadblocks. As I understand it, the ZeroEx library fu ...

Managing simultaneous access to a variable in NodeJS: Best practices

For instance: var i = 0; while(true) http.request('a url', callback_f); function **callback_f**(){ **i++**; } In this straightforward scenario, multiple requests could unintentionally increase the value of i simultaneously. How can I creat ...

Custom AngularJS directive: dynamic template rendering using scope value (ng-bind-html)

I am currently working on a directive which looks like this: ... template: function(element, attrs) { var htmlTemplate = '<div class="start-it" ng-if="isVisible">\ <p ng-bind-html="\'{{customDynamicText}}\&a ...

Transforming PHP shortcode into JQuery functionality

My website is built on Wordpress, and I use javascript to load some of the content. Here's an example: jQuery(".portfolio-fs-slides").css({"display":"none"}).prepend('<div class="portfolio-fs-slide current-slide portfolio-ppreview"><d ...

Revamp the angular design of the mat-tree UI bottom border line issue

Can you help me with changing the direction of the mat tree from right to left? I need to remove the bottom border, please refer to the image here ...

Displaying the action.type in Components with React and Redux: A comprehensive guide

I am looking for a way to display the status of the action.type in my component when it is triggered. This will help me show whether an action has been successfully passed or not (when catch is triggered). Do you have any suggestions on how I can achieve ...

Clearing a value with active getOptionSelected in Material-UI Autocomplete

I am using a Material-UI Autocomplete component and here is the code snippet: <Autocomplete onFocus={() => setFocusedProject(true)} onBlur={() => setFocusedProject(false)} ...

Enhancing the Search Bar in Bootstrap 4 Dashboard Template: A Step-by-Step Guide

Recently got my hands on the Bootstrap 4 Dashboard template and decided to enhance it by adding a form to the top search bar. However, I encountered an issue where the search bar shrinks and loses its original design. If you want to check out the Bootstra ...

Encountered a problem while attempting to remove node version

When I run the command nvm ls -> v4.3.2 system default -> 4.3.2 (-> v4.3.2) node -> stable (-> v4.3.2) (default) stable -> 4.3 (-> v4.3.2) (default) iojs -> N/A (default) Upon running nodejs --version, it returns v0 ...