What is the best way to display a tooltip above a parent container that has the tailwind class overflow-x-scroll enabled?

I'm having trouble displaying a tooltip when I hover over a square inside a parent container with the overflow-x-scroll class. The issue arises because the tooltip gets cut off due to the parent container's properties. Adjusting z-index and overflow-y didn't resolve the problem.

Unfortunately, changing the height of the parent container is not an option for me at this time.

View the Tailwind CSS playground: https://play.tailwindcss.com/Mb5XO1DTZs

HTML:

<div class="mx-auto max-w-6xl p-8">

  <div id="grid" class="grid grid-rows-7 gap-2 grid-flow-col p-4 bg-red-300 rounded-md shadow-md overflow-x-scroll">

    <div id="gridItem" class="relative cursor-pointer items-center bg-gray-500 shadow-md rounded-[2px] h-3 w-3 text-xs hover:z-50" data-tooltip="LOOK HERE">
    </div>
    
    <div id="gridItem" class="relative cursor-pointer items-center bg-gray-500 shadow-md rounded-[2px] h-3 w-3 text-xs hover:z-50" data-tooltip="LOOK HERE">
    </div>

    <div id="gridItem" class="relative cursor-pointer items-center bg-gray-500 shadow-md rounded-[2px] h-3 w-3 text-xs hover:z-50" data-tooltip="LOOK HERE">
    </div>

  </div>

</div>

CSS file:

@tailwind base;
@tailwind components;
@tailwind utilities;

#gridItem::before,
#gridItem::after {
  --scale: 0;
  --arrow-size: 5px;
  --tooltip-color: green;

  position: absolute;
  top: -.25rem;
  left: 50%;
  transform: translateX(-50%) translateY(var(--translate-y, 0)) scale(var(--scale));
  transition: 150ms transform;
  transform-origin: bottom center;
}


#gridItem::before {
  --translate-y: calc(-100% - var(--arrow-size));

  content: attr(data-tooltip);
  color: white;
  padding: .5rem;
  border-radius: .3rem;
  text-align: center;
  width: max-content;
  background: var(--tooltip-color);
}

#gridItem:hover::before,
#gridItem:hover::after {
  --scale: 1;
  z-index: 50;
  visibility: visible;
  overflow: visible;
}

#gridItem::after {
  --translate-y: calc(-1 * var(--arrow-size));

  content: '';
  border: var(--arrow-size) solid transparent;
  border-top-color: var(--tooltip-color);
  transform-origin: top center;
}

Answer №1

To make the tooltip elements work properly, it's important to position them outside the element with the overflow-x-scroll property.

Here is an example:

<script src="https://cdn.tailwindcss.com/3.3.5"></script>

