Problem arising when a tooltip sometimes fails to appear on the screen

One issue I'm encountering is that sometimes my tooltip doesn't fully appear on the screen. When my div is at the top of the screen, the tooltip gets cut off.

This is a snippet from my JSP page:

<div onmouseover="hideElementsByClassName('tooltip');displayElementById('toolip-42');" onmouseleave="hideElementById('toolip-42')">
    <img src="IMG/bouton/test.png">
    <div id="toolip-42" class="toolip" style="display: none;">
        <span style="font-weight: bold; font-size: 16px;">
            <table>
                ...
                ...
            </table>
        </span>
    </div>
</div>

Here's the CSS for the tooltip:

.tooltip {  
    width: 800px;   
    position: absolute;
    color: #000000;
    background-color: #FFFFFF;
    z-index: 1;
    border: 2px solid #C3C3C3;
    padding: 10px;
    border-radius: 5px;
    margin-top: -455px;
    margin-left: 55px;
    min-height: 400px;
    max-height: 80%;
    overflow: auto;
}

I want the tooltip to always show up on the right side of my image <img> without being cut off :

  • When I'm at the top of my screen, the tooltip should be displayed in the middle-bottom of the image.
  • When I'm in the middle of my screen, the tooltip should be shown in the center of the screen.
  • When I'm at the bottom of my screen, the tooltip should display in the middle-top of the screen.

Answer №1

Did you know that the 'title' attribute can be used to show tooltips on your website?

<div title="Hey there!">Hello world</div>

With this, the positioning of the tooltip will be taken care of automatically.

Answer №2

Here is a JavaScript solution for displaying an element:

function customDisplayElementById(eltId){
       var elt = document.getElementById(eltId);
        if(elt != null ){           
            elt.style.display= "";      
            elt.style['margin-top'] = -108 + 'px';// 108 height of div element
            var elmt = this.getOffset(elt);
            if(window.scrollY+window.innerHeight < elmt.bottom ){
                //console.log('popup trop bas');
                elt.style['margin-top'] = -1*(elmt.height-3) + 'px';
            }
            //console.log('elmt.left : ' + elmt.left + '\n','elmt.right : ' + elmt.right + '\n','elmt.top :' + elmt.top+ '\n','elmt.bottom : ' + elmt.bottom + '\n','elmt.width : ' + elmt.width + '\n','elmt.height : ' + elmt.height + '\n','window.screenY : '+ window.screenY);
        }
   }

  function getOffset(el) {
        const rect = el.getBoundingClientRect();
        return {
            left: rect.left + window.scrollX,
            top: rect.top + window.scrollY,
            right: rect.right + window.scrollX,
            bottom: rect.bottom + window.scrollY,
            width : rect.width,
            height : rect.height
        };
    }   

To implement this functionality on mouseover, use the following code:

<div onmouseover="customDisplayElementById('toolip-42'); onmouseleave="hideElementById('toolip-42')">

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

Struggling to get the font-weights of the Typography and Button components in the new React with Material-UI to display

In both my previous and most recent React applications, I have the following code snippets: <Typography variant="h6" color="inherit" noWrap > h6. Heading </Typography> <Button type="button" size="large" color="primary" > ...

Transferring time for streaming MP3 files from server to HTML5 audio

Currently, my node.js server is set up to convert and stream mp3 files in real-time. I have integrated an HTML5 audio tag to play this stream, but I am encountering a problem - the audio element is unable to determine the duration of the mp3 until it has c ...

Avoid flickering of images by properly handling the object URL when setting the image source in an asynchronous pipe

