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

Finding all anchor tags in raw HTML using htmlagilitypack

My HTML document contains the following code snippet: string htmlData = "<!DOCTYPE html><html><Head></Head><body>&lt;div&gt;&lt;a target=\"_blank\" href=\"http://blender.palmbeachschools.org/GetFile ...

The CSS Validator does not recognize the CSS pointer-events and appearance properties

I'm a CSS beginner who recently created an app. I've encountered some errors and warnings when validating my CSS code: https://i.stack.imgur.com/g4MUz.png https://i.stack.imgur.com/Es1DE.png Could someone kindly explain these errors and warnin ...

Is there a way to modify the appearance of a text box to suit individual preferences?

Is it possible to alter the shape of a text box, creating a rectangle with a portion removed from one side? I have included an example image for reference. I came across clip-path: polygon in CSS, but it seems to only accept four coordinates. Thank you i ...

What is the best way to center the input on the page?

Does anyone know how to align the <input type="text"> in the center? I'm trying to create a login form but having trouble with positioning the input element. ...

What is the method for adding an event listener in AngularJS within an ng-repeat iteration?

I'm completely new to AngularJS and currently working on building a photo gallery. The basic idea is to have an initial page displaying thumbnail photos from Twitter and Instagram using an AngularJS ng-repeat loop. When a user hovers over an image, I ...

How can we reduce the use of !important in SCSS when working with React and Material UI?

In my current project, I am developing a Select component in React with Material UI. The CSS styling for the component is managed through an external SCSS sheet imported into the script file. While working on restyling the component, I found it challengin ...

Content escapes its parent container and seamlessly transitions to the following element

I am facing an issue with the layout of my element, which includes an image and text centered inside a parent container. Whenever I try adding more elements below the existing image and text, the green text with a red border overflows the current parent . ...

Trouble getting custom CSS media queries to function properly alongside Bootstrap 3

I've encountered an issue while using bootstrap 3 with jquery UI to implement search and select fields for the user. The CSS code seems to be working fine on larger screens, but it's completely non-functional on smaller screens. I tried applying ...

What is the process for implementing JavaScript in Django?

I'm a newcomer to this and although I've tried searching on Google, I haven't found a solution. I'm facing an issue where I can include JavaScript within the HTML file, but it doesn't work when I try to separate it into its own fil ...

Show the flex items arranged horizontally

This template is generated dynamically by Django: <div class="center row"> <h3><span>The Core</span></h3> {% for member in core %} <a class="core_img " href="#"> <div class="img__overlay"> ...

How can we consolidate an excess of menu items into a condensed, collapsed menu?

I have a navigation bar displaying category items, but if the menu items exceed the navbar width, how can I condense them into a drop-down menu using Bootstrap 3.0? Currently, my navbar only shows 10 items due to limited space. However, I have more items ...

How can I dynamically adjust the size of an image in a directory based on its filename with php?

Is it possible to resize a specific image in a folder by its name using PHP? ..................................................................................................................................................................... I have trie ...

What techniques does DeviantArt use to adapt and adjust the content displayed on a page based on the width of the user's

Visit and observe the functionality where new content appears as you widen your browser window. The alignment of the content in the center is a key aspect of this design on DeviantArt's website. How is this achieved? I attempted to create a div with ...

Tips on finding the key associated with a specific value

When a form is submitted, one of the fields being sent is an ID number instead of the name for easier processing by the server. For example: HTML dropdown select <select ng-model="myColor" class="form-control" ng-options = "color.ID as color.color ...

having trouble with developing a dropdown menu using jquery

I'm currently creating a drop-down menu for my website and here is the code I'm using: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html dir="ltr"> <head> <met ...

Strip away the HTML tags and remove any text formatting

How can I effectively remove HTML tags and replace newlines with spaces within text? The current pattern I am using is not ideal as it adds extra space between words. Any suggestions on how to improve this pattern? replace(/(&nbsp;|<([^>]+)> ...

data-abide is flagging all fields as incorrect

Recently, I encountered an issue with a specific form I have created: <%= form_for(@building_shared_space, data: {abide:''}) do |f| %> ... <div class="field"> <%= f.label :room_type, 'Room Type' %> <%= ...

Issues with rendering components using ReactJS are causing problems with <p/> and <br/> tags not functioning as expected

I am having trouble using <p/> or <br/> tags to create new lines after a custom ReactJS component that includes Bootstrap CSS with <div className="col-sm-10">. var MyChatClientView = React.createClass({ render: function() { retur ...

Combine various canvases into a single one (for exporting purposes) with pure Javascript

I'm currently faced with a unique challenge involving the creation of around 200 canvases, each showcasing an image captured from the user's webcam as part of an experimentation process for the "photo finish" technique. My task now is to export ...

Update the content within every <td> element using AJAX/JQUERY on a table created from JSP

I have a database filled with descriptions and corresponding ID numbers. The table displays them like this: index.jsp <table> <tr> <td>Name:</td> <td>Id:</td> </tr> <c:forEach items ...