<div class="relative mx-auto max-w-6xl p-8 [--arrow-size:5px] [--tooltip-color:green]">
  <div id="grid" class="grid-rows-7 grid grid-flow-col gap-2 overflow-x-scroll rounded-md bg-red-300 p-4 shadow-md">
    <div id="gridItem" class="relative h-3 w-3 cursor-pointer items-center rounded-[2px] bg-gray-500 text-xs shadow-md hover:z-50" data-tooltip="LOOK HERE"></div>

    <div id="gridItem" class="relative h-3 w-3 cursor-pointer items-center rounded-[2px] bg-gray-500 text-xs shadow-md hover:z-50" data-tooltip="LOOK HERE"></div>

    <div id="gridItem" class="relative h-3 w-3 cursor-pointer items-center rounded-[2px] bg-gray-500 text-xs shadow-md hover:z-50" data-tooltip="LOOK HERE"></div>
  </div>

  <div class="absolute left-[calc(theme(padding.8)+theme(padding.4)+(theme(width.3)/2))] top-[calc(theme(padding.8)+theme(padding.4)-.25rem)] w-max origin-bottom -translate-x-1/2 translate-y-[calc(-100%-var(--arrow-size))] rounded-[.3rem] bg-[--tooltip-color] p-2 text-center text-xs text-white transition-transform scale-0 [#grid:has(#gridItem:nth-child(1):hover)~&]:scale-100">LOOK HERE</div>
  <div class="absolute left-[calc(theme(padding.8)+theme(padding.4)+(theme(width.3)/2))] top-[calc(theme(padding.8)+theme(padding.4)-.25rem)] w-max origin-top -translate-x-1/2 translate-y-[calc(-1*var(--arrow-size))] border-[length:--arrow-size] border-transparent border-t-[--tooltip-color] transition-transform scale-0 [#grid:has(#gridItem:nth-child(1):hover)~&]:scale-100"></div>

  <div class="absolute left-[calc(theme(padding.8)+theme(padding.4)+((100%-(theme(padding.4)*2)-(theme(padding.8)*2)-(theme(gap.2)*2))/3)+theme(gap.2)+(theme(width.3)/2))] top-[calc(theme(padding.8)+theme(padding.4)-.25rem)] w-max origin-bottom -translate-x-1/2 translate-y-[calc(-100%-var(--arrow-size))] rounded-[.3rem] bg-[--tooltip-color] p-2 text-center text-xs text-white transition-transform scale-0 [#grid:has(#gridItem:nth-child(2):hover)~&]:scale-100">LOOK HERE</div>
  <div class="absolute left-[calc(theme(padding.8)+theme(padding.4)+((100%-(theme(padding.4)*2)-(theme(padding.8)*2)-(theme(gap.2)*2))/3)+theme(gap.2)+(theme(width.3)/2))] top-[calc(theme(padding.8)+theme(padding.4)-.25rem)] w-max origin-top -translate-x-1/2 translate-y-[calc(-1*var(--arrow-size))] border-[length:--arrow-size] border-transparent border-t-[--tooltip-color] transition-transform scale-0 [#grid:has(#gridItem:nth-child(2):hover)~&]:scale-100"></div>

  <div class="absolute left-[calc(theme(padding.8)+theme(padding.4)+((100%-(theme(padding.4)*2)-(theme(padding.8)*2)-(theme(gap.2)*2))*2/3)+(theme(gap.2)*2)+(theme(width.3)/2))] top-[calc(theme(padding.8)+theme(padding.4)-.25rem)] w-max origin-bottom -translate-x-1/2 translate-y-[calc(-100%-var(--arrow-size))] rounded-[.3rem] bg-[--tooltip-color] p-2 text-center text-xs text-white transition-transform scale-0 [#grid:has(#gridItem:nth-child(3):hover)~&]:scale-100">LOOK HERE</div>
  <div class="absolute left-[calc(theme(padding.8)+theme(padding.4)+((100%-(theme(padding.4)*2)-(theme(padding.8)*2)-(theme(gap.2)*2))*2/3)+(theme(gap.2)*2)+(theme(width.3)/2))] top-[calc(theme(padding.8)+theme(padding.4)-.25rem)] w-max origin-top -translate-x-1/2 translate-y-[calc(-1*var(--arrow-size))] border-[length:--arrow-size] border-transparent border-t-[--tooltip-color] transition-transform scale-0 [#grid:has(#gridItem:nth-child(3):hover)~&]:scale-100"></div>
</div>

This serves as a basic demonstration of how it can function, but keep these points in mind:

  • The positioning of the tooltips may be fragile and might require JavaScript for proper alignment.
  • Consider the accessibility of the tooltips, following guidelines like WCAG 2.2 1.4.13.
  • The connection between tooltip content and its parent's data-tooltip element needs attention. Using JavaScript could help maintain synchronization.
  • Use of :has() is relatively new and not fully supported by all browsers, so fallback options using JavaScript should be considered.

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

List within a list that is only displayed if a specific condition is

Looking for help with CSS styling to make an embedded list display as a contiguous list in the final output. Here's the scenario: <ol> <li>Coffee <! -- embedded unordered or ordered list - begin --> <ul> <li>Ap ...

What is the best way to assign a numeric variable to the value attribute of an HTML form input?

Is there a way to assign a numeric value to the value attribute of an HTML form input element using a variable? I understand that the value attribute automatically gets converted to a string. How can I make sure the variable is read before being converted? ...

Offset & Centralized Container using Bootstrap

I am currently attempting to center the header and input field using bootstrap. Unfortunately, I am facing an issue where I cannot get the input field to align properly with the header. If anyone has an alternative method for achieving this alignment, I ...

Struggling to align multiple col-md-10 divs in the center without success

