Ensure that the tooltip remains visible within the confines of the page

In my Angular application, I have successfully implemented a versatile tooltip feature that goes beyond just displaying text. The tooltip is a div element that has the ability to contain various contents:

.tooltip-master {
  position: relative;

  .tooltip-slave {
    visibility: collapse;
    position: absolute;
    top: 80%;
    left: 80%;
  }
}
.tooltip-master:hover {
  .tooltip-slave {
    visibility: visible;
  }
}
<div class="tooltip-master">
  <div>Content within element</div>
  <div class="tooltip-slave">Tooltip content here</div>
</div>

Everything functions as intended when hovering over an element, with the tooltip appearing at the bottom right. However, for elements positioned near the bottom of the page, the tooltip extends beyond the viewable area and becomes inaccessible.

I am now seeking a solution that will automatically move the tooltip upward if it risks going off-screen. Ideally, I would like to achieve this using only CSS. Are there any recommendations or suggestions for achieving this behavior?

Answer №1

It's challenging to provide a precise solution without more information. It seems like you have set the positioning to top: 80% and left: 80% for a specific purpose, which may complicate finding a CSS-only resolution.

In such cases, I recommend using ngx-bootstrap. You can find more information on how to implement tooltips automatically at .

However, if you have a unique reason for wanting to customize it yourself, feel free to explore their implementation methods for guidance.

Answer №2

After much trial and error, I have managed to come up with a solution that, unfortunately, does not work with plain CSS, but gets the job done.

To determine whether the tooltip should be displayed above or below, you will need to add either the class tooltip-slave-above or tooltip-slave-below:

.tooltip-master {
  position: relative;

  .tooltip-slave {
    visibility: collapse;
    position: absolute;
    left: 80%;
    z-index: 200;
  }
  .tooltip-slave-below {
    top: 80%;
  }
  .tooltip-slave-above {
    bottom: 80%;
  }
}
.tooltip-master:hover {
  .tooltip-slave {
    visibility: visible;
  }
}

The master div now responds to the mouseenter event, and the tooltip classes are dynamically applied based on the value of the property setTooltipBelow:

<div class="tooltip-master" (mouseenter)="tooltipMasterEnter($event)">
  <div>Element content</div>
  <div [ngClass]="{
          'tooltip-slave': true,
          'tooltip-slave-below': setTooltipBelow,
          'tooltip-slave-above': !setTooltipBelow
        }">Tooltip content</div>
</div>

This property is determined in a simple manner:

public tooltipMasterEnter(e: MouseEvent) {
  const hBelow = e.view.innerHeight - e.y;
  const hAbove = e.view.innerHeight - hBelow;
  this.setTooltipBelow = hBelow > hAbove;
}

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

Alternative solution for :has selector in Firefox without relying on JavaScript

Currently I am building a small website for a friend and incorporating the :has pseudo class. However, Firefox does not support this feature by default, unless manually enabled. Are there any workarounds available in this case? I am creating a hamburger m ...

What is the best way to display converted value in Angular 8?

In my current project, I am utilizing .NET Core 2.2 for the backend and Angular 8 for the frontend. The scenario involves having integer values on the backend within a specified range. For example: [Required] [Range(1073741824, 1099511627776)] // ...

Guide to highlighting manually selected months in the monthpicker by utilizing the DoCheck function in Angular

I'm facing an issue and I could really use some assistance. The problem seems quite straightforward, but I've hit a roadblock. I have even created a stackblitz to showcase the problem, but let me explain it first. So, I've developed my own t ...

Ionic 2 fails to navigate when using [navPush]

Today I delved into working with Ionic 2 and was making good progress. I had successfully implemented navigation across 3-4 pages, but then something suddenly broke that has left me scratching my head. Now whenever I try to navigate to another page, I kee ...

What is the best method for aligning buttons in a row with all the other buttons?

I have been attempting to display two buttons, id="strength" and id="minion", when a certain button, id="expandButton", is clicked. I want these two buttons to be positioned on either side of the button that triggers their cre ...

Unclear on the usage of "this" in arrow functions?

