How to fix CSS so that the background color changes when hovered

Hey everyone, I'm working on a custom mouse-like feature where I want the color to change to white when hovering over links. I'm having some trouble with my code - it works fine when selecting the parent element but not the child. Can anyone help me out? I've been stuck on this for about 4 hours and could really use some assistance.

Here's the code snippet:

const cursor = document.querySelector(".cursor");
const cursor2 = document.querySelector('.cursor2');
document.addEventListener('mousemove',(e) =>{
  cursor.style.left = e.pageX + 'px';
  cursor.style.top =  e.pageY + 'px';
  cursor2.style.left = e.pageX + 'px';
  cursor2.style.top =  e.pageY + 'px';
} )
*{
    cursor: none;
}
body{
    background-color: black;

}
.cursor{
    position: fixed;
    width: 50px;
    height: 50px;
    border: 1px solid #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .12s;
  }
  
  .cursor2{
    position: fixed;
    width: 8px;
    height: 8px;
    background-color: #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .06s;
  }
  ul li a:hover ~ .cursor {
 background-color: white;
  }
<div class="nav">
        <ul>
            <li><a href="#">Hello</a></li>
           </ul>
    </div>



    <div class="cursor"></div>
    <div class="cursor2"></div>

I've included the code above that I've been working on, but I can't seem to figure out what's causing the issue. Any help would be greatly appreciated!

Answer №1

ul li a:hover ~ .cursor

This CSS rule targets any element with the class cursor that comes after a hovered a element.

In the provided HTML, however, the div with the class cursor is preceded by a div with the class nav.

To address this in CSS, you can use the following selector:

.nav:hover ~ .cursor

You may also add width: min-content; to all li elements to prevent the white background from expanding too much.

If the div.cursor directly follows the div.nav in your complete HTML structure, consider using + instead of ~.

const cursor = document.querySelector(".cursor");
const cursor2 = document.querySelector('.cursor2');
document.addEventListener('mousemove',(e) =>{
  cursor.style.left = e.pageX + 'px';
  cursor.style.top =  e.pageY + 'px';
  cursor2.style.left = e.pageX + 'px';
  cursor2.style.top =  e.pageY + 'px';
} )
*{
    cursor: none;
}
body{
    background-color: black;

}
.cursor{
    position: fixed;
    width: 50px;
    height: 50px;
    border: 1px solid #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .12s;
  }
  
  .cursor2{
    position: fixed;
    width: 8px;
    height: 8px;
    background-color: #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .06s;
  }
  
  .nav:hover ~ .cursor {
    background-color: white;
  }
<div class="nav">
        <ul>
            <li><a href="#">Hello</a></li>
           </ul>
    </div>



    <div class="cursor"></div>
    <div class="cursor2"></div>

Answer №2

There are a couple of solutions to this inquiry:

1- If you're able to utilize the ~ selector, you can have both a and

<div class="cursor"></div>
in the same parent element.

const cursor = document.querySelector(".cursor");
const cursor2 = document.querySelector('.cursor2');
document.addEventListener('mousemove',(e) =>{
  cursor.style.left = e.pageX + 'px';
  cursor.style.top =  e.pageY + 'px';
  cursor2.style.left = e.pageX + 'px';
  cursor2.style.top =  e.pageY + 'px';
} )
 *{
    cursor: none;
}
body{
    background-color: black;

}
.cursor{
    position: fixed;
    width: 50px;
    height: 50px;
    border: 1px solid #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .12s;
  }
  
  .cursor2{
    position: fixed;
    width: 8px;
    height: 8px;
    background-color: #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .06s;
  }
 .nav a:hover ~ .cursor {
 background-color: white;
  }
<div class="nav">
        <ul>
            <li>
            <a href="#">Hello</a>
            <div class="cursor"></div>
            <div class="cursor2"></div>
             </li>
        </ul>
</div>

2- In cases where altering the HTML structure is not an option, you can hover on the ul element:

