Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tooltips for mobile browsers

I currently set the title attribute of some HTML if I want to provide more information:

<p>An <span class="more_info" title="also called an underscore">underline</span> character is used here</p> 

Then in CSS:

.more_info {   border-bottom: 1px dotted; } 

Works very nice, visual indicator to move the mouse over and then a little popup with more information. But on mobile browsers, I don't get that tooltip. title attributes don't seem to have an effect. What's the proper way to give more information on a piece of text in a mobile browser? Same as above but use Javascript to listen for a click and then display a tooltip-looking dialog? Is there any native mechanism?

like image 638
at. Avatar asked Sep 21 '12 19:09

at.


People also ask

Do tooltips work on mobile?

Mobile tooltips are small in-app messages attached to a specific UI element or place on the screen. They're typically (but not exclusively) text-based, and are used to provide highly contextual guidance, highlight key features, or alert about new updates during app usage.

How do I show tooltip on touch devices?

Tap on an Icon and show the additional Information. If the element with the tooltip provides another click action (e.g an anchor-link), this won't work. A tooltip is triggered by tapping and holding an item. Keep the tooltip displayed as long as the user continues to hold the element.

How do I add tooltips to my website?

HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .

When should you not use tooltips?

1. Don't use tooltips for information that is vital to task completion. Users shouldn't need to find a tooltip in order to complete their task. Tooltips are best when they provide additional explanation for a form field unfamiliar to some users or reasoning for what may seem like an unusual request.


2 Answers

You can fake the title tooltip behavior with Javascript. When you click/tab on an element with a title attribute, a child element with the title text will be appended. Click again and it gets removed.

Javascript (done with jQuery):

$("span[title]").click(function () {   var $title = $(this).find(".title");   if (!$title.length) {     $(this).append('<span class="title">' + $(this).attr("title") + '</span>');   } else {     $title.remove();   } });​ 

CSS:

.more_info {   border-bottom: 1px dotted;   position: relative; }  .more_info .title {   position: absolute;   top: 20px;   background: silver;   padding: 4px;   left: 0;   white-space: nowrap; } 

Demo: http://jsfiddle.net/xaAN3/

like image 117
flavaflo Avatar answered Sep 29 '22 01:09

flavaflo


Here is a CSS only solution. (Similar to @Jamie Pate 's answer, but without the JavaScript.)

We can use the pseudo class :hover, but I'm not sure all mobile browsers apply these styles when the element is tapped. I'm using pseudo class :focus because I'm guessing it's safer. However, when using pseudo class :focus we need to add tabindex="0" to elements that don't have a focus state intrinsically.

I'm using 2 @media queries to ensure all mobile devices are targeted. The (pointer: coarse) query will target any device that the primary input method is something "coarse", like a finger. And the (hover: none) query will target any device that the primary pointing system can't hover.

This snippet is all that's needed:

@media (pointer: coarse), (hover: none) {       [title] {         position: relative;         display: inline-flex;         justify-content: center;       }       [title]:focus::after {         content: attr(title);         position: absolute;         top: 90%;         color: #000;         background-color: #fff;         border: 1px solid;         width: fit-content;         padding: 3px;       }     } 

/*Semantic Styling*/ body {   display: grid;   place-items: center;   text-align: center;   height: 100vh; }  a {   height: 40px;   width: 200px;   background: #fa4766;   color: #fff;   text-decoration: none;   padding: 10px;   box-sizing: border-box;   border-radius: 10px; }  /*Functional Styling*/ @media (pointer: coarse), (hover: none) {   [title] {     position: relative;     display: flex;     justify-content: center;   }   [title]:focus::after {     content: attr(title);     position: absolute;     top: 90%;     color: #000;     background-color: #fff;     border: 1px solid;     width: fit-content;     padding: 3px;   }  }
<a title="this is the Title text" tabindex="0">Tag with Title</a>

Obviously, you'll need to open this on a mobile device to test it. Here is a Pen with the same code.

like image 37
H K Avatar answered Sep 29 '22 00:09

H K