What is the best way to hide the CSS tooltip box once it has been hovered over?

I currently have some external CSS code obtained from W3Schools:

/* Tooltip container */
.tooltip {
    /*position: relative;*/
    display: inline-block;
}

/* Tooltip text */
.tooltip .tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: black;
    color: #fff;
    text-align: center;
    padding: 5px 0;
    border-radius: 6px;

    /* Position the tooltip text - see examples below! */
    position: absolute;
    z-index: 20;

}

/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
    visibility: visible;
}

span:hover .tooltiptext {
    display:none;
}

This code is intended to affect the following block of HTML:

<div class="CELL_INFO tooltip">
    <span class="tooltiptext">
        Square resources: '.$WOOD.' wood, '.$IRON_ORE.' iron ore, '.$STONE.' stone.
    </span>
</div>';

The goal here is to display a tooltip contained within the <span class="tooltiptext"> only when hovering over the <div class="CELL_IFNO">. Additionally, when hovering over the tooltip span itself or leaving the containing div, it should disappear. The idea behind this setup is to ensure that the tooltip remains hidden once the cursor is no longer over the main div.

In essence, I am attempting to have the tooltip appear ONLY when the parent div is being hovered over and not when directly interacting with the child tooltip span.

Unfortunately, my current implementation is not functioning as desired and I am unsure how to achieve this using purely CSS. Can someone provide insight into what might be causing this issue?

NOTE: When applying the display: none; property globally to the span element, it works, but I specifically need it to apply only to spans with the "tooltiptext" class.

Answer №1

In order to make the correct selection, it is important to eliminate any spaces from your selector

Consider using span:hover.tooltiptext instead of span:hover .tooltiptext

Having a space between them will target elements with the class .tooltiptext that are inside span:hover elements.

Answer №2

Your latest requirement specifies that you are looking to target a .tooltiptext element located within a span. It appears as though what you actually intended was to select a span with the .tooltip class. Give this a try:

span.tooltiptext:hover {
    display: none;
}

Alternatively, you can use:

.tooltiptext:hover {
    display: none;
}

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

Designing a unique customized top navigation bar in Magento 1.9.2

I recently attempted to create a unique top menu in magento 1.9.2 by modifying the Navigation.php file located at app/code/core/Mage/catalog/Block/. I added some custom classes in an effort to customize the menu. In order to achieve this customization, I ...

Transform CSS into React.js styling techniques

My current setup involves using Elementor as a REST Api, which is providing me with a collection of strings in React that are formatted like this: selector a { width: 189px; line-height: 29px; } Is there a tool or library available that can conver ...

Using JQuery to Load Text Content from Textarea into an IFrame

Seeking a solution to transfer HTML textarea content into an IFrame upon button click. Any suggestions on achieving this functionality? Thank you in advance. Illustratively, when a user types Hello World in the textarea and clicks the submit button, the t ...

Ways to control the number of boxes that are checked

I am currently working on a script to restrict the number of checkboxes that can be checked, but I am encountering an issue where the script is disabling all checkboxes on the page. Is there a way to only disable a specific checkbox within a certain div? ...

What is the best way to set up a clickable image without making the entire div clickable?

Is it possible to make just a specific image clickable within a div without making the entire div clickable? I have an image inside an anchor tag and a surrounding div that is clickable. The issue is that the entire space around the first image, .home, is ...

Having trouble getting JavaScript to select a stylesheet based on time?

I am attempting to implement two different styles based on the time of day. Here is the code I am using to select the CSS file depending on the current time: <script type="text/javascript"> function setTimedStylesheet() { var currentTim ...

Create a regular expression to identify and substitute incorrect HTML properties

It's a harsh reality that my regex skills are lacking. Upon revisiting an old project, I stumbled upon some code that definitely needs attention. Take a look: strDocument = strDocument.Replace("font size=""1""", "font size=0.2") strDocument = strDocu ...

Implementing CSS to Style Images in JavaFX FXML

After creating the FXML structure below, I attempted to define a border in CSS for the Image by using code in the customerform.css file: <VBox id="customerFormPane" styleClass="customerForm" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.exampl ...

Adjust the hue of a picture rendered on an HTML5 canvas element

Looking at this image, I am wondering: What is the best way to change the entire color of this image to a custom RGB value? Essentially, if I have a grayscale image, how can I convert it back to color by specifying any RGB value? ...

The alignment of the text does not match the custom checkbox

Looking to customize checkboxes in GWT with CSS only. Successfully styled checkboxes without text, but struggling with checkboxes next to text. Current appearance: https://i.sstatic.net/Z3uYH.png Desired look: https://i.sstatic.net/Ligx1.png <span ...

Adjust the color of the whole row in an HTML table by changing the background

I am struggling with styling my dynamic table that pulls data from an API. I have successfully generated the table and inserted rows using JavaScript. However, I am facing styling issues. The colors of my table rows are inconsistent (similar to when using ...

Column-oriented and Slim-fit packaging

I have been working on designing a layout with a fixed height that will display multiple columns of specified size to accommodate flowing text. While I have successfully implemented this, I am facing an issue where the enclosing div does not adjust its siz ...

If the navigation menu contains sub-menus, it should slide to the left

I am currently working on developing a navigation menu with four levels of depth. The menu displays three levels at a time, and when the user reaches the fourth level, the first level should move to the left to make room for the second, third, and fourth l ...

How to monitor multiple items using the mouseleave event in jQuery

I am currently trying to achieve a specific effect by keeping the green border visible for the active menu item when hovering over the dropdown. The issue I am encountering is that the border disappears when moving the cursor over the dropdown, which is no ...

Utilize the href attribute on an image, whether it be through a class

I work as a designer and I'm looking to extract the same href link from class="kt-testimonial-title", either in id="slick-slide10", or in class="kt-testimonial-item-wrap kt-testimonial-item-0", or in class="kt-testimonial-image". Can this be achieved ...

Achieving www prefix enforcement for loading js file

I am facing an issue with the script on my website, which is built using OpenCart. The problem arises when accessing the site with a www prefix - the JavaScript file fails to load. However, when accessed without the www prefix (e.g. example.com), everyth ...

Need help managing floats with the: after pseudo selector?

Struggling with floats and needing to utilize the :after technique, but it's not cooperating. The floats after the red and blue box need to be cleared. Despite trying everything, the issue persists. Here is the provided code: <!doctype html> &l ...

Is the height of the element determined by the parent rather than the content?

I need help with a layout featuring three containers - Total (with a red border), Left (yellow border containing an image), and Right (blue border containing text). Left is set to approximately 30% width with float: left, while Right is set to 70% width w ...

How can you exhibit various images without relying on the <img> tag?

Is there a more efficient way to display over 500 images from a folder on an HTML page without the need to manually write out each img src tag? I have searched online for solutions, but most suggestions involve using PHP or "glob", which I am hesitant to ...

Incorporate visual elements such as images that resemble checkboxes

I'm searching for an innovative approach to replace the traditional checkbox, incorporating images instead. My idea is to have users click on an image, which will then fade out and reveal a tick box overlay. My inspiration comes from Recaptcha 2, whe ...