본문 바로가기
HTML/CSS

[HTML/CSS] Tooltip 구현하기

by teamnova 2024. 1. 11.

안녕하세요, 이번 게시글에서는 html, css로 tooltip을 구현해보겠습니다.

우선 시연 영상부터 보여드리겠습니다.

다음은 전체 코드입니다. 

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>
  <body>
    <div class="tooltip">
      마우스를 올려보세요
      <span class="tooltiptext">여기는 툴팁 내용!</span>
    </div>
  </body>
</html>

<style>
  /* Tooltip container */
  .tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
    cursor: pointer; /* Change cursor style on hover */
  }

  /* Tooltip text */
  .tooltip .tooltiptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    top: 125%; /* Position tooltip below the element */
    left: 50%;
    margin-left: -80px; /* Use half of the width value */
    opacity: 0;
    transition: opacity 0.3s;
    box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.3); /* Add shadow effect */
  }

  /* Tooltip arrow */
  .tooltip .tooltiptext::after {
    content: "";
    position: absolute;
    bottom: 100%; /* Arrow on top of the tooltip */
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: transparent transparent #555 transparent; /* Arrow color and direction */
  }

  /* Show the tooltip text when you mouse over the tooltip container */
  .tooltip:hover .tooltiptext {
    visibility: visible;
    opacity: 1;
  }
</style>