.nav:hover ~ .cursor {
  background-color: white;
}

const cursor = document.querySelector(".cursor");
const cursor2 = document.querySelector('.cursor2');
document.addEventListener('mousemove',(e) =>{
  cursor.style.left = e.pageX + 'px';
  cursor.style.top =  e.pageY + 'px';
  cursor2.style.left = e.pageX + 'px';
  cursor2.style.top =  e.pageY + 'px';
} )
 *{
    cursor: none;
}
body{
    background-color: black;

}
.cursor{
    position: fixed;
    width: 50px;
    height: 50px;
    border: 1px solid #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .12s;
  }
  
  .cursor2{
    position: fixed;
    width: 8px;
    height: 8px;
    background-color: #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .06s;
  }
 .nav:hover ~ .cursor {
 background-color: white;
  }
<div class="nav">
        <ul>
            <li>
            <a href="#">Hello</a>
            </li>
        </ul>
</div>

<div class="cursor"></div>
<div class="cursor2"></div>

    

Answer №3

I discovered a solution to this issue - using JavaScript instead of relying solely on CSS.

HTML

<a onmouseenter="onHover()"  onmouseleave="notHover()" href="#">Text<a>

<div class="cursor"></div>
<div class="cursor2"></div>

JS part

const cursor = document.querySelector(".cursor");
const cursor2 = document.querySelector('.cursor2');

document.addEventListener('mousemove',(e) =>{
  cursor.style.left = e.pageX + 'px';
  cursor.style.top =  e.pageY + 'px';
  cursor2.style.left = e.pageX + 'px';
  cursor2.style.top =  e.pageY + 'px';
} )


function onHover(){
  cursor.style.backgroundColor = "white";
  cursor.style.height = "70px";
  cursor.style.width = "70px";
  cursor2.style.backgroundColor = "transparent";
  console.log("hello")
}

function notHover(){
  cursor.style.backgroundColor = "transparent";
  cursor.style.height = "50px";
  cursor.style.width = "50px";
  cursor2.style.backgroundColor = "white";
}

CSS

*{
    cursor: none;
}
body{
    background-color: black;

}
.cursor{
    position: fixed;
    width: 50px;
    height: 50px;
    border: 1px solid #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .12s;
  }
  
  .cursor2{
    position: fixed;
    width: 8px;
    height: 8px;
    background-color: #c6c6c6;
    border-radius: 50%;
    left: 0;
    top: 0;
    pointer-events: none;
    transform: translate(-50%, -50%);
    transition: .06s;
  }
  
  .nav:hover ~ .cursor {
    background-color: white;
  }

I am grateful for the assistance in finding an alternative approach to tackle this problem. I hope my method can help others facing a similar challenge. Thank you once again to all those who provided support and guidance.

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

Determining the lowest and highest values for a slider's range

I am looking to extract the minimum and maximum values from a price range. Here is the code snippet for reference: http://jsfiddle.net/dhana36/b453V/2/ Currently, I have one input field that controls both minimum and maximum values. However, I would pref ...

Customize navigation bar items in Bootstrap 4

I've been facing some challenges with adjusting the elements in my navbar. Check out this Navbar picture The main issues are: Placing the input div and search button on the left next to the brand image. Adjusting the width of the input divs. The col ...

Display additional links in Bootstrap navigation bar with a second level menu

Currently, I am integrating Bootstrap into an ASP.NET application using C#. Within this application, I have implemented a two-level menu system where certain options are available to users and others are only visible to administrators. The decision on whic ...

How can I target specific elements to toggle the active class using Javascript?

At the moment, I have a function that highlights my navbar menu items based on which one I clicked. However, the issue arises when there is a button inside my navbar receiving the highlight class upon clicking. I'm unsure of how to modify the JavaSc ...

retrieve the identification number associated with a specific value

