在html中用纯css和javascript显示工具提示到div的例子

73 阅读1分钟

HTML中的Tooltip是当鼠标越过一个元素时显示在一个元素中的重要信息。一个元素可以是div、按钮、输入控件。

本教程通过实例解释了为div元素添加工具提示的方法。

我们有多种方法可以在html页面中为div元素添加工具提示

<div class="tooltipContainer"> Name
    <span class="tooltip">Please enter name</span>
  <input type="text" name="name"/>
</div>

CSS代码:

.tooltipContainer {
  position: absolute;
  display: inline-block;
  cursor: pointer;
}

.tooltipContainer .tooltip {
  visibility: hidden;
  width: 130px;
  background-color: blue;
  color: #fff;
  text-align: left;
  border-radius: 6px;
  padding: 5px 10px;
  position: absolute;
  z-index: 1;
  bottom: 160%;
  left: 100%;
  margin-left: -140px;
  opacity: 0;
  transition: opacity 1s;
}

.tooltipContainer .tooltip::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -15px;
  border-width: 15px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}

.tooltipContainer:hover .tooltip {
  visibility: visible;
  opacity: 1;
}

在上面的例子中,没有添加javascript