6

http://codepen.io/anon/pen/wBaGgW

I currently have what a list of items and then a button next to them on the right: enter image description here

The tooltip must appear on focus and the tooltip must appear on hover - this works but the problem is that when an item is focused (after clicking on it) - the following item cannot be accessed via mouse (because preceeding is item focused!):

enter image description here

The tooltip must disappear when the mouse over the tooltip itself, but the focus is forcing it stay.

The test-case is here: http://codepen.io/anon/pen/wBaGgW

can anyone offer a solution that does not have any javascript? Also, the html markup cannot be changed too much. Minimal changes to HTML are OK. Just trying to prevent too much as I'll most likely need to compensate other parts of the application to fit the html changes.

Here shows the tooltip:

button:hover>.tooltip,
button:focus>.tooltip,
button:active>.tooltip {
    visibility: visible;
    opacity: 1;
}

I can hide the tooltip doing the following:

button:focus>.tooltip:hover {
    visibility: hidden;
    opacity: 0;
}

But that causes a crazy flickering effect as the mouse moves within the area in which the tooltip would appear.

Keep in mind the restrictions:

  • No JavaScript
  • Compatibility with IE8+ (please note, the tooltip css is coming from our global module, and I dont have direct access to change it, I am working on a separate module that I can of course override because my css loads after the global css does)
  • Tooltip must appear below (unfortunately)
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
docodemore
  • 1,074
  • 9
  • 19
  • How about making it hover to the right of the object? – LOTUSMS Nov 24 '14 at 20:22
  • @LOTUSMS I can't do that because it goes against business requirements. It also adds complexity (responsive web, tooltip not visible as its off screen, etc) – docodemore Nov 24 '14 at 20:24
  • anyway I see in his original CSS translateX right on the tooltip, which is NOT supported by IE8 (his requirements)... – qdev Nov 24 '14 at 20:25
  • 1
    The `pointer-events` css property would also let clicks go through the tooltip but it is not supported by IE8 and IE9. – Marcelo Nov 24 '14 at 20:26
  • The .tooltip class is coming from out global css, it is nothing I can change (I can override it though) - while that goes against business requirements, I can't say with certainty why it is there or if they provided some workaround. I am working on a different module. – docodemore Nov 24 '14 at 20:26
  • well, no javascript means no clickthough option. No right-placement, and compatible with IE8! Good luck. – LOTUSMS Nov 24 '14 at 20:29
  • Trust me, I've been working on this for days now :D Finally breaking down on SO to see if anyone can offer some help. So good luck isn't what I need here. I need much more than that!!! :D – docodemore Nov 24 '14 at 20:29
  • why dont you place it on the left side then? blocking the element currently being hovered upon. – Anubhav Nov 24 '14 at 20:33
  • @Anubhav That goes against biz requirements. Unfortunately. Also there are other places where these tooltips appear, all around I think left could work, but biz requirements won't let me do this. If it comes down to "no tooltips unless left" then I really, really, have to back that up with all the proof I can. – docodemore Nov 24 '14 at 20:35
  • 1
    There's no good reason for downvoting my question because it's too challenging or restrictive. I provided codepen, code, and the entire scenario. I spent quite some time formulating the question. – docodemore Nov 24 '14 at 20:40
  • 2
    I have a half-fix. If a button is focused, and the user clicks the tooltip, it is dismissed. It is also dismissed on hover if the user has nothing focused. http://codepen.io/anon/pen/azONYP – Marcelo Nov 24 '14 at 20:49
  • the requirements & constraints are making this impossible from my pov (no JS, no HTML changes, no replacement of the tooltip, IE8 compatibility, etc). – qdev Nov 24 '14 at 20:50
  • @qdev : html changes are allowed! Just minimal (because this is a tiny test case, you must imagine I'm actually working on something much more complex, but this is one of the mechanisms. So changing the HTML can have repurcussions, but minor changes are OK!!!) – docodemore Nov 24 '14 at 20:51
  • ok, but button:focus will always fight back to tooltip:hover (that's why flickering exists) and tooltip will still be visible when mouseover from tooltip anyway. JS it's a must in this case (to add / remove classes). – qdev Nov 24 '14 at 20:55
  • @qdev: if I can definitively prove that, then I may be able to get around the biz requirement. However, I dont want to jump the gun so I'll keep this question open for a few days first. I'll also be working on it and trying to avoid JS. I'd rather be safe than sorry. – docodemore Nov 24 '14 at 20:57
  • The crux of the problem is that, without using something that can clear focus or add/remove classes (i.e. Javascript or CSS properties not supported in IE8), there is no good way to PERMANENTLY override the rule which shows the tooltip on focus. – Marcelo Nov 24 '14 at 21:03
  • @Marcelo - what CSS properties that are not supported in IE8? I may be able to at least start with them, then conclude that JS is needed for IE8. I just have to be without a shadow of doubt that IE8 needs JS. and right now I am still optimistic. – docodemore Nov 24 '14 at 21:13
  • I ended up using JS, am still going to keep this question open to see if there are alternatives. thanks everyone for participating. – docodemore Nov 25 '14 at 22:34
  • @Marcelo: i've took your example for adding mine to it (button:focus + .tooltip:hover {background: none; border: none; -webkit-box-shadow: none; -moz-box-shadow: none;box-shadow: none;text-indent: -9999px;}), http://codepen.io/anon/pen/MYwrEK, it looks like it's ok now, isn't it? – Eran.E Nov 26 '14 at 11:40
  • @Eran I tried to combine them before, too. Unfortunately, the combo still has the issue that when a button has focus, you can't get the tooltip to show up on hover for the button directly below it. – Marcelo Nov 26 '14 at 14:32
  • @Marcelo: ok, i guess it's the only issue remains unresolved... – Eran.E Nov 26 '14 at 15:12

5 Answers5

5

With those restrictions, I don't know of any way to resolve your issue perfectly.

As a workaround, you can change the tooltip to be a sibling of the button, instead of a child and use the CSS adjacent sibling selector. This makes it so that when a user clicks the tooltip, it loses focus from the button and the tooltip is hidden. This will require you to fix the position of the tooltip a little (I used margin-top as a quick fix).

Code

button:hover + .tooltip,
button:focus + .tooltip,
button:active + .tooltip {
    visibility: visible;
    opacity: 1;
    margin-top:20px;
}
<ul>
  <li><span>Lorem Ipsum Dlar Set</span>
    <button>X
    </button>
    <span class="tooltip">Hello ToolTip
    </span>
  </li>
  ...
</ul>

Live example: http://codepen.io/anon/pen/azONYP

MaxiGui
  • 6,190
  • 4
  • 16
  • 33
Marcelo
  • 4,395
  • 1
  • 18
  • 30
3

Based my answer on this: Answer

html <button tooltip="Tooltip text">Test</buttoN>

css

[tooltip]:before {            
    position : absolute;
    content : attr(tooltip);
    pacity : 0;
}

[tooltip]:hover:before {        
    opacity : 1;
    margin-top:10px;
}

Here is the Fiddle

Update Fiddle now with focus. Added pointer event: none;

IE8 YEP YEP

No Javascript YEP

Must be below YEP

Community
  • 1
  • 1
Persijn
  • 14,624
  • 3
  • 43
  • 72
  • 1
    thanks for posting.. sadly there is also LOTS of flickering going on, if you move the mouse slowly down, flicker galore maybe due overlap. Also mouse to the left of the button, flickering galore. – docodemore Jan 09 '15 at 17:06
  • I tested in **firefox 34.0** and **chrome 39.** No flickering here. Im on linux so i cant test on internet explorer. What are you using? – Persijn Jan 09 '15 at 17:13
  • chrome 39, windows 7x64 - could try it on my mac. but at any rate, we have strict requirements, has to be compatible with windows and mac, chrome, firefox, safari, ie8-11(argh) – docodemore Jan 09 '15 at 17:22
  • The tooltips do not appear on focus in this solution. That is what makes this problem difficult. – Marcelo Jan 09 '15 at 20:13
  • Updated with focus. I thought that was a given when i have hover. Anyways added poiunter-events: none on the hvoer so there should be no flickering – Persijn Jan 09 '15 at 22:33
  • Thanks again for your post. It is a decent solution and could be used with JS fallback but it doesnt solve the problem. pointer-events: none does not work in IE8, – docodemore Jan 09 '15 at 22:41
  • Dident check the compatablity for IE8. and its not something i can test because i dont see the flicker your talking about even in IE on windows. If i cant see the problem i cant code css to fix it. – Persijn Jan 09 '15 at 23:42
  • thanks, here you can check compatibility http://caniuse.com/#search=.%20pointer-events - as far as flickers, ive been able to repeat that on mac os x and windows, chrome, firefox and ie11. – docodemore Jan 10 '15 at 03:33
2

when mouse leave the tooltip, it's needs to be removed completely? (like removing the ":focus")...beacuse if it's allow for the tooltip to be visible again after mouse leave so you can use:

button:focus>.tooltip:hover
{
  background: none;
  border: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  text-indent: -9999px;
}

codepen: http://codepen.io/anon/pen/OPVNaW

Eran.E
  • 940
  • 5
  • 8
  • Very close! But it's not working 100% right. The tooltip has to re-appear as well. This seems very promising though. – docodemore Nov 24 '14 at 21:35
  • it is re-appear, did you mean it's not needs to re-appear (removed completely) ? – Eran.E Nov 24 '14 at 21:56
  • @Eran I think what he means, is that when you focus on a button then you hover over its tooltip, it hides and it shows the button underneath, but that second button which is now showing does not show its tooltip on hover. – Marcelo Nov 24 '14 at 22:00
0

Try refactoring your CSS to something like this:

button:hover>.tooltip,
button:active>.tooltip {
    visibility: visible;
    opacity: 1;
      
}
button:focus>.tooltip {
    visibility: visible;
    opacity: 1;
    outline: none;
}
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
fictus
  • 286
  • 2
  • 5
  • Thanks for trying! That doesn't work (not even in chrome) - See the codepen: http://codepen.io/anon/pen/wBaGgW - I'll see what outline does (will check it out) – docodemore Nov 24 '14 at 20:37
  • You're correct. It works in Firefox but your question implies IE8+. Back to the drawing board... – fictus Nov 24 '14 at 20:45
0

Use <a> instead of buttons and style them as buttons.

/* `border-box`... ALL THE THINGS! */
html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  margin: 64px auto;
  text-align: center;
  font-size: 100%;
  max-width: 640px;
  width: 94%;
}

a:hover {
  text-decoration: none;
}

header,
.demo,
.demo p {
  margin: 4em 0;
  text-align: center;
}

/**
 * Tooltip Styles
 */

/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
  position: relative;
  z-index: 2;
  cursor: pointer;
}