Implementing a JWT authorized endpoint for images has made it impossible to directly link to image urls in HTML. To work around this issue, we have created an async pipe that loads the image with proper authorization (handled by an HTTP interceptor, not s ...

Combination of written content and mathematical equations displayed on an HTML webpage

I've been tasked with creating something similar to the following on an HTML page: The math tag in HTML is not suitable because it struggles with handling the line break between parts "/begin()-/end()". To achieve the desired effect (succes ...

Are the keyboard settings for FileUpload different between IE and Firefox?

When using Apache's File Upload, I noticed a difference in behavior between Internet Explorer and Firefox. In IE, pressing "tab" and then "enter" on the Browse button submits the form instead of opening an option to browse. However, clicking the space ...

Animate horizontal scrolling with jQuery when the user scrolls the mouse

I've been experimenting with an animated horizontal offset for a div element triggered by the mousescroll event. You can check out my progress so far by following this link - could someone please take a look and help me identify any issues? Currently ...

Adjusting the background color using CSS according to the browser's dimensions

I'm having trouble identifying the issue with this code. The background color remains white and doesn't change as expected. <style type="text/css"> h1 { position: absolute; text-align: center; width: 100%; ...

Enter the text div that has been chosen

I have a mini question regarding how to select text and place it in a selected div. This demo was created from codepen.io. In this demo, you will see a blue button and text in a textarea. I would like to be able to select the text "Select this text", cli ...

Alert will only occur once the done function in jquery's .ajax method is executed

Something seems off and I'm struggling to understand the situation. I've used jQuery's .ajax() function to run a script that retrieves data from the database. The script is functioning properly and the data is being returned as expected: $. ...

Creating a pagination feature for an HTML table without using jQuery

Hello, I am relatively new to front-end development and currently undergoing training in a project involving HTML, CSS, and JavaScript. However, I seem to be facing an issue with the Table HTML element. I have created a table structure below: A header ...

HTML5 pattern grouping feature is not functioning as expected

I am attempting to validate this regular expression pattern="([a-zA-Z]+[0-9]*){4,}", which essentially means: It must always start with an alphabetic character. If a number is included, there must be at least 4 characters in total; for example, aaaa would ...

Try rotating a triangle in 3D using either CSS or JavaScript

I want to create a right-angle triangle that looks like this . . . . . . . . . . . . . . . . . . but I also want to add a 3D animation that will transform it into a right angle like this . . . . . ...

Choose from a range of options using a dropdown menu with the ability to make multiple selections

Looking for a way to have a simple city drop down with multiple selection options? I tried using the 'multiple' attribute of the select tag, but it changed the dropdown into a box shape with multiple select options. What I really want is for the ...

The variables are filled with HTML tags that will render the display in

I am facing an issue with displaying HTML tags in my website. When I try to print a variable containing HTML tags in the tpl file, it shows the tags along with the content. This is how it looks in the PHP file: $content = "<h1>Header</h1>< ...

Steps for displaying text with line breaks on a webpage

https://i.sstatic.net/fxhj6.png I am in need of a way to display text with line breaks using this specific script: <script> var app = angular.module('MyApp', []); app.controller('Ctrl', function ($scope) { consol ...

Trigger the change of an element upon hovering over another element

Is there a way to manipulate an element when another element is being hovered over, with the two elements structured like this: <div id="parent_element"> <div id="class-open-1"></div> <div id="class-close-1"></div> < ...

Picking out specific SharePoint components using jQuery

I encountered an issue with making my two radio boxes readonly. To resolve this, I attempted to disable the unchecked option. Despite trying to access the elements using jQuery and reviewing the data from the console, the solution did not work as expected. ...

A guide on dynamically accessing an image from the src directory using props in a Vite + React application

export default function Card(props) { return ( <card> <img className={cardCss.card__img} src={`../assets/imgs/card/${props.img}`} alt="" /> <div className={cardCss.card__stats}> ...

implementation of bug decrementation with a fadeout

For my current project, I need to implement a feature where individual elements containing a photo and their corresponding title fade out when clicked. However, the issue is that clicking on any picture fades out everything. How can I resolve this so only ...

Utilizing jQuery and HTML5 data attributes to dynamically retrieve JSON data

I have successfully converted my channel entries into JSON using ExpressionEngine, which is working smoothly. My current challenge is populating a Sweet Alert 2 overlay with information from specific JSON objects based on an ID stored in a data-* attribut ...