Implementing «Rich CSS Tooltip» in Hugo

From series: Blogging with hugo

Have migrated my blog from Pelican to Hugo, hopefully that I will write more frequently. Hopefully!

During the migration process I couldn’t resist to implement rich css tooltips with html content ExampleText can contain tags with styles. And even images:
. And again, in the hope, that I will use it later. But I‘m already had fun with this process, that‘s why I will describe how I did it.

html-hint

I have import css library html-hint to my scss build. Something like this (line 1):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@import "../../node_modules/html-hint/dist/html-hint";

.hint--html {
    display: inline;
}

.hint__content {
    display: table;

    padding: 1rem;
    min-width: 30vw;
    left: unset !important;
    right: 50%;
    margin-left: 0;
    margin-right: 0;

    color: var(--foreground-color);
    background: var(--background-color);

    border: 1px solid var(--foreground-color);

    font-size: 1em;

    & .hint--title {
        display: block;

        margin-bottom: .4em;

        font-weight: bold;
        font-size: 1.1em;
    }
}

@media screen and (max-width: 600px) {
    .hint__content {
        min-width: 80vw;
        left: unset !important;
        right: unset;
        transform: unset !important;
    }
}

.hint--element {
    display: inline;

    text-decoration: underline dotted var(--dim-color);
    font-style: italic;

    cursor: pointer;
}

Then, add some css styling to the hint (3-50).

There is no prebuild minificated files, that’s why I have to enable nodejs to build my site. So, before the build, I need to run:

> npm install --save html-hint

Hugo shortcode

Create new file layouts/shortcodes/tooltip.html, that will contain markup for our hints:

<span class="hint--html hint--bottom">
    <span class="hint__element">{{- .Get "element" | safeHTML }}</span>
    <span class="hint__content">
        {{ with .Get "title" }}<span class="hint__title">{{- . -}}</span>{{ end }}
        {{- .Inner | safeHTML -}}
    </span>
</span>{{- "" -}}

Insert hint to post content

To create hint in our post content we need to add this shortcode:

{{< tooltip element="element with a hint" title="Title in hint" >}}
Text <b>in a hint</b> using <i style="color:red">HTML tags</i>.
{{< /tooltip >}}

Here is result:

element with a hint Title in hint Text in a hint using HTML tags.

Hover this element and the hint with HTML content will be displayed.

Series: Blogging with hugo

  1. Implementing «Rich CSS Tooltip» in Hugo
  2. Automatic Hugo blog deploy with Gitea and Drone