CSS ID selectors are not functioning properly

In my React.JS project, I am working with a div that contains a button and a list. The list is specifically identified by the id "results".

return <div>
    <Button label="Combine Cards" disabled={!this.props.combineReady} onClick={this.handleClick} accent primary raised />
     <br />
     <ul > 
        { JSON.parse(this.state.data).resultCards.map(function(card){
            return <li id="results">{card.deviation} <img src={'https://image.deckbrew.com/mtg/multiverseid/123456.jpg'}/></li>;                     
        }) }
    </ul>
</div>;

The styles.css file I am dealing with has the following structure:

/* The list container. */
ul{
    list-style: none;
    display: inline-block;
    width: 263px;
    text-align: left;
}

/* The individual list items. */
ul li{
    display: block;
    padding: 15px 20px;
    background-color: #F8F8F8;
    color: #7B8585;
    margin-bottom: 3px;
    position: relative;
    
    transition: 0.3s;
}

/* The card images within the list items. */
ul li img{
    transform: scale(0.6, 0.6);
    -ms-transform: scale(0.6, 0.6);
    -webkit-transform: scale(0.6, 0.6);
    align: top;
    padding: 3px;
    transition: 0.3s;
}

/* Images when hovered over. */
ul li img:hover{
    transform: scale(1, 1);
    -ms-transform: scale(1, 1);
    -webkit-transform: scale(1, 1);
}

ul li a{
    position: absolute;
    left: 160px;
    font-size: 12px;
    line-height: 16px;
    color: #969d9d;
}

/* List items when hovered over. */
ul li:hover{
    background-color:#d8f2f1;
}

li#results{
    display: inline !important; 
    background-color: #7B8585 !important;
    color: #F8F8F8 !important;
    height: 200px !important;
    overflow: visible !important;
}

There seems to be an issue with the styling of the list marked with id "results". Various attempts to target and modify the styling, including using li#results, ul li#results, and ul#results li, have not been successful. Any insights on how to correct this would be greatly appreciated.

(Note: There are two other lists in the project that need to maintain their separate styling.)

Below is the resulting HTML structure:

<div data-reactid=".0.0.0.1.0">
  <button class="style__raised___3-PWA style__primary___zmQdT" data-react-toolbox="button" data-reactid=".0.0.0.1.0.0">
    <span data-reactid=".0.0.0.1.0.0.1">Combine Cards</span>
      ... (remaining HTML content stripped for brevity)

The issue persists with different approaches like using <ul id="results"/> or <li id="results"/>. Strangely, with <li id="results"/>, the id seems to apply to all line elements, while using <ul class="results"/> results in no visible id or class tags at all.

Answer №1

I came to the realization that the id within the module automatically becomes a unique id, similar to classes. Feel free to utilize this feature!

// file: ./style.module.css
#green {
  color: green;
}

// file: main.jsx
import style from './style.module.css';

<div id={style.Green}>
  Text green
</div>

Despite WebStorm's linter indicating "Unresolved variable," everything is functioning correctly.

Answer №2

React Toolbox utilizes CSSModules to style its components and here is the recommended approach for using them effectively.

/* style.css */
.result {
  color: green;
}

When you import the CSS Module from a JS Module, it will export an object containing all mappings from local names to global names.

import styles from "./style.css";

...
<li className={styles.result}>{card.deviation}</li>

Answer №3

The reference to your ID #results in the css is targeting a li element with that specific ID:

li#results{
  display: inline !important; 
  background-color: #7B8585 !important;
  color: #F8F8F8 !important;
  height: 200px !important;
  overflow: visible !important;
}

Your ID results is contained within a ul tag.

<ul data-reactid=".0.0.0.1.0.2" id="results">
    <li data-reactid=".0.0.0.1.0.2.0">
      <span data-reactid=".0.0.0.1.0.2.0.0">0.8743035007251114</span>
      <span data-reactid=".0.0.0.1.0.2.0.1"> </span>
      <img src="https://image.deckbrew.com/mtg/multiverseid/126204.jpg" data-reactid=".0.0.0.1.0.2.0.2">
    </li>
    <li data-reactid=".0.0.0.1.0.2.1">
      <span data-reactid=".0.0.0.1.0.2.1.0">0.8663643850889716</span>
      <span data-reactid=".0.0.0.1.0.2.1.1"></span>
      <img src="https://image.deckbrew.com/mtg/multiverseid/373708.jpg" data-reactid=".0.0.0.1.0.2.1.2">
      ...8 more lines.
    </li>
</ul>

To resolve the issue, simply adjust your CSS selector to ul#results.

Answer №4