How can I retrieve the value of this ID, which is sent from the controller and displayed in the table? https://i.sstatic.net/UbdxT.png This is my HTML code: <tbody class="no-border-x"> <tr> <td id="id"></td> <td i ...

IMG creates scroll bars even when width is set as percentage

In a previous discussion about how padding is calculated when using percentages (%), it was mentioned that the padding is determined based on the parent element's width. Now, I have come across the following CSS assignment: .img_tutorial_full_width ...

What is preventing the audio from playing in Safari?

Recently, I developed a small JavaScript program that is meant to play the Happy Birthday tune. This program utilizes setInterval to gradually increase a variable, and depending on its value, plays or pauses certain musical notes. However, I encountered ...

Navigate to specific sections

I'm currently developing a website and working on creating a menu that allows users to smoothly scroll to anchors within the same page as well as anchors on different pages. Successfully implemented the scrolling functionality for same-page anchors u ...

Creating a JavaScript button for text capitalization: A step-by-step guide

Is there a way to modify this script so that users are required to click a button in order for all text entered into the form to be converted to uppercase? I understand that the onclick function needs to be utilized, but I am uncertain about where and ho ...

Display a div using a PHP statement

I'm struggling to retrieve a specific div from my PHP function. Here is the code: error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); require_once("./include/membersite_config.php"); if (!$fgmembersite->Chec ...

Using Selenium and Python to choose from a list of search results

Although I have come across similar questions, I have been unable to implement the suggested solutions in my specific case. Hence, I am reaching out for help regarding my particular problem. The code snippet in question is as follows: driver.get("ht ...

Experimenting with alterations to link styles

A particular test we are conducting involves examining the style changes of a link (a element) after a mouse over. Initially, the link is displayed with a black font and no decoration. However, upon hovering the mouse over it, the font color changes to bl ...

The issue with the CSS combination of :after and :hover:after across different HTML elements

Encountering an issue while attempting to create a rollover effect using CSS :after and :hover pseudo-elements. Check out the clock and facebook icons on the right side by visiting: See below for the CSS code: .icon { background: url('. ...

The drop-down list was designed with a main button to activate it, but for some reason it is not functioning properly. Despite numerous attempts, the list simply will not display as intended

<html> <body> <button onclick="toggle()" class="nm" > main</button> <div class="cntnr" style="display : none;"> <ul class="cnkid"> <li class="cnkid"><a class="cnkid" ...

How can I activate a radio button by its corresponding label using Selenium?

After diving into the world of automation with Selenium in C#, I find myself faced with a challenge. I need to automate clicking on a specific radio button on a website based on the text of the associated label. What makes it even more interesting is that ...

Creating web pages that are compatible with different browsers

When developing ASP.NET pages to ensure browser compatibility, what are the key considerations? I have noticed that layouts may not display correctly in all browsers, even though they work fine in some. Achieving browser compatibility seems to be a signif ...

What methods can be used to beautify Html 5 files in Java or Groovy?

Is it possible to pretty-print HTML5 files using libraries in Java or Groovy? While I know there are classes available for pretty-printing XML in Groovy, it won't work for HTML5 since it's not necessarily well-formed. Are there any solutions spec ...

Can a socket be opened on port 23 using HTML and jQuery?

My current challenge involves listening to data being pushed out on port 23 from a specific IP address, with the restriction that I am unable to change this port. I have successfully created a solution using PHP and AJAX, but it is not ideal as it times ou ...

I am experiencing difficulty with the modal box not opening

I am attempting to create a CSS/HTML modal without using JavaScript. Below is the code snippet I have been working with. Despite indicating the link, the modal does not open as expected. I have included 3 CSS files, integrating them into bootstrap.css an ...

What is the process for including the title attribute in DNN controls within the DotNetNuke platform?

Is there a way to add the title attribute to the DNN:Label control and have it render as a title attribute, <label title="myTitle">, in HTML? My code snippet: <div class="dnnFormItem"> <dnn:label id="lblDateNeeded" runat="server" control ...