What causes the disappearance of CSS styles when attempting to modify the className in react js?

I am currently working on a basic react application, and I am trying to dynamically change the name of a div element using the following code snippet -

<div className={'changeTab ' + (page.login ? 'leftSide' : 'rightSide')} id="tab" ></div>

However, I'm facing an issue where the CSS styling associated with it disappears. Interestingly, the rest of the page seems to be functioning properly, ruling out any linking problems with the CSS file. Strangely enough, when I only utilize the ID attribute instead of the class names, the styles work perfectly fine. The problem arises only when I use class names in the CSS, as the styles vanish completely, not even appearing in the developer tools when inspected. Although the class names are visible within the HTML section of the developer tools, they do not reflect the CSS properties.

To implement this conditional logic, I am utilizing a simple useState Hook as shown below -

    const [page, setPage] = useState([{
    login: true,
    signUp: false
  }])

Below is the corresponding CSS code snippet -

 #tab .changeTab .leftSide{
  position: absolute;
  height: 100%;
  width: 50%;
  left: 0;
  background: -webkit-linear-gradient(right,#4361ee,#4cc9f0);
  transition: all 0.6s cubic-bezier(0, 1.17, 0, 0.69);
  border-radius: 5px;
}

#tab .changeTab .rightSide{
  position: absolute;
  height: 100%;
  width: 50%;
  left: 0;
  background: red;
  margin-left: -50%;
  transition: all 0.6s cubic-bezier(0, 1.17, 0, 0.69);
  border-radius: 5px;
}

Being relatively new to react, I would greatly appreciate any guidance or assistance regarding this matter.

Answer №1

It appears that there may be a flaw in the logic of your CSS specification.

From what I understand, you are attempting to apply styling to an element that possesses both the id tab and the classes changeTab, as well as either the class leftSide or rightSide.

The current setup instructs the CSS to search for nested classes within the parent id tab.

This means that the CSS is seeking a child of the id tab with the class changeTab, followed by another child of that element with the class leftSide or rightSide.

To correct this, consider updating your CSS as follows:

#tab.changeTab.leftSide{
  position: absolute;
  height: 100%;
  width: 50%;
  left: 0;
  background: -webkit-linear-gradient(right,#4361ee,#4cc9f0);
  transition: all 0.6s cubic-bezier(0, 1.17, 0, 0.69);
  border-radius: 5px;
}

#tab.changeTab.rightSide{
  position: absolute;
  height: 100%;
  width: 50%;
  left: 0;
  background: red;
  margin-left: -50%;
  transition: all 0.6s cubic-bezier(0, 1.17, 0, 0.69);
  border-radius: 5px;
}

Note the removal of spacing in the CSS rules, specifying that the element must have the id tab, along with the classes changeTab and either leftSide or rightSide.

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

Having trouble utilizing Reactjs Pagination to navigate through the data

I'm currently working on implementing pagination for a list of 50 records, but I'm encountering an issue. Even though I have the code below, it only displays 10 records and I'm unaware of how to show the next set of 10 records until all 50 a ...

Unsuccessful conversion of JSON data to HTML using json2html in Python

