Creating a tooltip that doesn't rely on any child elements

I've been experimenting with creating a tooltip that appears when hovering over an <a> tag and displays a <div> from another location

For example:

<p>
blah blah <a class="tooltiphover">hover me</a> blah blah
</p>

<div class="tooltip">
<!-- tooltip info code goes here -->
</div>

However, all the tutorials and resources I have come across focus on using child elements which can clutter the code or require the use of extra <div> tags that may disrupt the visual appearance of the site

Another common approach is exemplified by:

<div class="tooltiphover">Hover me</div>
    <span class="tooltip">Tooltip stuff</span>
</div>

This becomes problematic especially when dealing with CSS selectors like .tooltiphover:hover .tooltip.

Is there a way in CSS to style both elements simultaneously without one being a child of the other? Using a comma or plus sign doesn't seem to achieve the desired effect.

Thank you for your help.

EDIT: To simplify, how can I make the div display in front of everything and beside the <a> tag like a proper tooltip? I might resort to using JavaScript for handling the mouseover event. It seems easier than struggling with CSS and HTML due to the confusing array of tooltip tutorials available online 😅

What would be the appropriate CSS code for achieving this?

Answer â„–1

There are numerous methods to achieve this without specifically needing a <div> tag. CSS offers a variety of selectors with distinct behavior rules. In general, CSS cascades and interacts in a forward manner, making it challenging to affect elements that come before in the document hierarchy (e.g., a child dictating behavior to its parent is uncommon.)

The most commonly used selectors for this task are either the element element selector or the element > element selector. These selectors require the target element to be a child of the parent element.

Here is an example:

div:hover > h2{
  display:block;
}

h2{
  display:none;
}
<div>hover me
<h2>i am revealed</h2>
</div>

<p>don't hover me</p>

However, as previously mentioned, these selectors create a tightly coupled relationship between the selected element and its parent. This is where the element ~ element selector becomes useful.

div:hover ~ h2{
  display:block;
}

h2{
  display:none;
}
<div>hover me</div>

<p>don't hover me</p>

<h2>i am revealed</h2>

While element > element necessitates a vertical relationship between the parent and child, element ~ element calls for a horizontal sibling relationship. Essentially, all elements meeting your selection criteria will be revealed as long as they are siblings within the same context.

But what if your desired target is not a direct sibling but a child of a sibling?

div:hover ~ h2{
  display:block;
}

h2{
  display:none;
}
<div>hover me</div>

<p>don't hover me</p>

<article>
  <h2>i am revealed</h2>
</article>

In this case, the previous approach fails because the elements are no longer direct siblings within the same context. There are various solutions to address this issue, both broad and specific, but one straightforward method is as follows:

div:hover ~ * h2{
  display:block;
}

h2{
  display:none;
}
<div>hover me</div>

<p>don't hover me</p>

<article>
  <h2>i am revealed</h2>
<article>

By combining the element ~ element selector with the * element selector, we can target any h2 element that is a child of any sibling to our original selector using the :hover pseudo-class.

If I understand correctly, and you wish for both the hover-triggering element and its target to change when hovered over, you simply need to define their respective styles separately.

div:hover ~ * h2{
  display:block;
}

div:hover{
  color: red;
}

h2{
  display:none;
}
<div>hover me and I turn red</div>

<p>don't hover me</p>

<article>
  <h2>i am revealed and not red</h2>
<article>

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

Place the "Close" button outside of the div container

