跳转到帖子
在手机APP中查看

一个更好的浏览方法。了解更多

OKCCN - XenForo & IPS Plugin Marketplace

主屏幕上的全屏APP,带有推送通知、徽章等。

在iOS和iPadOS上安装此APP
  1. 在Safari中轻敲分享图标
  2. 滚动菜单并轻敲添加到主屏幕
  3. 轻敲右上角的添加按钮。
在安卓上安装此APP
  1. 轻敲浏览器右上角的三个点菜单 (⋮) 。
  2. 轻敲添加到主屏幕安装APP
  3. 轻敲安装进行确认。
  • 选择语言

推荐的帖子

  • 行政经理

CSS Tooltip


CSS Tooltip

A CSS tooltip is used to specify extra information about something when the user moves the mouse pointer over an element:

Top Tooltip text
Right Tooltip text
Bottom Tooltip text
Left Tooltip text


CSS Create a Basic Tooltip

Create a tooltip that appears when the user moves the mouse over an element:

Example

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

/* Tooltip text */
.tooltiptext {
  visibility: hidden; /* Hidden by default */
  width: 130px;
  background-color: black;
  color: #ffffff;
  text-align: center;
  padding: 5px 0;
  border-radius: 6px;
  position: absolute;
  z-index: 1; /* Ensure tooltip is displayed above content */
}

/* Show the tooltip text on hover */
.tooltip:hover .tooltiptext {
  visibility: visible;
}
</style>

<div class="tooltip">Hover over me
  <span class="tooltiptext">Some tooltip text</span>
</div>
Try it Yourself »

Example Explained

HTML:

  • Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text.
  • The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext".

CSS:

  • The tooltip class use position:relative, which is needed to position the tooltip text (position:absolute). Tip: See examples below on how to position the tooltip.
  • The tooltiptext class holds the actual tooltip text. It is hidden by default, and will be visible on hover.
  • The :hover selector is used to show the tooltip text when the user moves the mouse over the <div> with class="tooltip".


Positioning the Tooltip

You can position the tooltip as you like. Here we will show how to position the tooltip to the left, right, top and bottom.

Right- and Left-aligned Tooltip

In this example, the tooltip is placed to the right (left:105%) of the "hoverable" text (<div>). Also note that top:-5px is used to place it in the middle of its container element. We use the number 5 because the tooltip text has a top and bottom padding of 5px. If you increase its padding, also increase the value of the top property to ensure that it stays in the middle (if this is something you want). The same applies if you want the tooltip placed to the left.

Example

Right-aligned tooltip:

.tooltiptext {
  top: -5px;
  left: 105%;
}

Result:

Hover over me Tooltip text
Try it Yourself »

Example

Left-aligned tooltip: 

.tooltiptext {
  top: -5px;
  right: 105%;
}

Result:

Hover over me Tooltip text
Try it Yourself »

Top- and Bottom-aligned Tooltip

If you want the tooltip to appear on top or on the bottom, see examples below. Note that we use the margin-left property with a value of minus 65 pixels. This is to center the tooltip above/below the hoverable text. It is set to the half of the tooltip's width (130/2 = 65).

Example

Top-aligned tooltip: 

.tooltiptext {
  width: 130px;
  bottom: 100%;
  left: 65%;
  margin-left: -65px; /* Use half of the width (130/2 = 65), to center the tooltip */
}

Result:

Hover over me Tooltip text
Try it Yourself »

Example

Bottom-aligned tooltip: 

 .tooltiptext {
  width: 130px;
  top: 100%;
  left: 50%;
  margin-left: -65px; /* Use half of the width (130/2 = 65), to center the tooltip */
}

Result:

Hover over me Tooltip text
Try it Yourself »

Fade-in Tooltip

If you want a tooltip that fades in, use the CSS transition property and the opacity property, and go from being completely invisible to 100% visible, in a number of specified seconds (2 second in our example):

Example

.tooltiptext {
  opacity: 0;
  transition: opacity 2s;
}

.tooltip:hover .tooltiptext {
  opacity: 1;
}
Try it Yourself »

Tooltip Subpages

Continue learning about CSS tooltips:

  • Tooltip Arrows - arrow styling, fade-in effects



参与讨论

你可立刻发布并稍后注册。 如果你有帐户,立刻登录发布帖子。

游客
回帖…

帐户

导航

搜索

搜索

配置浏览器推送通知

Chrome (安卓)
  1. 轻敲地址栏旁的锁形图标。
  2. 轻敲权限 → 通知。
  3. 调整你的偏好。
Chrome (台式电脑)
  1. 点击地址栏中的挂锁图标。
  2. 选择网站设置。
  3. 找到通知选项并调整你的偏好。