I have been experimenting with converting JSON data to HTML using json2html. The JSON data structure I am working with is as follows: json_obj = [{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username" ...

One package in Node.js, a pair of dependencies, and a single export utilizing ES6 syntax

Currently, the library contains multiple frameworks in one npm module (which is admittedly not the best practice). However, as we work on splitting the library into separate JS framework specific repositories like React, JQuery, Angular, etc., I'm fac ...

Utilizing React JS to Activate the Glyphicon Calendar Icon on Click

Could someone please advise on how to make the calendar glyphicon activate the datetime picker upon clicking? I have a button currently but it is not functional. I've been searching for React-specific solutions without success. <div className={cla ...

Issue with justify-content in Bootstrap 5 still unresolved

Currently working with Bootstrap 5 and have set up 4 cards. My goal is to position the second card on the left side. I've applied the class justify-content-xl-start to the card, however, it doesn't seem to be functioning as expected. I also attem ...

File naming conventions in React: using JSX extensions

Having recently started working with React, I began my journey using create-react-app. The default file names were index.js, so I decided to change the file extension to index.jsx. However, upon attempting to launch the application with npm start, I encou ...

What are the steps to ensure that this iframe adjusts perfectly to the page in terms of both vertical and horizontal dimensions

I have a sandbox from Material UI that you can view at this link: https://codesandbox.io/s/material-demo-forked-c8e39?file=/demo.js Upon loading the page, an iframe displays some HTML content. Although the width fits perfectly, there are two vertical scro ...

What is the most effective strategy for managing dependencies for npm packages?

I am currently working on extracting a few Vue.js components from the main application and converting them into an npm package stored in a repository. This package will be imported and utilized across two different websites. To bundle everything, I am util ...

What advantages does the use of $(e).attr(name,value) offer compared to using e.setAttribute(name,value)?

Scenario: The variable "e" represents an element of type "HtmlElement" and not a "css selector" I am referring to any attribute, not just the standard allowed ones like "atom-type" or "data-atom-type". Regardless of the attribute name, will it function wi ...

How can I use AngularJS to show a JSON value in an HTML input without any modifications?

$scope.categories = [ { "advertiser_id": "2", "tier_id": 1, "tier_name": "1", "base_cpm_price": "", "retarget_cpm": "", "gender": "", "location": "", "ageblock1": "", "ageblock2": "", "ageblock3": ...

Adding Multiple CSS Classes to an HTML Element using jQuery's addClass Feature

Take a look at this HTML code snippet: <html> <head> <style type="text/css"> tr.Class1 { background-color: Blue; } .Class2 { background-color: Red; } </style> </head> <body> < ...

What is the method for determining the location of the VR headset in a three.js environment?

Can anyone help me understand how to obtain the VR headset's position using three.js? I have successfully created a scene and am receiving live controller positions, but I am unsure on how to access the headset's position to track the user's ...

Upon setting up Material UI, I encounter a range of errors during installation

Encountering errors during the installation process of Material UI using npm (npm install @material-ui/core) is not uncommon: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cg ...

Utilize an array value as a parameter for getStaticProps within Next.js

Currently, I am fetching my Youtube playlist using a fetch request and utilizing getStaticProps(). However, I am encountering an issue where my playlist is dependent on the result of an array of objects. export async function getStaticProps(){ const MY_P ...

Discovering the size of content through solely the height and width of a div: Is it possible?

Can someone help me figure out how to calculate the content length based solely on the height and width of a div? For example, if a Div has a height:200px & width:200px, I would like to know that the stored content length is 298. function calculateCo ...

Next.js presents a unique problem with double rendering that developers must be

My current issue involves double rendering in my Next.js application. Running next 13, whenever I use next dev, the page is always rendered twice: // The project is simply the result of creating a `create-next-app` // pages/index.js import { useEffect, use ...

Creating unique Mongoose Object IDs in the Next.js frontend

I am having trouble generating Mongoose compliant ObjectId's on a NextJs frontend. The issue arises when attempting to import mongoose and using the standard const ObjectId = mongoose.Types.ObjectId; followed by const _id = new ObjectId();, which resu ...

Python form submission error code 405 encountered

I recently developed a simple WSGI Server using Google app engine. Here's an example of the code: import webapp2 class MainHandler(webapp2.RequestHandler): def get(self): self.response.write(''' ...

Is there a way to programmatically create multiple MUI Select components with their corresponding options?

Through processing existing data, I am dynamically generating multiple MUI "<Select>" elements. However, when interacting with one of the "<Select>" elements, the selected value is being applied to all other "<Select>" elements. For inst ...

What is the best way to convert my MySQL data into JSON in a specific format?

I need help with outputting MySQL data into various formats below: timelist, usersex, and userage from the users table. <script type="text/javascript"> timeList = new Array(), userSex = new Array('female','male','male') ...