Tooltip with HTML content without JavaScript

2019-01-07 08:55发布

There are plenty of JavaScript-based libraries that show tooltips when you hover your mouse over a certain area of a web page. Some are rather plain, some allow the tooltip to display HTML content styled with CSS.

But is there a way to show a styled tooltip without using JavaScript? If you just use the title attribute, tags are not processed (e.g. foo<br />bar doesn't produce a line break). I'm looking for a solution that allows one to display styled HTML content without using any JavaScript.

标签: html css tooltip
8条回答
劳资没心,怎么记你
2楼-- · 2019-01-07 09:23

You can use the title attribute, e.g. if you want to have a Tooltip over a text, just make:

<span title="This is a Tooltip">This is a text</span>
查看更多
Ridiculous、
3楼-- · 2019-01-07 09:29

Pure CSS:

.app-tooltip {
  position: relative;
}

.app-tooltip:before {
  content: attr(data-title);
  background-color: rgba(97, 97, 97, 0.9);
  color: #fff;
  font-size: 12px;
  padding: 10px;
  position: absolute;
  bottom: -50px;
  opacity: 0;
  transition: all 0.4s ease;
  font-weight: 500;
  z-index: 2;
}

.app-tooltip:after {
  content: '';
  position: absolute;
  opacity: 0;
  left: 5px;
  bottom: -16px;
  border-style: solid;
  border-width: 0 10px 10px 10px;
  border-color: transparent transparent rgba(97, 97, 97, 0.9) transparent;
  transition: all 0.4s ease;
}

.app-tooltip:hover:after,
.app-tooltip:hover:before {
  opacity: 1;
}
<div href="#" class="app-tooltip" data-title="Your message here"> Test here</div>

查看更多
登录 后发表回答