I am facing an issue with the login button protruding slightly outside the border of a div. Despite trying to adjust the code to align it correctly, the issue persists. .closeWindow{ float: right; margin-left: 15px; /* half the width of the close b ...

Establishing an index for a kendo ui dropdownlist component post-ajax operation

After retrieving the datasource, I am attempting to set the index on my kendo-ui dropdownlist. While I have been successful using a checkbox (which I would rather not use) and with a local json datasource, I am encountering issues setting the selected valu ...

An Illustrative Example of Inheritance in Javascript?

I've been searching on various platforms for a straightforward example of inheritance without much luck. The examples I've come across are either too complex, obscure, or just not user-friendly. I have multiple service functions that all share so ...

VueJS can manipulate an inline template by dynamically changing its content and reinitializing

this particular query shares similarities with the discussions on VueJS re-compiling HTML in an inline-template component as well as How to implement Vue js directive in an appended html element Regrettably, the approach suggested in those threads is no l ...

Displaying a dynamic progress bar across all elements in fullscreen mode

I am looking to implement a full-screen indeterminate progress bar that overlays all screen elements. Here is my specific use case: When a user fills out a form, including an email field, the email id is checked against a database via ajax. While waiting ...

The Language Switcher is not displaying the new language setting

Starting a fresh website using Nuxt, I am currently in the process of setting it up for multilingual functionality in French and English. For setting up translation and language switcher, I referred to this specific tutorial and implemented the following: ...

Error message: The AJAX POST request using jQuery did not return the expected data, as the data variable could not be

There is a script in place that retrieves the input text field from a div container named protectivepanel. An ajax post call is made to check this text field in the backend. If the correct password is entered, another div container panel is revealed. < ...

Automatically populate a dropdown list based on the selection made in another dropdown menu

I'm populating a second textbox based on the input of the first textbox in auto.jsp. Now, I want to automatically populate a combo box as well. How can I achieve this? Specifically, I want to autofill the second combo box based on the selection made i ...

Unresponsive radio buttons in React failing to display as checked

My child component is designed to receive props from a parent. Within the child component, there are radio buttons that render like this: <div> <div className="radio"> <label> <input type="radio ...

Looking to optimize the JavaScript code for improved performance speed

Is there a more efficient way to avoid writing the same lines of code repeatedly without compromising performance? I've attempted using a for loop to categorize fields as 'mandatory' or 'optional', but it still requires duplicating ...

Jquery Banner Fade In animation malfunctioning

I am facing an issue with my banner on various browsers, especially IE 7,8, and possibly 9. When the fade-in effect is applied at the bottom of the banner, the shadows underneath turn black. Is there anyone who can help me resolve this issue? Website: ww ...

Struggling to establish a functioning proxy in my React and Node application

In the process of developing a react client app with a node.js express backend, I have encountered an issue related to project structure. https://i.sstatic.net/8rID0.png The client app includes a proxy configuration in its package.json file: "proxy": "h ...

A guide on retrieving specific data from a dynamic table in a React application

I am working on an app that features a dynamic table, where the data is retrieved from an API. My goal is to extract one of the parameters from this table, modify it, and then send it to another API. Here is a snapshot of the form-table: https://i.sst ...

How can I create an HTML custom upload button that changes to a hand cursor when hovered

Utilizing the CSS style tip shared in this post, I successfully designed a custom upload button. However, the new challenge is to ensure that the entire button area changes the mouse pointer icon to a hand. You can view my partial solution here (jSFiddle) ...

Modify the value of select options when the page is first loaded

I want to set up a dropdown menu with a list of regions, and I need the drop down to automatically select an option based on the variable value that I assign. Here is the code snippet. <select name="_sft_location[]" class="sf-input-select" title=""&g ...

Displaying a collection of nested objects in AngularRendering a group

Is there a way to render an object of objects in Angular without converting it into an array or similar structure? I have a large object of objects and I want to avoid multiple iterations through object-to-array conversions just to loop through the array i ...

Creating a dynamic multi-choice dropdown list with Django and Vue.js

I have been attempting to implement the Vue.js multiselect component from the following link: However, I am encountering an issue where instead of the expected multiselect dropdown, the output is not as desired and looks like this: https://i.sstatic.net/ ...

Is it necessary to install only form control styles from Bootstrap 4?

Does Bootstrap 4 offer a way to only install the form control styles like input group? I came across a resource that allows you to solely install the Bootstrap 4 Grid. Is there anything similar for downloading just the form styles? ...

Functionalities of HTML controls

I currently have a select element in my HTML code which looks like this: <select> <option id="US" value="US"> </option> <option id="Canada" value="Canada"> </option> </select> My requirements are twofold: ...

In the event that conflicting media queries take precedence over one another

I developed a set of queries for a particular element on a landing page. Here is the HTML structure: <div class="hsContent"> <article> This is my amazing content. Wow! </article> </div> This is how the initial styling looks fo ...