/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
  visibility: hidden;
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
  opacity: 0;
  pointer-events: none;
}

/* Position tooltip above the element */
[data-tooltip]:before {
  position: absolute;
  top: 150%;
  left: 50%;
  margin-top: 5px;
  margin-left: -80px;
  padding: 7px;
  width: 160px;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
  background-color: #000;
  background-color: hsla(0, 0%, 20%, 0.9);
  color: #fff;
  content: attr(data-tooltip);
  text-align: center;
  font-size: 14px;
  line-height: 1.2;
}

/* Triangle hack to make tooltip look like a speech bubble */
[data-tooltip]:after {
  position: absolute;
  top: 150%;
  left: 50%;
  margin-left: -5px;
  width: 0;
  border-bottom: 5px solid #000;
  border-bottom: 5px solid hsla(0, 0%, 20%, 0.9);
  border-right: 5px solid transparent;
  border-left: 5px solid transparent;
  content: " ";
  font-size: 0;
  line-height: 0;
}

/* Show tooltip content on hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
  visibility: visible;
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
  opacity: 1;
}


/* Show tooltip content on focus */
[data-tooltip]:focus:before,
[data-tooltip]:focus:after {
  visibility: visible;
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
  filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
  opacity: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<h1>CSS Simple Tooltip</h1>


<div class="demo">
  <p><a href="#" class="btn btn-primary" data-tooltip="I’m the tooltip text.">I’m a button with a tooltip</a></p>
</div>
Worthy7
  • 1,455
  • 15
  • 28