skip to Main Content

I’m trying to make a tooltip, that pops up when hovered over, not wrap text when it is immediately able to. The tooltip functionality works perfectly, this question is purely related to text wrapping and width and such. Currently, the it wraps to the width of the longest word it contains, but I want it to reach the width of the viewport before wrapping text.

.tooltip {
  position: relative;
  display: inline-block;
}

.tooltip .tooltiptext {
  visibility: hidden;
  position: absolute;
  z-index: 1;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
}
<a class="tooltip">
        Hover
        <div class="tooltip tooltiptext">
                Tooltip Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
        </div>
</a>

2

Answers


  1. Remove "position: relative;" and you get this css:

    .tooltip {
    }
    
    .tooltip .tooltiptext {
      visibility: hidden;
      position: absolute;
      z-index: 1;
      
    }
    
    .tooltip:hover .tooltiptext {
      visibility: visible;
    }
    
    .tooltip {}
    
    .tooltip .tooltiptext {
      visibility: hidden;
      position: absolute;
      z-index: 1;
    }
    
    .tooltip:hover .tooltiptext {
      visibility: visible;
    }
    <a class="tooltip">
        Hover
        <div class="tooltip tooltiptext">
            Tooltip Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
        </div>
    </a>
    Login or Signup to reply.
  2. I would add a width to the div, so that you can control how much space it actually use.

    You can also remove the display: inline-block if you don’t want the div to go to the right

    .tooltip {
      position: relative;
    }
    
    .tooltip .tooltiptext {
      visibility: hidden;
      position: absolute;
    }
    
    .tooltip:hover .tooltiptext {
      visibility: visible;
      z-index: 1;
      width: 20rem;
      display: inline-block;
    }
    <a class="tooltip">
            Hover
            <div class="tooltip tooltiptext">
                    Tooltip Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
            </div>
    </a>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search