Activating Anchor's Pseudo Content on Hover of Another Element

I've encountered a straightforward issue - I have an anchor in a navigation menu that generates an arrowhead character as ::after pseudo content in CSS when hovering over it:

https://i.sstatic.net/Yhd9A.jpg

However, the arrowhead character disappears when moving the mouse away and then hovering on a different anchor in the dropdown menu below it:

https://i.sstatic.net/zOdbZ.jpg

Below is the basic CSS used to achieve the hover effect:

.nav > li.dropdown > a:hover:after {
display: block;
position: relative;
top: 2px;
width: 100%;
text-align: center;
content: "\25B2";
color: #ccc;
}

ul.dropdown-menu > li > a:hover {
padding: 10px 15px;
color: #0096d6;
}

Since this is a common navigation technique, I am looking for a way to use Javascript to keep the ::after content displayed on the top anchor when interacting with a separate anchor in the dropdown menu below it. jQuery cannot access ::after elements as they are not part of the DOM. However, some examples show creating ::after content by adding style tags to document heads in pure Javascript, though I'm unsure if this will solve my issue. Is there a method where hovering over one element can trigger the ::after content of another element using Javascript? Thank you for any assistance you can provide.

UPDATE: Here is a relevant HTML snippet as requested. I have also updated the CSS above to reflect the dropdown hover state.

<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Tools and Information</a>        
<ul class="dropdown-menu">
<li><a href="/foo">Print Permanence</a></li>
<li><a href="/foo2">Another Link</a></li>
</ul>
</li>
</ul>

Answer №1

Shift the hover effect to the li element like this:

.nav>li:hover>a:after {
  display: block;
  position: relative;
  top: 2px;
  width: 100%;
  text-align: center;
  content: "\25B2";
  color: #ccc;
}

$('body').on('click', '.dropdown-toggle', function(e) {
  e.preventDefault();
  $('.dropdown-menu').toggleClass('active');
});
.nav li {
  position: relative;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: 100%;
  margin: 0;
  padding: 0;
}

.dropdown-menu.active {
  display: block;
}

.dropdown-toggle {
  position: relative;
}

li {
  list-style: none;
  display: inline-block;
}

a {
  display: block;
  color: white;
  padding: 10px;
  width: 150px;
}

.nav>li>a {
  background: blue;
}

.dropdown-menu a {
  background: green;
}

.nav>li:hover>a:after {
  display: block;
  position: relative;
  top: 2px;
  width: 100%;
  text-align: center;
  content: "\25B2";
  color: #ccc;
}

