Pure CSS: Center Tooltip Above Text On Hover pt. 2

前端 未结 6 1903
春和景丽
春和景丽 2021-01-13 01:05

Expanding on Pure CSS: Center Tooltip Above Text On Hover -- how does one make the tooltip hover centered relative to its container if the tooltip is wider than said

相关标签:
6条回答
  • 2021-01-13 01:31

    Here's the simplest solution using CSS3 transform.

    JSfiddle Demo

    .drag-hint {
      position: relative;
      margin: 50px;
      display: inline-block;
      padding: 0 1em;
      border: 1px solid red;
      /* for visual reference */
    }
    .drag-hint > span {
      background: black;
      color: white;
      white-space: nowrap;
      display: none;
    }
    .drag-hint:hover > span {
      display: inline-block;
      position: absolute;
      top: -25px;
      left: 50%;
      transform: translateX(-50%);
      text-align: center;
    }
    <span class="drag-hint">
        <span>drag drag drag drag drag</span>
    Hover me</span>
    
    
    <span class="drag-hint">
        <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</span>
    Hover me</span>

    0 讨论(0)
  • 2021-01-13 01:31

    Try with modified below code in your code.

    .drag-hint:hover > span {
      display: inline;
      position:absolute;
      top: -25px;
      left: 0;
      right: 0;
      text-align: center;
      width:200px;
     }
    
    0 讨论(0)
  • 2021-01-13 01:37

    JS Bin

    I achieve your question.

    HTML

    <h1>This:</h1>
        <div id="test" class="drag-hint"><p>drag drag drag drag drag</p>Hover me</div>
        <h1>Should appear as:</h1>
        <img src="http://s22.postimg.org/h1qk194bh/Untitled_2.jpg">
    

    CSS

    .drag-hint {
      position:relative;
      margin: 50px;
    }
    .drag-hint > p {
      background: black;
      color: white;
      width: 100%;
     display: none;
    }
    .drag-hint:hover > p {
      display: inline;
      position:absolute;
      top: -40px;
      left: 0;
      right: 0;
      text-align: center;
    }
    
    0 讨论(0)
  • 2021-01-13 01:45

    JS Bin

     .drag-hint:hover > span {
        display: table;
        position: absolute;
        top: -25px;
        left: -51px;
        right: 0;
        text-align: center;
        padding: 2px;
     }
    
    0 讨论(0)
  • 2021-01-13 01:45

    Use div instead of span

    <div id="test" class="drag-hint"><div>drag drag drag drag drag</div>Hover me</div>
    

    a fiddle here

    0 讨论(0)
  • 2021-01-13 01:46

    I hope this will solve your problem. http://jsbin.com/xihupeyowize/1/

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