I need help centering a group of articles within a container. I've been using the helper class center-block along with the CSS property float: none; on the container, but it hasn't been working to center the articles. I also tried placing center- ...

The dropdown menu with Bootstrap's <li> sub menu is not expanding as expected

<li class="dropdown"> <a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">Utilitas<span class="c ...

How can I make an image tag perfectly fit a CSS grid cell without using object-fit?

In this Vue component, the parent element displays 8 instances of these components in a 2x4 grid layout. The issue arises when the image within each component is larger than its container, causing it to not shrink to fit while maintaining the aspect ratio ...

What is the best method for eliminating empty links (i.e. empty tags with an href="x") from an HTML source code

There are empty links found on a webpage that I have pinpointed, and now I am looking to utilize JavaScript to eliminate them. The text is enclosed within anchor tags. ...

Utilizing "beneath the scroll" on a bootstrap 5.1 webpage

I have a website built with Bootstrap and I am looking to implement a "below the flow" positioning for the footer, preferably using CSS. I have been referring to it as "below the fold," although I'm not sure if that is the correct terminology to use. ...

The art of replacing material-ui styles with styled components

As a newcomer to UI material design, I am eager to create my own customized Button Component using styled-components. I am facing a challenge in overriding the CSS based on different button variations such as "primary" or "secondary". You can find my cod ...

Assign a "margin-bottom" value of "0" to all elements in the final row of a responsive layout

I'm currently working on a Bootstrap responsive template design where I have multiple items arranged in rows. The number of items per row and the number of rows vary depending on the screen resolution. <div class="row"> <div class="col-xs- ...

CSS, Assistance in incorporating a sub menu into a drop-down menu

I'm looking to enhance my drop down menu by adding a sub menu. Specifically, I would like to create a sub menu for Item 3 in the HTML below. How can I achieve this? Thank you, Below is my CSS code: .nav_menu { width:100%; background-color:# ...

Is there a way to distinguish the HTML Elements from the CSS elements and retrieve them individually?

I am working on achieving a specific task and this JS fiddle represents one part of it. Check out the JS Fiddle here alert($("#content")[0].outerHTML); When executed, this code returns the following DOM structure: <div id="content"> <div ...

Step-by-step guide to vertically centering a single column in a two-column grid layout using Materialize CSS

I am trying to create a layout with two columns, where text is on the left side and a large image is on the right. I am utilizing col s12 l6 to ensure that these elements stack on top of each other on smaller screens. In the design, I need the text on the ...

ways to dynamically modify the color scheme using tailwind

My goal is to retrieve a hex color code from the database and apply that color to a div element. I have attempted to use bg-[${color}] as the className, but unfortunately the color is not being applied. How can we dynamically change the color in tailwind ...

linking a div within a navigation bar

I have implemented a template from bootstrap. Here is the navigation bar where you can find the about section. Inside it, there is a bootstrap button: <button type="button" class="btn btn-light">Light</button> When I click on this button, the ...

Modifying canvas border colors using AngularJS

Currently, I am in the process of learning AngularJS and have developed a website that includes a canvas element. My main objective is to change the border color after clicking on a checkbox. Here is the code snippet for canvas.html : <!DOCTYPE html&g ...

button that smooth slides out using jquery

Here is the source code for a jQuery slideout: jQuery(function($) { $('#slideClick').toggle(function() { $(this).parent().animate({left:'0px'}, {queue:false, duration: 500}); }, function() { $(t ...

Having trouble with video playback in the HTML5 media player on Codeigniter platform

I'm attempting to incorporate a video into my web application using the HTML5 media player within the Codeigniter framework. Below is the code snippet I have been working on: public function view($settings, Screen $screen) { ...

An issue has occurred in Safari/IE because Error x[i].getElementsByTagName("image")[1] is not defined

I have been struggling to incorporate an RSS feed into my HTML page. It seems to function properly on iPad, iPhone, and Chrome, but encounters issues with Internet Explorer and Safari. Specifically, I am receiving an error message that states: x[i].getEle ...

What is the process of utilizing a <li> for a hover selector in jquery?

I've encountered an issue with a list that I am trying to modify its content when hovering over them. Despite using the id as a selector for triggering the hover function, all list items change color instead of just the intended one. The challenge lie ...