As outlined in this discussion on the CSS Modules GitHub repository, there are a couple of options available:

  1. To make it global in the .css file, use the syntax below:

    :global(#results){ /* Your css rules here */}
    

It should be noted, however, that this approach contradicts the localization purpose CSS modules are designed for.

  1. Alternatively, in your JSX/TSX file, utilize the id obtained from the styles object generated by CSS Modules, like so:

    import styles from "./styles.css";
    
    return (<li id={styles.results}></li>)
    

Be mindful that the ID will not be result anymore but a dynamically generated one, so handle it appropriately.

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

Adding a Close Button to a MaterializeCSS Card or Card-Panel

Hey everyone! I'm looking for guidance on how to add a close button (x) to a card or card-panel in MaterializeCSS. While Bootstrap offers an Alert component, unfortunately MaterializeCSS does not have one readily available. Does anyone know of a wor ...

Modify the value of a variable inside another {{variable}} (not sure what it's called)

I am looking to update the variable "prefs" to reflect either "easy, medium, or hard" options based on user interaction. For instance: THIS {{choice.prefs.title}} NEEDS TO BE UPDATED TO {{choice.easy.price}} or {{choice.medium.price}} or {{choice.hard. ...

Is the presence of the selenium webdriver being registered?

Is there a method to detect if a website can tell I am using a bot while running Selenium WebDriver in Python or Puppeteer in JavaScript? Are there any resources that indicate whether a bot test would be failed, such as Cloudflare or Captcha? Appreciate a ...

Obtain HTML tags from RSS CDATA section

Is there a way to extract HTML tags from a CDATA tag in an RSS feed? I have utilized a basic jQuery function to retrieve the JSON object from the RSS, below is my code: $.get("someURL", function(data) { console.log('InGet'); ...

"Enhancing web design with jQuery mousemove to dynamically adjust CSS filters according to mouse location

I'm attempting to create a simple mousemove event that changes the CSS filter property of my background div based on the position of the mouse. While I have successfully achieved this in the past with background-color, I am now encountering issues wit ...

Using Sails.js to display JSON data retrieved from an HTTPS request in the view

Just getting the hang of Sails.js, so any help is appreciated. I've used an XML service and successfully converted it to JSON using xml2js var req = https.request(options, function(res) { var xml = ''; res.on('data', fun ...

Text affected by mix-blend-mode glitch

Currently, I am experimenting with knockout text using the mix-blend-mode property. An interesting issue arises when I examine the responsiveness of my design by utilizing Developer tools in the browser console - there are faint lines that momentarily appe ...

Getting stuck in an endless loop while making a call to Axios for data fetch with React Suspense

I am currently working on fetching data using Axios and displaying it within a suspense element. I came across a tutorial that demonstrates this process: https://dev.to/darkmavis1980/a-practical-example-of-suspense-in-react-18-3lln However, I am encounter ...

Node.js and Express throwing errors when trying to access a certain endpoint with HTTPS and passing parameters

I am experiencing issues with my node.js express webserver that operates on both HTTP and HTTPS, specifically related to express's route parameters and HTTPS. Express allows for parameters in the routing, such as: app.get('/users/:userid', ...

Create a horizontal scrolling div with a fixed position

Even though this topic has been discussed numerous times, I have not found any solutions that work for me. My webpage has a sidebar on the left with position:fixed in CSS, and I want it to scroll horizontally along with the rest of the content. For the he ...

What is the correct method for completely eliminating a mesh from the three.js scene?

I am looking for a way to fully remove meshes from a three.js scene without causing any memory leaks. I have noticed that reloading the same models multiple times can lead to browser crashes, indicating that memory is not being properly deallocated. ...

What methods can be used to ensure that the variable $ can serve as both an object and a function?

One interesting feature of jQuery is how the variable $ can function as both an object and a function. // For example, you can access object properties like $.fn; // or $.isReady; // You can also call the function $ like this $(); So, how can we make the ...

Place the background image of the parent element relative to the child element

I am struggling to position the background image of <body> relative to one specific child <div>. <body> <div>content</div> <-- place background image of body relative to this div <div>other content</ ...

Creating a grid layout similar to Tumblr using HTML and CSS

Struggling all day to figure out how to create a Tumblr-style grid for my website, I'm reaching out here for some assistance. Here is the page: I have a grid of images on my project page that initially looked fine, but as I try to add new elements, i ...

Is there a way to eliminate the additional and varying pseudo-padding on text inputs in both Webkit and Gecko browsers?

The issue I'm facing is specific to input[type="text"] elements in Webkit. No matter what adjustments I make, it seems like there is an additional padding: 1px 0 0 1px (top and left only) applied to the element. A similar anomaly occurs in Gecko as w ...

Tips on how to show the image icon in place of the image name

Here is a code snippet for multiuploading images using Vue. Currently, the uploaded images are displayed by their file names. However, I am looking to enhance this functionality by displaying an image icon instead of the file name. If anyone knows how to ...

What steps are needed to build a Nested Default Dictionary in JavaScript?

I've been working on creating an Apps Script and have run into a challenge with implementing a Default Dictionary. Initially, I tried using the following code snippet: class DefaultDict { constructor(defaultInit) { return new Proxy({}, { g ...

Access to the XMLHttpRequest from the origin http://localhost:3000 has been restricted by the Cross-Origin Resource Sharing (CORS)

I've successfully set up both the front and backend for an app I am currently developing. Now, I need to test the HTTP requests being sent from the front end to the backend while they are running on localhost. Here's the code snippet for sending ...

Developing with Express and React

Currently, I am running experiments with React and Express. My primary objective is to establish a connection between Express and a MySQL database to retrieve data. I have a good understanding of how both React and Express operate, and after several attem ...

What are the different approaches for creating a website that adapts to different screen sizes and devices

The popularity of responsive web design is growing rapidly. While many recognize Twitter Bootstrap as a top framework, there are other options worth exploring. Several impressive examples of responsive websites, such as Smashing Magazine and CSS Tricks, ...