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

Encountering an undefined variable in the .env file

Once the .env file was created in the main React folder REACT_APP_API_KEY=gzomlK5CKLiaIWS.... I also installed the .env NPM library Despite this, I continued to receive undefined in the API file. What could be causing this issue? import React, { useState ...

Add a hyperlink within a button element

I am looking to add a route to my reusable 'button' component in order to navigate to another page. I attempted using the <Link> </Link> tags, but it affected my CSS and caused the 'button' to appear small. The link works if ...

What was the reason behind resolving the 0 height body problem by adjusting the WebView Layout Parameter?

My Xamarin WebView was not displaying my HTML correctly. Even though it was set to fill the whole screen, the body height in the HTML remained at 0px. In addition, I had a div in the HTML with the following style: position:absolute; top:0px; bottom:0px; ...

What is the best way to access all sections of a JSON file containing nested objects within objects?

Here is an example of my JSON file structure: [{ "articles": [ { "1": { "sections": [ {"1": "Lots of stuff here."} ] } }, { "2": { "sections": [ {"1": "And some more text right here"} ] } } }] The c ...

What is the best way to reload a React/TypeScript page after submitting a POST request?

I am working on a custom plugin for Backstage that interacts with Argo CD via API calls. To retrieve application information, I make a GET request to the following endpoint: https://argocd.acme.com/api/v1/applications/${app-name} If the synchronizati ...

designing a modular socket.io framework in node.js

Suppose in a node.js application, we have an app.js file structured like this: var express = require('express') var app = express(); var server = http.createServer(app); ... module.exports = { app:app, server:server } Additionally, there ...

Maintaining Styles After Focus is Removed in CSS: A Guide

The CSS that styles our buttons is as follows: .btn-outline-primary { color: blue; border: 1px solid blue; background: transparent; transition: all 0.3s ease 0s; } .btn-outline-primary:hover, .btn-outline-primary:focus { background: ...

Ways to center text vertically within a cell in a Bootstrap 5 table

I am trying to achieve vertical alignment in one of the cells of a table I created. According to the Bootstrap 5 documentation, the vertical-alignment utilities only affect certain elements like inline, inline-block, inline-table, and table cell elements. ...

How can I determine if my clients are utilizing the CDN or NPM versions of my JavaScript library?

At this moment, I'm contemplating releasing an open-source version of my library on NPM. My main concern is figuring out how to track the usage of my CDN or NPM by clients. Is there a method available to achieve this? ...

Error encountered in Chrome while trying to fetch Next.js PWA with the use of next-pwa: Unhandled TypeError

Hello, I was recently working on a Next.js project and attempted to convert it into a PWA using next-pwa. To start off, I created the next.config.js file. const withPWA = require('next- pwa'); module.exports = withPWA({ pwa: { dest: ...

How can I generate a list of JavaScript files to be included in a template for both production and development environments using Grunt?

I need a way to organize a list of JavaScript files in one central location, either within gruntfile.js or an external JSON file, and then dynamically implement it into a template for both development and production environments. List of JavaScript files: ...

In JavaScript, is it possible to dynamically alter and showcase the value of a select tag?

My code snippet in the HTML file contains Javascript: <script> $(document).ready(function(){ $("#sub").click(function(){ var user_issue = $("#issue").val(); ...

Understanding the React Profiler: Deciphering the timing measurements

I have been utilizing the React profiler tool to optimize my application's performance. It often presents me with a graph like the one shown below: I find myself puzzled by the timings indicated on the graph. The numbers seem inconsistent, leading to ...

Unable to transfer props object to React component using TypeScript

Apologies if this seems like a basic issue, but I've been struggling with it for the past two days. I'm currently working on writing a simple unit test in Vitest that involves rendering a component to the screen and then using screen.debug(). The ...

How can we sort an array based on the inner HTML values of its children elements in JavaScript?

Can you arrange a JavaScript array based on the innerText values of its HTML elements? I am generating a div element (class="inbox" id="inbox${var}") with a number as its innerText, then adding all these divs to an array (numArr). I wan ...

Issues with keyboard accessibility in drop down menus

Looking to improve the keyboard accessibility of my menu bar. Swapped out all instances of :hover with :focus, but unfortunately dropdown menus are still not accessible via keyboard. Does anyone have a solution for this issue? Appreciate any help! ...

Utilizing a (helper) function within Redux

I am currently using react-native-router-flux along with react-redux and I believe this is the right place to ask my question. Please correct me if I'm mistaken. Within my application, I have an ActivityModal Container which I use to display a modal ...

Where can I locate htmlWebpackPlugin.options.title in a Vue CLI 3 project or how can I configure it?

After creating my webpage using vue cli 3, I decided to add a title. Upon examining the public/index.html file, I discovered the code snippet <title><%= htmlWebpackPlugin.options.title %></title>. Can you guide me on how to change and cu ...

Organizing grid elements within the popper component

I am struggling to align the labels of options in the AutoComplete component with their respective column headers in the popper component: https://i.stack.imgur.com/0VMLe.png const CustomPopper = function (props: PopperProps) { co ...

Consolidate HTML project into a unified HTML file

In my current project, I am working with a static HTML report that is organized in the structure outlined below: 1.css 2.font 3.js 4.pages 5.attachments 6.index.html I would like to know how I can combine all these elements to create a unified 'repor ...