Issues with hover effects not applying to background colors (ReactJS)

React Component:

import React from "react"

const Category = () => {
    return (
        <table cellSpacing={25}>
            <tr>
                <td id="fruits">Fruits & Vegetables</td>
                <td id="pc"> Personal Care</td>
                <td id="dairy">Dairy Products</td>
            </tr>
            <tr>
                <td id="stap">Staples</td>
                <td id="snack">Snacks</td>
                <td id="stat">Stationery</td>
            </tr>
            <tr>
                <td id="bake">Bakery</td>
                <td id="home">Home Care</td>
                <td id="bev">Beverages</td>
            </tr>
        </table>
    );
}

export default Category; 

CSS


#fruits{
  background-image: url("C:\Users\thish\OneDrive\Desktop\travel_advisor\src\image_src\fruits_blur.png") ;
}
#pc{
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\personal_care_blur.webp");
}
#dairy{
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\dairy.PNG");
}
#stap{
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\stap.PNG");
}
#snack{
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\snack.PNG");
}
#stat{
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\stat.PNG");
}
#bake{
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\bake.PNG");
}
#home{
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\home.PNG");
}
#bev{
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\bev.PNG");
} 

td:hover{
cursor: pointer;
background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\71AC0A.png");
text-decoration: underline;
}

The issue at hand is when hovering over the cells, the background image should change to a different color rather than changing it. This could be due to the pre-existing background images assigned to those table cells. The other styles seem to work correctly except for the background-color change upon hover. Attempts were made with both background-color and background-image properties. Any insights on resolving this behavior?

Answer №1

Don't forget to include !important in the hover style:

td:hover {
  cursor: pointer;
  background-image: url("C:\Users\OneDrive\Desktop\travel_advisor\src\image_src\71AC0A.png") !important;
  text-decoration: underline;
}

Here is a demonstration of this in action:

const Category = () => {
  return (
    <table cellSpacing={25}>
      <tr>
        <td id="fruits">Fruits & Vegetables</td>
        <td id="pc"> Personal Care</td>
        <td id="dairy">Dairy Products</td>
      </tr>
      <tr>
        <td id="stap">Staples</td>
        <td id="snack">Snacks</td>
        <td id="stat">Stationery</td>
      </tr>
      <tr>
        <td id="bake">Bakery</td>
        <td id="home">Home Care</td>
        <td id="bev">Beverages</td>
      </tr>
    </table>
  );
};

ReactDOM.render(<Category />, document.querySelector('.react'));
#fruits {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}
#pc {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}
#dairy {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}
#stap {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}
#snack {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}
#stat {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}
#bake {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}
#home {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}
#bev {
  background-image: url("https://asia.olympus-imaging.com/content/000107507.jpg");
}

td:hover {
  cursor: pointer;
  background-image: url("https://burst.shopifycdn.com/photos/city-lights-through-rain-window.jpg?width=1200&format=pjpg&exif=1&iptc=1") !important;
  text-decoration: underline;
  color: white;
}
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

Answer №2

td:hover {
  background-color: blue !important;
}
<table cellSpacing={25}>
  <tr>
    <td id="fruits">Fruits & Vegetables</td>
    <td id="pc"> Personal Care</td>
    <td id="dairy">Dairy Products</td>
  </tr>
  <tr>
    <td id="stap">Staples</td>
    <td id="snack">Snacks</td>
    <td id="stat">Stationery</td>
  </tr>
  <td id="bake">Bakery</td>
    <td id="home">Home Care</td>
    <td id="bev">Beverages</td>
  </tr>
</table>

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

One can easily include CSS content within the div element

Below is the CSS code used to create counters: #lctndrp li:before{ content: counters(item, ".") " "; counter-increment: item; } The HTML code is as follows: <ol> <li> ::before one <ol> <li> ::be ...

Using window.open in Google Chrome results in multiple tabs being opened instead of just one

I have a dynamic input field with a search button. Initially, only one tab is opened when I click the button. But after saving and clicking it again, multiple tabs open for the same URL. The number of tabs is equal to the number of dynamic input fields cre ...

Interested in incorporating href within a PHP variable?

