Explain Codes LogoExplain Codes Logo

Html - how can I show tooltip ONLY when ellipsis is activated

html
responsive-design
performance
accessibility
Anton ShumikhinbyAnton Shumikhin·Sep 9, 2024
TLDR

To show a tooltip on hover solely when text is truncated with an ellipsis, utilize JavaScript. Test if the element's scrollWidth is larger than its clientWidth. If it is, we've got an overflow situation, and you should apply a title attribute allowing the tooltip to appear.

// JavaScript: The Secret Life of Walter `offsetWidth` & `scrollWidth` element.offsetWidth < element.scrollWidth ? element.title = element.textContent : element.removeAttribute('title');

An Ellipsis' Best Friend, CSS guarantees its display:

.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

Assign .ellipsis class to an element and run the JavaScript check on hover to reveal the tooltip only when required.

Job Description: Tooltip Management Specialist

jQuery can help us "hire" a Tooltip Management Specialist, detecting ellipsis with a mouseenter event:

// You had me at 'ready!' $(document).ready(function(){ $('.ellipsis').on('mouseenter', function() { // $this is not a dollar, it's an element! var $this = $(this); // If I had a penny for every text overflow... $this.attr('title', $this.offsetWidth < $this.scrollWidth ? $this.text() : ''); }); });

Tooltip Gym: Performance Optimization

Let's pump up the tooltip performance and give it a protein shake of optimization:

  • Cache that jQuery object as $this; let's not waste resources.
  • Tooltip behavior and positioning achieved through CSS pointer-events and position:relative.
  • Flex those tooltip muscles with box-shadow, border-radius, opacity, and visibility.

The Text That Couldn't Sit Still

Dynamic content is a hyperactive text, let's guide it:

  • Check for content overflow whenever new elements join the DOM "playground" or when the kids' content is updated
  • Dynamically apply and eliminate the title attribute depending on whether the textual "teenagers" need guidance.
  • Use a class selector from jQuery for efficient tooltip update disco across several elements.

Vanilla Sky: Clean JavaScript

For those staring at the Vanilla JavaScript sky, here's your snippet:

// forEach() - The JavaScript equivalent of Oprah. She gives out tooltips! document.querySelectorAll('.ellipsis').forEach(function(el) { el.addEventListener('mouseenter', function() { // Einstein would be proud: title relative to space and time(overflows)! if (this.offsetWidth < this.scrollWidth && !this.title) { this.title = this.innerText; } }); });

Accessibility: No Tooltip Left Behind

Tooltips can certainly enhance UX, but let's make sure to put accessibility first and foremost on our list:

  • Readable for all users, including those using screen readers
  • Implement aria-label or aria-labelledby for screen reader support while managing dynamic title attributes

Planet Compatibility and the Cross-Browser Safari

Ensure your solution passes the Compatibility Test of the browser savannah, including the elusive Internet Explorer:

  • Consistency in ellipsis and tooltip management on each platform

Race Against the Performance Clock

Aim for a minimal performance footprint – We wouldn't want to trip over our own tooltips:

  • Avoid "overweight" libraries or complex code that may hinder your page speed. Stay lean

Touchscreen Tango

We mustn't forget our fancy friends, the touchscreens, where hover events do not naturally occur. Slide into the touchstart event or apply other methods to tap into displaying tooltips on touch devices.