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

前端 未结 6 1905
春和景丽
春和景丽 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;
    }
    
        drag drag drag drag drag
    Hover me
    
    
    
        Lorem ipsum dolor sit amet, consectetur adipisicing elit.
    Hover me

提交回复
热议问题