Can an element be targeted for hover in CSS without it being a child element?

In my HTML code, I have an <aside> and a <header>. The <header> contains a child element called container, which has some children including one named burger bar. What I am trying to achieve is that when I hover over the burger bar element, I want the <aside> element to become visible. I've attempted to accomplish this using the CSS selector

header .container .burger_bar:hover + aside
, but since the aside is not positioned adjacent to the burger bar in the HTML structure, this method does not work as intended.

Furthermore...


<div class='header'> 
    <div class='container'>
         <div class='burger_bar'>...</div>
    </div>
</div>

<aside>...</aside> /* <---when hovering the burger bar this will be 
transform: translate(0%) right after being transform: translate(-100%) */


Answer №1

To select the target element when both trigger and target are at the same level, you can utilize the CSS selector .trigger:hover ~ .target while hovering over the .trigger.

.trigger:hover ~ .target {
  color: green;
}
<div class="trigger">Hover Me</div>
<div class="target">Target</div>

If your trigger and target are not on the same level, it is recommended to use JavaScript to dynamically add a class to your target.

const trigger = document.querySelector('.trigger');
const target = document.querySelector('.target');

trigger.addEventListener('mouseover', () => {
  target.classList.add('active');
});

trigger.addEventListener('mouseleave', () => {
  target.classList.remove('active');
});
.target.active {
  color: green;
}
<div class="parent1">
  <div class="trigger">Hover Me</div>
</div>
<div class="parent2">
  <div class="target">Target</div>
</div>

Alternatively, you may consider using the :has() pseudo-class, but note that its support is limited (currently only works in Safari).

.parent1:has(.trigger:hover) ~ .parent2 .target {
  color: green;
}
<div class="parent1">
  <div class="trigger">Hover Me</div>
</div>
<div class="parent2">
  <div class="target">Target</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

Access the file using NodeJS and SailsJS for download

I am working on creating an API to enable the download of a static file named test.js. Upon testing the API using Postman, I noticed that instead of initiating a download process, it simply displays the content of the file. Is this behavior acceptable? a ...

Counting the occurrence of a specific class within an element using jQuery

Within my table, each row is assigned one or more classes based on its region. This is the structure of my table: <table> <thead> <th>Title</th> <th>Name</th> </thead> <tbody> <tr class="emea ...

Split an array of simple data types in JavaScript into separate sections

Is there a way to divide an unordered array of primitive types into specific segments like this: var array = [102,103,104,201,203,204,303,301,302,405,406,408,101]; => newArray = [[101,102,103,104],[201,203,204],[303,301,302],[405,406,408]] The divi ...

Organizing outcomes from a for each function into an array using javascript

I have a form with multiple values of the same name, and I need to arrange this data in an array before sending it via AJAX. However, when I try to do this using the .push function, I encounter an error that says "Uncaught TypeError: dArray.push is not a f ...

Expansive dropdown in Bootstrap 5

My navbar currently has a mega menu dropdown for 'products', but I want this dropdown to take up the entire width of the screen to accommodate more items. However, my CSS modifications did not produce the desired result. On large screens, it fail ...

What is the process for uploading an image to Postman with Express.js?

I have a collection of jpeg and png images saved on my local machine. What is the process for displaying them in Postman using express.js? Should I utilize sendFile? Is it feasible to showcase multiple images at once? Below is a sample code snippet: app ...

Identifying text within a paragraph using JavaScript regex, excluding any URLs mentioned

How can I use JavaScript on the client side to find a search term in a paragraph while excluding any matches that are part of a URL? I attempted to use the following regex but encountered an error: "A quantifier inside a lookbehind makes it non-fixed widt ...

Forces of Attraction and Repulsion in Three.js/Physijs: Exploring the Dynamics of Object

For my chemistry extra credit project, I am working on creating a 3D simulation that focuses on the atomic structure of compounds. My goal is to develop a visual representation of elements surrounding a central atom, such as fluorine surrounding nitrogen i ...

Working with Firebase cloud functions: Updating nodes

I'm integrating Firebase cloud functions into my application to track user likes. Each time a user likes a video, their ID is saved along with a boolean parameter (true). Here's an example: https://i.sstatic.net/rnqH1.png Within the Firebase c ...

Troubleshooting problems with text areas in Android when using JQueryMobile and PhoneGap

When viewing my textarea control, I noticed that it breaks away from the .css theme and displays two boxes - one themed and one not. This issue only occurs on my HTC Evo device, as the code works fine on the emulator. You can see a screenshot of this behav ...

Uploading my application on multiple servers after making changes using AngularJS and PHP

Currently, I am working on an angular-php web application that is live online for multiple users, each on their own subdomain. These subdomains include: sub1.mydomain.com sub2.mydomain.com sub3.mydomain.com sub4.mydomain.com sub5.mydomain.com Issue: An ...

What's preventing me from using the left click function on my published blog post?

This is my first time creating a blogger template and everything seems to be working correctly. However, I have encountered one problem. For some reason, I am unable to left click on my blog post. I have not installed any right/left click disabler and I&a ...

Swapping objects from Blender to Three.js using BlenderSwap

I'm just starting out with Blender and Blendswap. Recently, I downloaded a .blender file from Blendswap and wanted to incorporate it into my three.js scene. After exporting the Blender file as a .obj, I received both a .obj and a .mtl file. I then att ...

The jQuery extension for XDomainRequest in the $.ajax function called onprogress is

Summary: I am trying to make this compatible with this: Detailed Explanation: My goal is to develop a jQuery extension that introduces a progress method to the $.ajax object and works seamlessly with IE8 & IE9's XDomainRequest object. Currently, I ...

How to align two <select> elements side by side using Bootstrap

The issue I am encountering is that these two select elements within the same control-group are being displayed vertically when I want them to be displayed horizontally. I attempted using inline-block in CSS, but there are other <div> elements with ...

What is the process for customizing a form in the "add event" feature of wdCalender?

I am looking to customize the form in wdCalendar to add events with fields like first name and last name instead of "what". I need to modify the quickadd function in jquery.calendar.js, which is triggered when a user clicks on the calendar to add an event. ...

Retrieve input field values using the jQuery Taggd plugin in edit mode

Incorporating the jQuery taggd plugin has been smooth sailing so far. I made a few tweaks, specifically to use it in edit mode. In this edit mode, when a user inputs a value in the textbox, the plugin checks whether it is a URL or a string. If it's a ...

Restricting character count when displaying output in JavaScript

Is there a way to restrict the number of characters displayed on a label using a JavaScript file? I am encountering an issue with a specific part of the code that is triggered by a button click and is responsible for printing the contents from a preview bo ...

Access the reset button on a dynamic image using the document.getElementById method

I'm still new to coding in JavaScript and I have a question. In my class, I was required to assign four buttons to four different JavaScript commands. I managed to get three of them working successfully, but the last one seems to be giving me trouble. ...

Seamless Exploration with Google StreetView

Google uses a special effect on StreetView images to transition smoothly between them. You can see an example of this effect in the images below, with the second image showing it being applied. My question is: How can I incorporate this same effect into m ...