javascript - 使用 css 或 js 突出显示半高文本

标签 javascript css highlight

<分区>

我需要像黄线那样做。 enter image description here

我知道使用<span><p>可以模仿标记效果。 但我需要这个标记 not to full 文本高度,必须是 1/2 或 1/3 高度。(如图)

我尝试使用伪类(之前和之后),但仍然失败。

请给我一些提示!

最佳答案

我能想到的最简单和最快的方法是使用 linear-gradient 来“半填充”你的背景:

.half_background {
  background: linear-gradient(to top, yellow 50%, transparent 50%);
}
<p>Some text, <span class="half_background">some other text with half background</span>.</p>

⋅ ⋅ ⋅

然后,我们可以很容易地扩展它来做一些其他的事情:

p {
  background-color: #eee;
  margin: 0.4em 0;
  padding: 0.6em;
}

.half_background {
  background: linear-gradient(to top, var(--bot) 50%, var(--top) 50%);
}
<p>Some text, <span class="half_background" style="--top: transparent; --bot: yellow;">some other text with half background</span>.</p>
<p>Some text, <span class="half_background" style="--top: orange; --bot: transparent;">some other text with half background</span>.</p>
<p>Some text, <span class="half_background" style="--top: violet; --bot: cyan;">some other text with half background</span>.</p>

关于javascript - 使用 css 或 js 突出显示半高文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51436852/

相关文章:

JavaScript - 为什么此代码记录 'undefined' 但显示在 Chrome 控制台中? (关闭)

javascript - iOS 上的 JQ UI 可拖动 : initiating dragging in taphold-handler

html - 如何居中对齐 CSS 导航菜单?

php - 在php中突出显示菜单中的当前页面

javascript - JS 正则表达式 : Double slash splitting along with other characters

JavaScript 作用域和闭包语法

css - 首先显示未样式化的 HTML,然后更改为样式化版本,这可能是什么原因造成的?

javascript - 如何取消突出显示onclick?

markdown - stackedit是否允许您禁用语法突出显示代码块?

javascript - 异步/同步 Javascript