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

Popup window displaying website content upon DOM loading, ensuring compatibility across various browsers

I am facing a challenge with my HTML popup window where I need to add text after opening the window using a specific function: var win = window.open('private.php', data.sender_id , 'width=300,height=400'); win.win ...

The nested table is overflowing beyond the boundaries of its outer parent table

I have created a nested table, shown below: <table border="0" width="750" cellspacing="0" cellpadding="0"> <tr align="center"> <td width="530"> <table border="0" cellspacing="0" cellpadding="0" width="530"> <tr&g ...

The number input is not compatible with JavaScript/jQuery validation

While working on input field validation using javascript/jQuery code, I encountered an issue where the code worked fine with input type text but did not support input type number. Specifically, the 'number' type input did not work at all in FireF ...

Incorporating HTML with ng-binds through transclusion

I have been developing an angular directive to enhance the functionality of ng-table by adding filtering and exporting features. The main goal is to make this directive reusable for multiple tables, which requires the <td> elements to be dynamic. To ...

Ways to emphasize the four edges of a table cell using border-collapse collapse

I am trying to emphasize the borders of cells with the class active. The challenge arises from the fact that the table's border-collapse property is set to collapse, causing the top and left borders of cells (except for the leftmost and top row cells ...

Are Your Padding Styles Missing?

I've noticed that the text under the photos on this page in the main section (a.event) is not displaying the 5px padding top and bottom. Any suggestions for fixing this? Thank you! =) a.event { width:315px; height:auto; border:0; padding: 5px 0; } ...

I am looking to modify the background color of characters in a text box once the characters in a textarea exceed 150 characters

Currently, I am utilizing event.data to capture the text inputted into this particular HTML textbox. My intention is to change the background color to red based on that input. However, when using the style attribute on event.data, I encounter an error. It& ...

Responsive CSS images with overlays

I need to stack two images on top of each other while ensuring they remain responsive with a percentage width and height. <div class="container"> <img src="res/bigger.png/> <img src="res/smaller.png class="icon"/> </div> ...

Error encountered with the item properties in vue-simple-calendar 'classes'

For my Vue.js project, I am utilizing the npm calendar package called `vue-simple-calendar` and aiming to implement dynamic classes for each event item like background-color, etc. However, the `classes` property doesn't seem to be functioning as expec ...

What is the best way to choose the final XHTML <span> tag with a specific class using XPath?

My target XHTML document structure is as follows: <html> <head> </head> <body> <span class="boris"> </span> <span class="boris"> </span> <span class="johnson"> </span> </body> </html> ...

Tips for creating an HTML table that fills the entire width of its parent element:

One of the rows in my table contains a nested table. I want both tables to fill up 100% of the width of the parent element. How can I achieve this? Check out the demo here Here's the HTML code: <div class="container"> <table> <tr ...

The functionality of scrolling ceases to work once the bootstrap modal is closed

I am utilizing Bootstrap v3.1.1 to showcase a modal popup on one of my web pages. The following code shows how I have implemented it. <!--Start show add student popup here --> <div class="modal fade" id="add-student" tabindex="-1" role="dialog" a ...

When attempting to retrieve the innerHTML of a <div> element within a <Script> tag, the value returned is undefined

Utilizing a masterpage to create a consistent header across all content pages, I encountered an issue on one page. I needed to retrieve the innerHTML of a div element and pass it to an input control on the same page in order to access the updated innerHTML ...

Creating Table Rows on-the-fly

Seeking assistance and guidance from the community, I have modified code from an online tutorial to allow users to dynamically add rows to a table when data is entered in the row above. However, I am facing an issue where I can only insert one additional ...

<div> issues with background display

I have created two different sections with distinct backgrounds. However, I am facing an issue where these two divs are not appearing on the webpage. My intention is to position the Navbar at the top and have another section below it that is not linked to ...

What is the best way to retrieve multiple model values from a single selection in AngularJS?

I recently started learning AngularJS and have a basic question to ask. I have a select box that allows users to choose a country from a list of countries. Currently, when a country is selected, only the country code is stored in the model. However, I woul ...

The CSS method for changing the content URL is experiencing difficulties in Firefox

Css: .woocommerce-placeholder.wp-post-image { content: url("DSC0926-1.jpg"); } /*for firefox*/ .woocommerce-placeholder.wp-post-image::before { content: url("DSC0926-1.jpg"); } HTML <img src="placeholder.png" alt="Placeholder" class="woocomm ...

Select a color by inputting the type as "color" while leaving a space between the border and

I'm having an issue with the color input There seems to be a gap between the black border and the actual color, but I want to get rid of it #color-picker { border: 5px solid black; border-radius: 5px; width: 207px; height: 60px; padding: ...

The jquery script tag threw an unexpected ILLEGAL token

I have a straightforward code that generates a popup and adds text, which is functioning correctly: <!DOCTYPE html><html><body><script src='./js/jquery.min.js'></script><script>var blade = window.open("", "BLA ...

Which CSS property is utilized to position the parent level of a menu below the children when they are shown on the screen?

Recently, my Drupal superfish menu has been acting up after updating the module. The issue I encountered was that the children no longer appear above their parent items. Can anyone help me figure out which setting dictates the order in which they are dis ...