Ways to design siblings that are not currently being hovered over

Is there a way to achieve an effect where hovering over one list item causes all other list items to fade out?

I'm searching for a method to style an element that is not being hovered, when one of its siblings is in the hovered state.

I've tried two different approaches:

1. Hover Detection on Parent Element

The issue with this approach is that hovering anywhere within the parent element triggers the effect, even between anchor tags which is not ideal. The effect should only be applied when directly hovering over the list item itself.

.d-flex {
  display: flex;
  justify-content: space-around;
}

.d-flex:hover a:hover {
  color: black;
}

.d-flex:hover a {
  color: white;
}

.d-flex a {
  color: blue;
}
<div class="d-flex">
  <div class="item"><a href="#">item 1</a></div>
  <div class="item"><a href="#">item 2</a></div>
  <div class="item"><a href="#">item 3</a></div>
  <div class="item"><a href="#">item 4</a></div>
</div>

2. Using the General Sibling Combinator (~)

This approach is close, but it only selects siblings that appear after the element in the DOM.

.d-flex {
  display: flex;
  justify-content: space-around;
}

a {
  color: black;
}

.item:hover ~ .item a{
  color: white;
}

a:hover {
  color: blue;
}
<div class="d-flex">
  <div class="item"><a href="#">item 1</a></div>
  <div class="item"><a href="#">item 2</a></div>
  <div class="item"><a href="#">item 3</a></div>
  <div class="item"><a href="#">item 4</a></div>
</div>

PLEASE NOTE: I am specifically seeking a CSS-only solution for this problem. While JavaScript can achieve this effect, I would like to avoid that route.

Answer №1

You're on the right track with your initial approach. To enhance it, make use of the pointer-events property to deactivate hover effects in empty spaces and restrict them only to the links:

.d-flex {
  display: flex;
  justify-content: space-around;
  pointer-events:none;
}

.d-flex:hover a:hover {
  color: black;
}

.d-flex:hover a {
  color: white;
}

.d-flex a {
  color: blue;
  pointer-events:auto;
}
<div class="d-flex">
  <div class="item"><a href="#">item 1</a></div>
  <div class="item"><a href="#">item 2</a></div>
  <div class="item"><a href="#">item 3</a></div>
  <div class="item"><a href="#">item 4</a></div>
</div>

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

Switching PHP include on an HTML page using JavaScript

I've been attempting to modify the content of the div with the ID "panel_alumno" using a JavaScript function that triggers when a button is clicked. My goal is to display a different table each time the button is pressed, but so far, I haven't be ...

What is the best way to incorporate a changing variable within an htmx request?

One of the endpoints in my site requires an ID to be passed as a parameter. For example: mysite.com/product/{id}?limit=5 I'm wondering how to pass the 'id' variable in the hx-get attribute. I can utilize AlpineJS or vanilla JS for this tas ...

Looking to discover how to optimize a website for various Android resolutions?

Currently, I am working on designing websites specifically for Android phones. However, I have encountered a major challenge due to the wide range of screen resolutions found on these devices. Some phones boast a height of 800px while others are limited to ...

Error copying cell width attribute when using border collapse

Currently, I am attempting to duplicate a table header by copying the tr HTML and then replicating the th widths. Unfortunately, this method is unsuccessful as the widths are displayed as (width minus W) pixels when border: 'Wpx' and border-colla ...

Obtaining the accurate offsetTop and offsetLeft values for an element following a CSS3 rotation

Is there a method to accurately determine the offsetTop and offsetLeft values of an element post-transform rotation? Are there any lesser-known element properties that could be helpful in this scenario? Attached is an image that can provide further clari ...

Is there an equivalent of getElementById for placeholder text?

I need help automating the input of information on a webpage using JavaScript. Each field has a unique ID, like this: <div id="cc-container" class="field has-float-label"> <input placeholder="ccnumber" id="credit_card_number" maxlength="16" ...

What is the process for deselecting text from a text field?

After performing a search and displaying the results on my search form, I've noticed that the text query in the textfield is being marked as selected text even though I don't want it to be. How can I prevent this? Fiddle 1) What is the best app ...

SVGs do not display on iPhones

Having some issues with my code - specifically, I have an SVG animation that seems to work on all devices except iPhones. I've been struggling to find a solution. Here is the code snippet for the SVG: <div class="contenuto-svg"> <svg vi ...

The md-autocomplete feature showcases search results in a list format rather than a dropdown menu

Currently, I am working on implementing the Angular Material md-autocomplete tag to provide suggestions for zip codes in a form. To achieve this, I have set up an asynchronous service that fetches the suggestions and populates the results. My reference poi ...

Converting HTML/Javascript codes for Android Application use in Eclipse: A Step-by-Step Guide

How can I implement this in Java? Here is the HTML: <head> <title>Google Maps JavaScript API v3 Example: Geocoding Simple</title> <link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="styles ...

(Reactjs) Steps to automate submission of file upon selection

I'm having trouble figuring out how to automatically submit an image using an input field without a submit button. My current approach involves selecting the file and then submitting it automatically. Here's what I have so far: const [image, setI ...

React js background image not filling the entire screen

Having experience with React Native, I decided to give ReactJS a try. However, I'm struggling with styling my components because CSS is not my strong suit. To build a small web application, I am using the Ant Design UI framework. Currently, I have a ...

What is the best way to arrange cards in a specific order with breaks in between each flip

I am currently working on creating flip cards using HTML and CSS. I have successfully implemented the hover effect which flips the cards when hovered over. Now, my goal is to make the cards automatically flip one after the other at specific time intervals ...

Include an Open OnClick event in the React/JSX script

We have a dynamic menu created using JavaScript (React/JSX) that opens on hover due to styling. My inquiries are: Instead of relying on CSS for the hover effect, where in the code can I implement an OnClick event to trigger the menu opening using Java ...

Is there a way to display the button only when the user is able to enter an email in the input field?

I have a form for users to update their profile and a button to delete their account. I want the delete account button to only be visible if the user enters their email. $(document).ready(function() { var user_email = $('input[name="emp_email"]&a ...

Dynamic way to update the focus color of a select menu based on the model value in AngularJS

I am looking to customize the focus color of a select menu based on a model value. For example, when I choose "Product Manager", the background color changes to blue upon focusing. However, I want to alter this background color dynamically depending on th ...

Achieve perfect vertical alignment for a set of three identical boxes using CSS media queries

Bootstrap is being used and the goal is to align three boxes of the same height vertically. https://i.stack.imgur.com/8xg0g.png Currently, the three blocks are not of equal height and do not occupy maximum space. Some manual padding has been added to addr ...

How can I locate the CSS selector for a text message?

Looking for a CSS selector to target the message "Congratulations! displayed after successfully filling out a registration form with Selenium Web driver. Need to assert the text "Congratulations, Your email has been verified". Here is the HTML code: <d ...

Center an element over two other elements using CSS

I have three elements: checkout-left, checkout-right, and product-2 <div class="order-form"> <div class="checkout-left"> <div class="video another-video"> ...

A Simple Guide to Mastering the Flexbox Columns Concept

After exploring flexbox for some time and finally grasping its potential, I wanted to share an example showcasing how simple it is to create flexible column layouts without the need for CSS hacks involving excessive margins and paddings. I understand that ...