Add tooltip to font awesome icon

后端 未结 6 781
情深已故
情深已故 2021-02-11 13:06

Has anyone here added tooltips to font-awesome icons?

I have the following jsfiddle, but cannot seem to find a guide to add tooltips to the icons.



        
相关标签:
6条回答
  • 2021-02-11 13:19

    codepen Is perhaps a helpful example.

    <div class="social-icons">
      <a class="social-icon social-icon--codepen">
        <i class="fa fa-codepen"></i>
        <div class="tooltip">Codepen</div>
    </div>
    
    body {  
      display: flex;
      align-items: center;
      justify-content: center;
      min-height: 100vh;
    }
    
    /* Color Variables */
    $color-codepen: #000;
    
    /* Social Icon Mixin */
    @mixin social-icon($color) {
      background: $color;
      background: linear-gradient(tint($color, 5%), shade($color, 5%));
      border-bottom: 1px solid shade($color, 20%);
      color: tint($color, 50%);
    
      &:hover {
        color: tint($color, 80%);
        text-shadow: 0px 1px 0px shade($color, 20%);
      }
    
      .tooltip {
        background: $color;
        background: linear-gradient(tint($color, 15%), $color);
        color: tint($color, 80%);
    
        &:after {
          border-top-color: $color;
        }
      }
    }
    
    /* Social Icons */
    .social-icons {
      display: flex;
    }
    
    .social-icon {
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;
      width: 80px;
      height: 80px;
      margin: 0 0.5rem;
      border-radius: 50%;
      cursor: pointer;
      font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
      font-size: 2.5rem;
      text-decoration: none;
      text-shadow: 0 1px 0 rgba(0,0,0,0.2);
      transition: all 0.15s ease;
    
      &:hover {
        color: #fff;
    
        .tooltip {
          visibility: visible;
          opacity: 1;
          transform: translate(-50%, -150%);
        }
      }
    
      &:active {
        box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.5) inset;
      }
    
      &--codepen { @include social-icon($color-codepen); }
    
      i {
        position: relative;
        top: 1px;
      }
    }
    
    /* Tooltips */
    .tooltip {
      display: block;
      position: absolute;
      top: 0;
      left: 50%;
      padding: 0.8rem 1rem;
      border-radius: 3px;
      font-size: 0.8rem;
      font-weight: bold;
      opacity: 0;
      pointer-events: none;
      text-transform: uppercase;
      transform: translate(-50%, -100%);
      transition: all 0.3s ease;
      z-index: 1;
    
      &:after {
        display: block;
        position: absolute;
        bottom: 0;
        left: 50%;
        width: 0;
        height: 0;
        content: "";
        border: solid;
        border-width: 10px 10px 0 10px;
        border-color: transparent;
        transform: translate(-50%, 100%);
      }
    }
    
    0 讨论(0)
  • 2021-02-11 13:25

    In regards to this question, this can be easily achieved using a few lines of SASS;

    HTML:

    <a href="https://www.urbandictionary.com/define.php?term=techninja" data-tool-tip="What's a tech ninja?" target="_blank"><i class="fas fa-2x fa-user-ninja" id="tech--ninja"></i></a>
    

    CSS output would be:

    a[data-tool-tip]{
        position: relative;
        text-decoration: none;
        color: rgba(255,255,255,0.75);
    }
    
    a[data-tool-tip]::after{
        content: attr(data-tool-tip);
        display: block;
        position: absolute;
        background-color: dimgrey;
        padding: 1em 3em;
        color: white;
        border-radius: 5px;
        font-size: .5em;
        bottom: 0;
        left: -180%;
        white-space: nowrap;
        transform: scale(0);
        transition: 
        transform ease-out 150ms,
        bottom ease-out 150ms;
    }
    
    a[data-tool-tip]:hover::after{
        transform: scale(1);
        bottom: 200%;
    }
    

    Basically the attribute selector [data-tool-tip] selects the content of whatever's inside and allows you to animate it however you want.

    0 讨论(0)
  • 2021-02-11 13:27

    Simply use title in tag like

    <i class="fa fa-edit" title="Edit Mode"></i>
    

    This will show 'Edit Mode' when hover that icon.

    0 讨论(0)
  • 2021-02-11 13:29

    You should use 'title' attribute along with 'data-toogle' (bootstrap).

    For example

    <i class="fa fa-info" data-toggle="tooltip" title="Hooray!"></i>Hover over me
    

    and do not forget to add the javascript to display the tooltip

    <script>
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();   
        });
    </script>
    
    0 讨论(0)
  • 2021-02-11 13:30

    The issue of adding tooltips to any HTML-Output (not only FontAwesome) is an entire book on its own. ;-)

    The default way would be to use the title-attribute:

      <div id="welcomeText" title="So nice to see you!">
        <p>Welcome Harriet</p>
      </div>
    

    or

    <i class="fa fa-cog" title="Do you like my fa-coq icon?"></i>
    

    But since most people (including me) do not like the standard-tooltips, there are MANY tools out there which will "beautify" them and offer all sort of enhancements. My personal favourites are jBox and qtip2.

    0 讨论(0)
  • 2021-02-11 13:36

    Simply with native html & css :

    <div class="tooltip">Hover over me
      <span class="tooltiptext">Tooltip text</span>
    </div>
    
    /* Tooltip container */
    .tooltip {
      position: relative;
      display: inline-block;
      border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
    }
    
    /* Tooltip text */
    .tooltip .tooltiptext {
      visibility: hidden;
      width: 120px;
      background-color: #555;
      color: #fff;
      text-align: center;
      padding: 5px 0;
      border-radius: 6px;
    
      /* Position the tooltip text */
      position: absolute;
      z-index: 1;
      bottom: 125%;
      left: 50%;
      margin-left: -60px;
    
      /* Fade in tooltip */
      opacity: 0;
      transition: opacity 0.3s;
    }
    
    /* Tooltip arrow */
    .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;
    }
    
    /* Show the tooltip text when you mouse over the tooltip container */
    .tooltip:hover .tooltiptext {
      visibility: visible;
      opacity: 1;
    }
    

    Here is the source of the example from w3schools

    0 讨论(0)
提交回复
热议问题