Search code examples
htmlcsstwitter-bootstraphtml-tabletooltip

Putting HTML Table in a tooltip inside an icon


I've been trying to put a table in a tooltip that's inside an icon, the main goal is create a little legend that explains the meaning of these colors using this icon in a yellow/orange color.

By far I only managed to make the icon appear, but with not further success. The table that I've made in the column "Color" by now it just shows the name of the color, but I want to show like a little square painted in the color that represents i.e. a red square for the color red, a blue square for the color blue... and so on.

This is the code that I've made:

<style>
    .tooltip {
        position: relative;
        display: inline-block;
        border-bottom: 1px dotted black;
    }

    .tooltip .tooltiptext {
        visibility: hidden;
        width: 180px;
        background-color: #555;
        color: #fff;
        text-align: center;
        border-radius: 6px;
        padding: 5px 0;
        position: absolute;
        z-index: 1;
        bottom: 125%;
        left: 50%;
        margin-left: -60px;
        opacity: 0;
        transition: opacity 0.3s;
    }

    .tooltip .tooltiptext::after {
        content: "";
        position: absolute;
        top: 100%;
        left: 50%;
        margin-left: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: #555 transparent transparent transparent;
    }

    .tooltip:hover .tooltiptext {
        visibility: visible;
        opacity: 1;
    }
</style>
<a>
   <span class="tooltip">
      <i class="fas fa-info-circle"></i>
      <table class="tooltiptext">
         <thead>
            <tr>
               <th>Color</th>
               <th>Significado</th>
            <tr>
         </thead>
         <tbody>
            <tr>
               <th>Gris</th>
               <th>Comprado</th>
            </tr>
            <tr>
               <th>Verde</th>
               <th>Emergencia</th>
            </tr>
            <tr>
               <th>Verde</th>
               <th>Urgencia</th>
            </tr>
            <tr>
               <th>Verde</th>
               <th>Urgencia Menor</th>
            </tr>
            <tr>
               <th>Verde</th>
               <th>Sin Urgencia</th>
            </tr>
         </tbody>
      </table>
   </span>
</a>             

Edit: Here is a little snippet of it.


Solution

  • I was able to do it using bootstrap and javascript, I used js in order to avoid a little issue with visual studio, because the compiler didn't allowed me to put a class attribute inside the title attribute of bootstrap's tooltip.

    Here's the code:

    <a id="information" data-toggle="tooltip"  data-html="true">
       <span><i class="fas fa-info-circle" style="color: #ff9900"></i></span>
    </a>
    
    function info() {   
    
        var infoTooltip = document.getElementById("information");
    
        infoTooltip.setAttribute("title",
            `
            <table>
                <thead>
                    <tr>
                        <th>Colores</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th><span class="badge badge-danger px-2">Emergencia</span></th>
                    </tr>
                    <tr>
                        <th><span class="badge badge-warning px-2">Urgencia</span></th>
                    </tr>
                    <tr>
                        <th><span class="badge badge-success px-2">Urgencia Menor</span></th>
                    </tr>
                    <tr>
                        <th><span class="badge badge-primary px-2">Sin Urgencia</span></th>
                    </tr>
                    <tr>
                        <th><span class="badge badge-light px-2">Comprado</span></th>
                    </tr>
                </tbody>
            </table>
            `
        );
    }
    

    This is the outcome.