CSS-Only tooltip that adjusts size dynamically

Having trouble getting a tooltip to work exactly as desired? I need to implement tooltips for the contents of dynamically built HTML tables. It must be done with CSS only, without relying on JavaScript or events for performance reasons. The challenge is having the tooltip always appear above and centered in the table data (TD) when hovered over, complete with an arrow below it. While I've managed to make this work, the issue arises when the tooltip content spans multiple lines, causing its height to increase and shift downwards from the center of the TD. How can I ensure that the tooltip remains consistently positioned above the TD by the same amount regardless of its height?

An example of the HTML used for the TD:

<td>
    <div class="tooltip">
       <span>My Tooltip Text</span>
       Text in the TD
    </div>
</td>

The CSS code being utilized:

.tooltip span
{
    display: none;
    color: #000;
    text-decoration: none;
}

.tooltip:hover span
{
    display: block;
    position: absolute;
    background-color: #292929;
    margin: -2.8em -1em;
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    white-space: pre-line;
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    text-align: left;
}

.tooltip:hover span::after
{
    top: 100%;
    left: 50%;
    border: solid transparent;
    content: '';
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
    border-top: 8px solid #292929;
    border-right: 8px solid transparent;
    border-left: 8px solid transparent;
    border-width: 10px;
    margin-left: -40px;
}

Answer №1

for instance

@keyframes heartbeat {
  0% {
    opacity: .5;
    transform: translate(-50%, -100%) scale(.5); 
  }
  50%{
   opacity: 1;
   transform: translate(-50%, -100%) scale(1.2);
  }
  100% {
     transform: translate(-50%, -100%) scale(1);
  }
}

.tooltip span
{
    color: #000;
    text-decoration: none;
    transform: translate(-50%, -100%) scale(.5); 
    position: absolute;
    background-color: #292929;
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 400;
    text-align: center;
    box-shadow: 0 0 4px 2px rgba(0,0,0,.2);
    width: max-content;
    max-width: 200px;
    top:0;
    left:50%;
    margin-top: -18px;
    visibility: hidden;
    opacity: 0;
}
.tooltip:hover{
  position: relative;
}
.tooltip:hover span
{
    opacity: 1;
    visibility: visible;
    transform: translate(-50%, -100%) scale(1);
    animation: heartbeat 1s;
}

.tooltip:hover span::after
{
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: solid transparent;
    content: '';
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
    border-top: 8px solid #292929;
    border-right: 8px solid transparent;
    border-left: 8px solid transparent;
    border-width: 10px;
}
<br>
<br>

<br>
<br>

<table border="1">
  <tr>
    <td>Hello</td>
    <td>
      <div class="tooltip">
         <span>My Tooltip Text</span>
         Cool description
      </div>
    </td>
    <td>
       <div class="tooltip">
         <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>
         Lengthy content
      </div>
    </td>
  </tr>
</table>

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

Using the justify-content:space-between property does not seem to be effective in a Nextjs

In my Next.js app, I am facing an issue with the justify-content: space-between; property not working as expected for two divs inside a container. However, justify-content: center; works fine. Interestingly, when I use the same code in an index.html file, ...

Automatically adjust column sizes using Bootstrap's responsive width feature

While I have a feeling this question might have been asked before, my search has turned up no similar issues. I am currently working on styling a menu bar using a standard bootstrap row and columns setup (flexbox). The menu is dynamically generated by Wor ...

"Struggling to horizontally center a div with a fixed width in CSS - what am I

Whenever I try to use it on my browser, the content shifts to the left and I can't seem to center it using text-align or flexbox. The issue gets resolved when I remove the width property, but I really need it to have that paragraph-style look I'm ...

One way to retrieve form data in a controller when using serialization in an AJAX request is by accessing the

