1

I try to use the approach for showing an HTML tooltip. I have already for some adaptions of the solution especially since the size didn't fit in properly. The working solution looks like

sup.footnote::after {
  content: attr(data-count);
}
sup.footnote:hover {
  cursor: help;
  position: relative;
}
sup.footnote cite {
  display: none;
}
sup.footnote:hover cite {
  display: inline-block;
  position: absolute;
  top: 0px;
  left: -50%;
  width: 250px;
  background: #f0f0f0 no-repeat 100% 5%;
  border: #c0c0c0 1px dotted;
  border-radius: 8px;
  margin: 10px;
  padding: 8px;
  overflow: hidden;
}
sup.footnote cite a {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  display: block;
}
<p> another line </p>
<p>
  Dies ist ein Beispiel für eine Fußnote in HTML
  <sup class="footnote" data-count="1">
    <cite>
      <span> [Sample span]</span>&nbsp;with some explanation:
      <a lang="en" href="http://test.gv.at/test.pdf">
        http://test.gv.at/test/test/abde4/iabcde/009909asdf/vandsfk23/verylong/gghhy.pdf
      </a>
    </cite>
  </sup>
</p>
<p> another line </p>

In general this works quite good. My problem here is that depending on the entry of the mouse cursor the overlapping is too less. This causes the problem when trying to move the cursor into the displayed box the box vanishes before the cursor entered it.

In the demo the effect isn't that serious (try it from the left top corner). But in real life the hiding sux.

I tried several solutions with opacity. This one looks quite promising but I couldn't stich them together properly. I guess that's due to the fact that display:none is used.

Any idea what kind of CSS I should use to delay the hiding?

LeO
  • 4,238
  • 4
  • 48
  • 88

2 Answers2

1

Scroll down for the final solution. But some notes about delay: You could use transition with duration and evtl. delay to control the display time. But note that you can't animate display. Therefore you can use opacity. For example:

cite {
  display: inline-block;
  opacity: 0;
  transition: opacity 300ms step-end 300ms;
}
sup:hover cite {
  opacity: 1
}

But in this case, you tooltip always exists in DOM. Invisible by opacity but display 'none' would remove it from DOM. (Test it with dev tools.)

You could use the transition to delay the tooltip, but if you want to use "display" you might think a bit differently.

Solution

Wrap the tooltip element and overlap with the sup element to avoid gaps between (keep hover).

sup.footnote::after {
  content: attr(data-count);
}

sup.footnote {
  cursor: help;
  position: relative;
}

sup.footnote .tooltip {
  display: none;
  position: absolute;
  top: 0;
  left: 0;

  /* Toggle next line to see the hit box. */
  /* background-color: rgba(255, 255, 0, 0.5); */
}

sup.footnote:hover .tooltip {
  display: inline-block;
}

sup.footnote .tooltip cite {
  display: inline-block;
  background: #f0f0f0 no-repeat 100% 5%;
  border: #c0c0c0 1px dotted;
  border-radius: 8px;
  margin: 10px;
  padding: 8px;
  width: 250px;
  overflow: hidden;
}

sup.footnote .tooltip cite a {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  display: block;
}
<p>another line</p>
<p>
  Dies ist ein Beispiel für eine Fußnote in HTML
  <sup class="footnote" data-count="1">
    <span class="tooltip">
      <cite>
        <span> [Sample span]</span>&nbsp;with some explanation:
        <a lang="en" href="http://test.gv.at/test.pdf">
          http://test.gv.at/test/test/abde4/iabcde/009909asdf/vandsfk23/verylong/gghhy.pdf
        </a>
      </cite>
    </span>
  </sup>
</p>
<p>another line</p>

demo

See hit box:

debug

You could also play with some padding around your hover element sup to reach the similar solution (overlapping / remove gap between elements).

Domske
  • 4,795
  • 2
  • 20
  • 35
  • Thx for detailed analysis and proposals. I need to check next week what fits for my use case. Especially since I have trailing text as well (need to check for paddings). – LeO Jun 24 '22 at 13:17
1

You can achieve this with paddings in a pseudo class.

sup.footnote {
  position: relative;
}
sup.footnote::before {
  content: '';
  height: 100%;
  width: 100%;
  position: absolute;
  padding-right: 0.5em;
  padding-bottom: 0.5em;
}

/* new style */
sup.footnote {
  position: relative;
}

sup.footnote::after {
  content: attr(data-count);
}


/* new style */
sup.footnote::before {
  content: '';
  height: 100%;
  width: 100%;
  position: absolute;
  padding-right: 0.5em;
  padding-bottom: 0.5em;
}

sup.footnote:hover {
  cursor: help;
  position: relative;
}

sup.footnote cite {
  display: none;
}

sup.footnote:hover cite {
  display: inline-block;
  position: absolute;
  top: 0px;
  left: -20%; /* changed */
  width: 250px;
  background: #f0f0f0 no-repeat 100% 5%;
  border: #c0c0c0 1px dotted;
  border-radius: 8px;
  margin: 10px;
  padding: 8px;
  overflow: hidden;
}

sup.footnote cite a {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  display: block;
}
<p>another line</p>
<p>
  Dies ist ein Beispiel für eine Fußnote in HTML
  <sup class="footnote" data-count="1">
        <cite>
          <span> [Sample span]</span>&nbsp;with some explanation:
          <a lang="en" href="http://test.gv.at/test.pdf">
            http://test.gv.at/test/test/abde4/iabcde/009909asdf/vandsfk23/verylong/gghhy.pdf
          </a>
        </cite>
      </sup> Dies ist ein Beispiel für eine Fußnote in HTML
</p>
<p>another line</p>
Anton
  • 8,058
  • 1
  • 9
  • 27
  • The problem with the padding is that there might be regular text afterwards. And this shifts as well to the right. By itself it looks great but it has an undesired side effect. – LeO Jun 24 '22 at 12:50
  • The paddings would be my preferred solution. With introduction of a new Tag I have other problems in my use case. – LeO Jun 24 '22 at 12:59
  • In this case, it's better to create a second pseudo-class with padding and add `position: relative;` to the parent element. I've updated snippet. – Anton Jun 24 '22 at 13:09
  • 1
    Thx. This woks the way itts expected. I use this solution since it won't introduce no additional HTML Tags. – LeO Jun 27 '22 at 06:42