ul.dropdown-menu > li > a:hover {
  padding: 10px 15px;
  color: #0096d6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav navbar-nav">
  <li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">Tools and Information</a>
    <ul class="dropdown-menu">
      <li><a href="/foo">Print Permanence</a></li>
      <li><a href="/foo2">Another Link</a></li>
    </ul>
  </li>
</ul>

Answer №2

To optimize the nested structure of the submenu, the hover effect should be shifted to the anchor that reveals the submenu upon hovering.

By doing this, the anchor will be hovered over simultaneously with the submenu anchors.

Take a look at this snippet for reference:

li { cursor: pointer; }
.outer ul { display: none; }
.outer:hover ul { display: block }
.outer { 
  display: inline-block;
  position: relative; 
  padding: 4px;
}
.outer span { background-color: #007bff; } 
.outer ul {
  position: absolute;
  top: 50px; /* li height + padding + actual distance */
  background-color:#007bff;
}
.outer ul::before {
  content: "";
  display: block;
  position: absolute;
  top: -10px;
  left: 0px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 10px 10px 10px;
  border-color: transparent transparent #007bff transparent
}
<ul>
  <li class="outer">
        <span>Abcd</span>
        <ul>
           <li>abcd</li>
           <li>efgh</li>
           <li>ijkl</li>
        </ul>
  </li>
  <li class="outer">Efgh</li>
  <li class="outer">Ijkl</li>
</ul>

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

How to use Axios in Vue JS to access an array within a JSON object

Struggling to access arrays inside a JSON object and save them into separate arrays. Despite trying various methods, I have not been successful in retrieving these arrays. I suspect that my approach to accessing arrays within the JSON object is incorrect, ...

What is the process of converting a file into a binary stream using Javascript?

I'm currently in the process of building a website using Next.js and I want to upload a file to OneDrive using the Microsoft Graph REST API. The documentation states that the request body should be the binary stream of the file you wish to upload, bu ...

Arranging Bootstrap columns based on click action

How can I rearrange the order of columns using bootstrap and jQuery when clicked? I've tried to implement this functionality for the left button, but it doesn't seem to be working as expected: $("#left").on("click", function() { ...

Emulate the selection process using element-ui and vue-test-utils

During my unit tests using Jest and Element-ui in Vue, I encountered an issue with a component containing a select element with 2 options. After selecting an option from the dropdown, I needed to verify that a specific action was called. 1) Everything wor ...

Angular is unable to iterate through a collection of ElementRef instances

My list of ElementRef contains all my inputs, but adding listeners to them seems to indicate that textInputs is empty even though it's not. @ViewChildren('text_input') textInputs!: QueryList<ElementRef>; ngAfterViewInit(): void { ...

Insert an item into a convoluted array

I'm struggling to insert an Object into a complex array. My attempt looks like this: "DUMMY_PLACES[0].todos.byIds.push," but I am unable to make it work. I have an id and content for the object, and the completed property should default to false. I ho ...

Tips for organizing the output of items from the .getJSON method

I am currently facing an issue where my output is displayed on the page in the same order as the raw JSON file. I am seeking assistance on how to sort the items based on a specific property within each item. For instance, I would like to sort alphabetical ...

Is it possible to create an HTML link that prevents users from navigating back to the previous page or displaying the previous link in their browsing history?

I've been tasked with creating a button on a client's website that links to another page, like Google, for the purpose of assisting someone seeking help in case of an emergency. The challenge is finding a way to prevent the user from easily retur ...

Convert the data into a format that is compatible with JavaScript

Having trouble extracting a value from json and placing it into my controller. I want to assign the membership value of 8 to $scope.value = data.membership; Service call in JS: .service('getMembership', function ($http, SERVER_URL) { r ...

Automatically trigger a Bootstrap 5.2 modal when the page loads

Currently, I've been utilizing Bootstrap in conjunction with JQuery and have a specific code snippet that displays a Modal when a page is loaded. However, with the latest version 5.2 of Bootstrap, there is a push to move away from using JQuery (which ...

Guide: Generating a DIV Element with DOM Instead of Using jQuery

Generate dynamic and user-defined positioning requires input parameters: offsetHeight, offsetLeft, offsetParent, offsetTop, offsetWidth ...

Divide a single array into two separate arrays using React Native

My goal is to divide an array into two separate arrays, one for letters and the other for frequencies. var list = [ "ES 324798", "LE 237076", "EN 231193" ] This is the original array that needs to be split. I am looking to create an array with all the l ...

Generating modal pages on the fly

One thing that's been on my mind is creating modals for my page. Typically, I would include the HTML code for my modal directly in the page like this: <div class="my-modal"> <form action="/home/index"> <input type="text" class="txt- ...

Encountering an error message related to Bootstrap carousel: "Unable to read property 'offsetWidth' of undefined"

Encountering a frustrating error message: "Cannot read property 'offsetWidth' of undefined" when using Bootstrap carousel. Let me share my code with you: <div id="myCarousel" class="carousel slide" > <ul class=& ...

What is the best way to display breadcrumb text on a new line within a pop up when the width exceeds without resorting to a scroll bar

Presently, my breadcrumb has a scrollable wrap with text overflowhttps://i.sstatic.net/dhllv.png I want to make the overflowed text continue on the next line. How can I achieve this? The CSS code I am using for the image attached is as follows: .breadcrumb ...

Differences between using CSS custom properties in the :root selector versus the body/html tags

Why would you choose to define global CSS custom properties in the :root element rather than in the body or html tags? How might this decision impact the effects or performance of the page? ...

Creating Chaos in PrimeFaces Page Navigation with JavaScript

I have implemented third-party connection monitoring code called Online JS by Denis Radin and it seems to be causing unexpected interactions with the navigation elements on my webpage. I am seeking insight into possible reasons for this behavior in order t ...

Is there a way to verify the functionality of DigitalOcean features using doctl before transitioning to a live environment?

As a newcomer to using DigitalOcean Functions, I have set up some JavaScript namespaces using the doctl serverless command. My main query is regarding testing DigitalOcean functions locally before deploying them to production. Is there a way to do this wit ...

Is there a way to ensure that neighboring divs have the same height as the tallest div in a row?

In the product description column, the value spans two lines, causing the row to adjust its height accordingly. However, the issue arises when adjacent divs do not match this height, resulting in an incomplete border as shown in the image below. This probl ...

Error Alert: The combination of React and Redux-router is causing an unexpected issue. The reducer is expected to be a

Whenever I make a coding mistake and encounter a runtime error, the error reporting is not very helpful. For example, when I mistakenly typed Date() instead of new Date(), the error message I received was... Uncaught Error: Expected the reducer to be a fu ...