Search code examples
htmlcsstooltiphtml-lists

Using tootips with lists [HTML, CSS]


Looked at W3School's tooltips guide and basically retyped it, but the example doesn't use a list, and I am using a list for a practice website I'm doing. As a result, it's formatted weirdly.

I tried messing around with it, removing the li tag and it worked but it isn't what I'm looking for.

h1 {
    color: #D3D3D3;
}

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

.tooltip .tooltiptext {
    visibility: hidden;
    width: 120px;
    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 .toolptiptext::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;
    }

I expect it to be a list that when you hover over it'll show the tooltip, formatted the same as a regular HTML list. That's really it.


Solution

  • .tooltiplink::after {
      visibility: hidden;
      content: attr(name);
      background-color: #555;
      color: #fff;
      text-align: center;
      border-radius: 6px;
      padding: 5px 0;
      position: absolute;
      margin: -1rem;
      padding: 5px;
      border-style: solid;
      border-color: #555 transparent transparent transparent;
      opacity: 0;
      transition: opacity 0.3s;
    }
    .tooltiplink:hover::after {
      visibility: visible;
      opacity: 1;
    }
    <ul>
      <li class="tooltiplink" name="This is a tooltip">Option 1
      </li>
      <li class="tooltiplink" name="This is a tooltip for the 2th option">Option 2
      </li>
      <li class="tooltiplink" name="This is a tooltip for the 3th option">Option 3
      </li>
    </ul>