html - 嵌入跨度和伪元素的问题:之后

标签 html css

我正在尝试添加带有伪元素:after的下划线来注释一些文本。

我的问题是,我想强调下划线。在此示例中,这是短语“实际上确实可以...”和“ ...不起作用”。



.test {
  margin-top: 60px;
}

span.annotation {
  position: relative !important;
  display: inline-block !important;
}

span.annotation:hover {
  cursor: pointer;
  box-shadow: none;
}

span.annotation:after {
  content: '';
  position: absolute;
  left: -.15em;
  width: calc(100% + .5em);
  transition: all .2s;
  height: 3px;
}

span.annotation.firstLevel:after {
  z-index: -3;
  bottom: -0.5em;
}

span.annotation.secondLevel:after {
  z-index: -2;
  bottom: -0.2em;
}

span.annotation.thirdLevel:after {
  z-index: -1;
  bottom: 0.1em;
}

span.annotation:hover:after {
  width: calc(100% + 1em);
  content: attr(title);
  text-align: center;
  font-size: 8px;
  color: #444;
}

span.annotation.firstLevel:hover:after {
  height: calc(100% + 2.3em) !important;
}

span.annotation.secondLevel:hover:after {
  height: calc(100% + 1.9em) !important;
}

span.annotation.thirdLevel:hover:after {
  height: calc(100% + 1.6em) !important;
}

span.additionalType:after {
  background: gold;
}

span.annotation.testA:after {
  background: gainsboro;
}

span.annotation.testB:after {
  background: deepskyblue;
}

span.annotation.testX:after {
  background: gold;
}

span.annotation.testC:after {
  background: deeppink;
}

span.annotation.testD:after {
  background: tomato;
}

span.annotation.testE:after {
  background: greenyellow;
}

<div class="test">
  This is <span title="TestA" class="annotation testA firstLevel"><span title="TestB" class="annotation testB secondLevel"><span title="TestC" class="annotation testC thirdLevel">pretty good example</span> for the</span> <span title="TestD" class="annotation testD secondLevel">Text Annotation</span>  but <span title="TestX" class="annotation testX thirdLevel">actually <span title="TestE" class="annotation secondLevel testE">it</span> do not work</span></span>
</div>





fiddle

最佳答案

这似乎是一个堆栈上下文问题。将z-index:0添加到注释元素以隔离每个元素的上下文,并避免让所有:after属于同一堆叠上下文。



.test {
  margin-top: 60px;
}

span.annotation {
  position: relative !important;
  display: inline-block !important;
  z-index:0;
}

span.annotation:hover {
  cursor: pointer;
  box-shadow: none;
}

span.annotation:after {
  content: '';
  position: absolute;
  left: -.15em;
  width: calc(100% + .5em);
  transition: all .2s;
  height: 3px;
}

span.annotation.firstLevel:after {
  z-index: -3;
  bottom: -0.5em;
}

span.annotation.secondLevel:after {
  z-index: -2;
  bottom: -0.2em;
}

span.annotation.thirdLevel:after {
  z-index: -1;
  bottom: 0.1em;
}

span.annotation:hover:after {
  width: calc(100% + 1em);
  content: attr(title);
  text-align: center;
  font-size: 8px;
  color: #444;
}

span.annotation.firstLevel:hover:after {
  height: calc(100% + 2.3em) !important;
}

span.annotation.secondLevel:hover:after {
  height: calc(100% + 1.9em) !important;
}

span.annotation.thirdLevel:hover:after {
  height: calc(100% + 1.6em) !important;
}

span.additionalType:after {
  background: gold;
}

span.annotation.testA:after {
  background: gainsboro;
}

span.annotation.testB:after {
  background: deepskyblue;
}

span.annotation.testX:after {
  background: gold;
}

span.annotation.testC:after {
  background: deeppink;
}

span.annotation.testD:after {
  background: tomato;
}

span.annotation.testE:after {
  background: greenyellow;
}

<div class="test">
  This is <span title="TestA" class="annotation testA firstLevel"><span title="TestB" class="annotation testB secondLevel"><span title="TestC" class="annotation testC thirdLevel">pretty good example</span> for the</span> <span title="TestD" class="annotation testD secondLevel">Text Annotation</span>  but <span title="TestX" class="annotation testX thirdLevel">actually <span title="TestE" class="annotation secondLevel testE">it</span> do not work</span>
  </span>
</div>

关于html - 嵌入跨度和伪元素的问题:之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54582611/

相关文章:

javascript - 如何在打开新选项卡(打开文件)时保持在选项卡中?

html - 垂直划分CSS布局

html - 创建自定义论坛,使用表格、<ul> <li> <dl> <dt> 或 div?

javascript - 在页面顶部不拖动时光标位置和可拖动位置不一样

javascript - 我的 jQuery 代码在本地系统中不起作用,但在 jsFiddle 中起作用

css - z-index 未按预期使用 before 伪元素呈现

javascript - 如何从 HTML 元素内部获取值并在 JS 中将其设置为变量

html - 如何使表格的两个单元格之间的边距为 0?

javascript - Angularjs 不更新 HTML View

html - 浏览器中的 CSS 高度大小