When using (#myForm).serialaize() in an ajax call for form submission, how can the form data be accessed in the controller? What does the representation of form data look like in the controller? $('#myForm').on('submit', function(e){ ...

Add a script tag to every image in my HTML at once using C#

Managing a site with a thousand images can be overwhelming, especially when trying to add a script tag like <%=Settings.MyDomain%> to each one. I'm looking for a more efficient way to accomplish this task without spending hours on manual labor. ...

Tips for adjusting the width of table columns when dealing with browser fields

When working with a table field, I am allowing input as a file. <table> <tr> <th> <input type="file" name="icon" id="icon" /> </th> </tr> </table> The file input field is recommended to have a width of 242-246px ...

Activate the button with a tap of the space bar on a webpage

Is it possible to have a button on a webpage triggered both by clicking with the mouse and hitting the spacebar? HTML <div class="col-12 button small-button"> <a onclick="generator()"> <img src="icones%20web%20projeto.p ...

Tips for rearranging the icon placement of the parent menu item on Bootstrap Navigation Bar for mobile display

I need assistance with adjusting the position of the bootstrap navigation menu bar parent menu icon, in mobile view. Currently, the menu header displays a right-arrow-down icon at the end of the header name as shown below. https://i.sstatic.net/kv4T2.png ...

Adjust the spacing between the title and other elements in an R Shiny application

How do I modify the font and size of a title, as well as add some spacing between the title and other elements? navbar <- navbarPage( Title = "Intervals", tabPanel("Data Import", sidebarLayout(sidebarPanel(fi ...

How to remove an event listener when e.target points to a hyperlink

element, I encountered an issue using a slideshow component sourced from our components library. This component receives swipe events from a utility function that is initialized upon mounting. The problem arose while testing on a mobile phone - tapping a ...

Positioning and resizing elements based on varying screen sizes and levels of zoom

I recently designed a basic webpage using HTML with two main elements - a chat window for user interaction and a chart.js plot. Here is a snippet of the HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

Attempting to adjust table size in MPDF to fill the available height

I'm currently utilizing MPDF in order to create a PDF document from an HTML page that includes a table. My goal is to have the table expand to fill up the remaining space on the page. Here is the specific table I am working with: I want the final el ...

Does HTML elements come with a pre-set background color of white or transparency by default?

Feeling a bit confused about a straightforward issue. Can someone clarify for me - What is the original background color of HTML elements? Is it white by default or transparent? ...

Creating a login form using HTML, PHP, and MySQL: A step-by-step guide

UPDATE [13.10.16] After revisiting a previous question of mine on stackoverflow that has low reputation, I realized the reasons behind it and decided to update it to make it more relevant. When I initially asked this question, I was attempting to create ...

Sending checkbox selections to JavaScript

I am currently exploring Angular by working on a chat application project. My main focus right now is on passing checkbox values to my JS code. I have included snippets of the code below. The issue I'm encountering is that I can't seem to retriev ...

Selecting the first child within a table using the first-child

I recently set up a login portal page for three different websites. The layout consists of three separate login portals displayed neatly in two columns within a table named "loginPortals". I am currently working on styling the page using CSS, aiming to enh ...

Enhance Bootstrap: adjust button width based on fluid text width

Incorporating Bootstrap 5 framework, I have designed a card that showcases brand information along with a navigation button on the right side. https://i.sstatic.net/25QVr.png When the description is brief, the button aligns perfectly in a single line. Ho ...

Access an HTML file in Text Edit on a Mac directly from a web browser

Is there a way to utilize Javascript or another appropriate script to open an HTML file in Text Edit on my Mac? I have created a local web page using Text Edit that has different tabs linking to other Text Edit files within the page. I am looking for a m ...

The issue of nested tabs in Bootstrap 3 causing problems with sliders within the main tab has

Hello, I am utilizing Bootstrap 3 tabs. Within the nav-tabs, there are three tab-panes: the first tab contains text only, the second tab includes a slider, and the third tab has nested tabs. However, I encountered an issue where the slider in the nested t ...

Remove padding in Twitter Bootstrap table cells

One of my current projects requires making a button that fills the entire width and height of the TD element. I have attempted setting the normal .btn-warning with -Xpx!important, but it did not produce the desired result. Here is what I currently have: ...