没有JavaScript的HTML内容工具提示

问题描述 投票:53回答:8

当您将鼠标悬停在网页的某个区域上时,有许多基于JavaScript的库可以显示工具提示。有些是相当简单的,有些允许工具提示显示使用CSS设置样式的HTML内容。

但是有没有办法在不使用JavaScript的情况下显示样式化的工具提示?如果您只使用title属性,则不处理标记(例如foo<br />bar不会产生换行符)。我正在寻找一种解决方案,允许用户在不使用任何JavaScript的情况下显示样式化的HTML内容。

html css tooltip
8个回答
67
投票

我用css做了一个小例子

.hover {
  position: relative;
  top: 50px;
  left: 50px;
}

.tooltip {
  /* hide and position tooltip */
  top: -10px;
  background-color: black;
  color: white;
  border-radius: 5px;
  opacity: 0;
  position: absolute;
  -webkit-transition: opacity 0.5s;
  -moz-transition: opacity 0.5s;
  -ms-transition: opacity 0.5s;
  -o-transition: opacity 0.5s;
  transition: opacity 0.5s;
}

.hover:hover .tooltip {
  /* display tooltip on hover */
  opacity: 1;
}
<div class="hover">hover
  <div class="tooltip">asdadasd
  </div>
</div>

小提琴

http://jsfiddle.net/8gC3D/471/


14
投票

使用title属性:

<a href="#" title="Tooltip here">Link</a>

6
投票

与koningdavid类似,但在display:none和block上工作,并添加了额外的样式。

div.tooltip {
  position: relative;
  /*  DO NOT include below two lines, as they were added so that the text that
        is hovered over is offset from top of page*/
  top: 10em;
  left: 10em;
  /* if want hover over icon instead of text based, uncomment below */
  /*    background-image: url("../images/info_tooltip.svg");
            /!* width and height of svg *!/
            width: 16px;
            height: 16px;*/
}


/* hide tooltip */

div.tooltip span {
  display: none;
}


/* show and style tooltip */

div.tooltip:hover span {
  /* show tooltip */
  display: block;
  /* position relative to container div.tooltip */
  position: absolute;
  bottom: 1em;
  /* prettify */
  padding: 0.5em;
  color: #000000;
  background: #ebf4fb;
  border: 0.1em solid #b7ddf2;
  /* round the corners */
  border-radius: 0.5em;
  /* prevent too wide tooltip */
  max-width: 10em;
}
<div class="tooltip">
  hover_over_me
  <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis purus dui. Sed at orci. </span>
</div>

5
投票

这个非常有趣,

仅限HTML和CSS

.help-tip {
  position: absolute;
  top: 18px;
  left: 18px;
  text-align: center;
  background-color: #BCDBEA;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  font-size: 14px;
  line-height: 26px;
  cursor: default;
}

.help-tip:before {
  content: '?';
  font-weight: bold;
  color: #fff;
}

.help-tip:hover span {
  display: block;
  transform-origin: 100% 0%;
  -webkit-animation: fadeIn 0.3s ease-in-out;
  animation: fadeIn 0.3s ease-in-out;
}

.help-tip span {
  display: none;
  text-align: left;
  background-color: #1E2021;
  padding: 5px;
  width: 200px;
  position: absolute;
  border-radius: 3px;
  box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
  left: -4px;
  color: #FFF;
  font-size: 13px;
  line-height: 1.4;
}

.help-tip span:before {
  position: absolute;
  content: '';
  width: 0;
  height: 0;
  border: 6px solid transparent;
  border-bottom-color: #1E2021;
  left: 10px;
  top: -12px;
}

.help-tip span:after {
  width: 100%;
  height: 40px;
  content: '';
  position: absolute;
  top: -40px;
  left: 0;
}
<span class="help-tip">
    	<span > This is the inline help tip! </span>
</span>

3
投票

您可以使用title属性,例如如果你想在文本上有一个工具提示,只需:

<span title="This is a Tooltip">This is a text</span>

3
投票

用css做另一种类似的方法:

<style>
#img {  }
#img:hover {visibility:hidden}
#thistext {font-size:22px;color:white }
#thistext:hover {color:black;}
#hoverme {width:50px;height:50px;
 }

#hoverme:hover { background-color:green;position:absolute ;left:300px;top:100px;width:40%;height:20%;}

</style>

<p id="hoverme"><img id="img" src="http://a.deviantart.net/avatars/l/o/lol-cat.jpg"></img><span id="thistext">LOCATZ!!!!</span></p>

试试吧:http://jsfiddle.net/FdBu7/

这里有一些关于过渡和新方法的链接:http://www.w3schools.com/css3/css3_transitions.asp http://dev.opera.com/articles/view/css3-show-and-hide/


3
投票

纯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>

0
投票

这是我的解决方案:

https://gist.github.com/BryanMoslo/808f7acb1dafcd049a1aebbeef8c2755

该元素使用工具提示文本重新标记“tooltip-title”属性,并在悬停时使用CSS显示,我更喜欢此解决方案,因为我不必将工具提示文本包含为HTML元素!

© www.soinside.com 2019 - 2024. All rights reserved.