My goal is to send a verification email without displaying the link in the email itself. Instead, I want to include text that says "click here to verify." { $verify_email=" Hello, Thank you for signing up. To verify your e-mail, please visit http://".$sit ...

Ways to exclusively display map items matching those in the array

Hey everyone, I'm dealing with a map that looks like this: Map { '708335088638754946' => 38772, '712747381346795670' => 12051, '712747409108762694' => 12792 } Alongside this map, I also have an array: let arr ...

What advantages could learning ReactJS first give me before diving into NextJS?

Just mastered TS and now faced with the decision of choosing a framework. I'm curious why it's recommended to learn ReactJS before NextJS. I've read countless articles advising this, but no one seems to delve into the reasons behind it. Ca ...

Ways to merge two arrays into one in React JS

Here are two arrays presented below: const arrayA = { 0: { id: XXXXXXX, name: "test" }, 1: { id: YYYYYYY, name: "example" } } const arrayB = { 0: { id: XXXXXXX, category: "sea", } 1: { id: YYYYY ...

I'm curious, are there any html rendering engines that can display text-based content using curl-php?

When utilizing PHP cURL to interact with webpages, I often find myself needing to use regular expressions if the page contains AJAX and JavaScript elements. Does anyone have any recommendations for rendering HTML pages and extracting the text-based render ...

Open the HTML page from a separate directory

I'm facing an issue with loading additional HTML onto a page in my project when a link is clicked. The HTML fragment file I want to load is stored in a different folder within the project structure. Despite my efforts, I keep encountering a 404 error ...

Using the Composition API in Vue 3: Guide to invoking a method within the template to retrieve a return value

Within my template, I have implemented the following code snippet: <template> {{ getScope(scope.row, itemIn.field) }} </template> For the Option API section, I've included: methods: { getScope(data, key) { const str ...

Tips for looping through a JSON object?

Similar Question: How to extract a specific value from a nested JSON data structure? I am looking to loop through a two-dimensional JSON object, whereas I already know how to do so for a one-dimensional JSON object. for (var key in data) { alert(data ...

The function called Nuxt: n2 is not defined

When using Nuxt 3, I encountered a TypeError that looks like the following: Uncaught TypeError: n2 is not a function My issue revolves around a button that triggers the function toggleSelectRow with a @click.prevent directive. The function in question is ...

Changing the color of specific sprites in three.js

I'm currently exploring three.js and encountering some difficulties when attempting to adjust the color of individual sprites in an array. My reference point is the example provided on this link from threejs.org. The specific task I am working on in ...

Thick black border on select list in Internet Explorer version 10

After recently switching to IE10, I couldn't help but notice that all my dropdown lists (select lists) now have a bold black border when in the "dropped down" state, like the example below. Unfortunately, I am unable to inspect the element using IE&ap ...

Using ajax to send an array to PHP

I have an array named "heart" that is being sent via ajax... var heart = [31,32,33,34,35,36,37,38,39,42,43]; // Sending this data via ajax to php file/ $.ajax({ type: "POST", data:{ 'system': heart }, url: "login-function.php", success: f ...

Altering iframe Content with the Help of Selenium Web Driver

I am looking to update the content of an iframe with new material using Selenium WebDriver. Note: I have attempted the following method: driver.swithTo().frame(frame_webelement); driver.findElement(By.xxx).sendKeys("Mycontent"); While I was able to cle ...

Creating a sleek horizontal scrolling list with the least amount of rows using tailwindcss

Is there a way to create a 3-row list with horizontal scroll without the unwanted space between items? I've been using tailwindcss and grid to achieve this layout, but I'm encountering issues with spacing: https://i.stack.imgur.com/WeRyQ.png Cu ...

Switching between vertical and horizontal div layouts while reorganizing text fields within the top div

function toggleDivs() { var container = document.querySelector(".container"); var top = document.querySelector(".top"); var bottom = document.querySelector(".bottom"); if (container.style.flexDirection === "column") { container.style.flexDirec ...

Using AsyncStorage as a dependency in React Native to trigger the useEffect hook

I'm currently developing a react native authentication screen and I've encountered an issue with AsyncStorage where the token is not being retrieved in my Authentication.js screen after logging in. This results in the profile screen not loading a ...

Using JavaScript to adjust the width of the table header

I have a table that I need to adjust the width of using JavaScript or jQuery. <div class="dataTables_scrollHeadInner" > <table class="table table-condensed table-bordered table-striped dataTable no-footer" > <thead ...

What are the top recommendations for efficiently retrieving large amounts of data in Next.js?

I am in the process of creating a calendar for upcoming events throughout the year. Each event will function like a blog post complete with its own thumbnail and page. The set up for the CMS is fairly simple. To achieve this, I am utilizing FaunaDB and fe ...