After going through various discussions and articles on this topic, I find myself still perplexed about the meaning of this in arrow functions. I've been encountering run-time errors with a code snippet similar to the following: export class Foo imp ...

Angular 7 is throwing an error message that reads: "Module not found: 'AppModule'"

When running ng build, an error occurs without providing any specific details like the file name. This project is an ASP.NET Core app with Angular 7. c:\Users\siva\Myapp\ClientApp>ng build Date: 2019-08-08T13:22:52.205Z Hash: 3cf960 ...

Error: Angular 4.1.3 routing is unable to locate child module within parent module

Thanks in advance for any help! Greetings, I'm encountering the following error as described in the title. Despite my attempts at troubleshooting and research, I am unable to resolve it. Below are some important files - can anyone with experience in ...

Optimizing the position of smart info windows in Google Maps

I am facing a challenge while attempting to switch the infowindow in Google maps to the smartinfowindow, as the position of the infowindow appears incorrect. This issue only occurs with the smartinfowindow and not with the standard infowindow. Upon furthe ...

What is causing the discrepancy between the appearance of my fiddle and my page?

Hey there, I encountered an issue while working on a page using jsFiddle - it seems to be behaving differently when viewed on the actual website! If you want to take a look: http://jsfiddle.net/8Hpmj/8/ http://ktaadn.me/about/ The example at ktaadn.me do ...

Validation of Angular 5 forms for detecting duplicate words

I need help with a basic form that has one input. My requirement is to validate that the user does not input an existing word. If the user does enter an existing word, I would like to display a message below the input field saying "This word already exis ...

Establish a set height for ngx-datatable component in Angular version 4

Looking for guidance on setting up a table in an Angular 4 application using the swimlane/ngx-datatable. The table requires the property scrollbarV, and I want to achieve a fixed height with a scroll bar, similar to the example provided by them. However, i ...

How to interrupt a JQuery animation and restart it from the middle?

Currently, I am tackling my first JQuery project and facing a challenge. As the user mouseleaves the .container, the animation does not reset but continues as if they are still within it. My goal is to have the animation revert in reverse if the user decid ...

Tips for presenting HTML source code with appropriate tag coloring, style, and indentation similar to that found in editors

I need to display the source code of an HTML file that is rendered in an iframe. The source code should be shown with proper tag colors and indentations similar to editors like Sublime Text. https://i.stack.imgur.com/IbHr0.png I managed to extract the sour ...

CSS3 Animation displaying text color change on various elements in a continuous loop

I was attempting to create a dynamic animation where multiple texts change color, similar to what you can see on https://vercel.com. I have successfully figured out how to make one text change color using keyframes, but I am struggling to make each text ch ...

The calculator I designed using HTML, CSS, and JavaScript is experiencing difficulty adjusting to various screen sizes

I recently built a calculator using HTML, CSS, and JavaScript. It works perfectly on PC or big screens, but when viewed on smaller devices like phones or tablets, the display gets cut off and does not adjust properly. Here are some example pictures for ref ...

The arrangement of columns is not correctly aligned

My website layout is giving me trouble. I want to create a three column design, but unfortunately, it's not aligning properly. Each subsequent column is being pushed down slightly more than the previous one. Is there any way to fix this issue? Interes ...

Is there a way to restore a minimized min.css file?

Recently, I attempted to utilize an online tool for unminifying my CSS code. However, when I replaced the minified code with the unminified version and accessed the URL, I discovered that the entire website had lost its layout. Unfortunately, relying sole ...

The ASP .Net Core 3.1 Angular template is malfunctioning

https://i.sstatic.net/T3hFx.pngAfter creating a new Web Project using the .Net Core with Angular template in Visual Studio 2019, I encountered an error when trying to build the solution. The error message stated: An unhandled exception occurred while proce ...

The Material Angular components fail to load

Just started a brand new project using Angular and Material design UPDATE : Check out the live editor on StackBlitz: here Working on implementing the toolbar example, but here's what I have so far: https://i.sstatic.net/MUVWb.png Tried inserting t ...