【问题标题】:Yellow underline CSS randomly not applied to full width of span黄色下划线 CSS 随机不应用于跨度的全宽
【发布时间】:2021-12-03 07:15:27
【问题描述】:

示例: https://flowster.app/flowster-affiliate-program-activation-bonus/

您可以看到黄色下划线(突出显示)仅使其达到“A”,但它应该位于“Flowster Affiliate Program”下方:

但是,HTML 看起来像这样:

<h1 class="elementor-heading-title elementor-size-default">
  <span class="yellowhighlight">Flowster Affiliate Program</span> -
  Activation Bonus
</h1>

yellowhighlight CSS 类看起来像:

span.yellowhighlight {
  position: relative;
  z-index: 0;
}

span.yellowhighlight::after {
  content: "";
  position: absolute;
  left: -0.15em;
  right: -0.15em;
  top: 0.8em;
  height: 0.4em;
  border-radius: 2em;
  background: #fffa50;
  z-index: -1;
}

这很奇怪,因为在其他页面上它看起来很正常。

span.yellowhighlight {
  position: relative;
  z-index: 0;
}

span.yellowhighlight::after {
  content: "";
  position: absolute;
  left: -0.15em;
  right: -0.15em;
  top: 0.8em;
  height: 0.4em;
  border-radius: 2em;
  background: #fffa50;
  z-index: -1;
}
<h1 class="elementor-heading-title elementor-size-default">
  <span class="yellowhighlight">Flowster Affiliate Program</span> -
  Activation Bonus
</h1>

【问题讨论】:

  • 将 display:inline-block 添加到 span
  • @TemaniAfif 我们已经在许多页面中使用这些跨度,所以这不是一个好的选择。希望有办法更新类 CSS?
  • 拆分成三个span,每个span一个字。
  • @ЖнецЪ 这不是一个好的长期解决方案。这需要非技术人员可用于任何页面上的任何文本。试图向他们解释他们需要为每个单词使用跨度会很困难。

标签: html css background-color elementor


【解决方案1】:

使用背景来实现:

span.yellowhighlight {
  --s:0.4em; /* control the size */
  --d:2px;  /* control the distance */
  background:
    linear-gradient(#fffa50 0 0) 50% calc(100% - var(--d))/calc(100% - var(--s)) var(--s),
    radial-gradient(farthest-side,#fffa50 98%,#0000) bottom var(--d) left  0/var(--s) var(--s),
    radial-gradient(farthest-side,#fffa50 98%,#0000) bottom var(--d) right 0/var(--s) var(--s);
  background-repeat:no-repeat;
  -webkit-box-decoration-break:clone;
          box-decoration-break:clone;
}
<h1 class="elementor-heading-title elementor-size-default">
  <span class="yellowhighlight">Flowster Affiliate Program</span> -
  Activation Bonus
</h1>

<h1 class="elementor-heading-title elementor-size-default">
  <span class="yellowhighlight">Flowster Affiliate<br> Program</span> -
  Activation Bonus
</h1>

要了解技巧,请为每个背景层使用不同的颜色:

span.yellowhighlight {
  --s:0.4em; /* control the size */
  background:
    linear-gradient(pink 0 0) bottom/calc(100% - var(--s)) var(--s),
    radial-gradient(farthest-side,blue 98%,#0000) bottom left /var(--s) var(--s),
    radial-gradient(farthest-side,red 98%,#0000) bottom right/var(--s) var(--s);
  background-repeat:no-repeat;
  -webkit-box-decoration-break:clone;
          box-decoration-break:clone;
}
<h1 class="elementor-heading-title elementor-size-default">
  <span class="yellowhighlight">Flowster Affiliate Program</span> -
  Activation Bonus
</h1>

<h1 class="elementor-heading-title elementor-size-default">
  <span class="yellowhighlight">Flowster Affiliate<br> Program</span> -
  Activation Bonus
</h1>

【讨论】:

  • 哇,我认为这是迄今为止我见过的最好的解决方案,因为它不需要任何 HTML 修改,也不必对每个单词都使用 。只有一件事……注意新的黄色高光比旧的低得多?可以将它向上移动,使其更“在”单词“后面”而不是“在单词下面”吗?希望这是有道理的。我希望很容易将屏幕截图附加到 cmets :(
  • @Kane 检查更新,添加了另一个变量来控制距离
  • 是的!你摇滚。
【解决方案2】:

需要对现有 HTML 和 CSS 进行最少更改的解决方案


在阅读了所有的cmets和所有答案后,我想出了一个解决方案。

我的主要目标不是更改任何 HTML(即,相同的结构、相同的类),也不是更改您想要实现下划线的方式(即,使用 ::after)。我已经使用 Chrome DevTools 测试了我的解决方案,以确保它可以正常工作。

说明

诀窍是,当您有三个 span 元素(即每个单词在一个单独的 span 元素中)时,如何实现连续的下划线。

你只需要做两件事:

  1. 你可以用“溢出”来实现连续的下划线。这可以通过设置width: 120%;来完成。

  2. 但您不想“溢出”最后一个 span 元素(无论有多少)。这可以通过将width: 100%; 设置为最后一个span 元素来完成。

我的解决方案 IRL 的屏幕截图

片段

span.yellowhighlight {
  position: relative;
  z-index: 0;
}

span.yellowhighlight::after {
  content: "";
  position: absolute;
  left: -0.15em;
  right: -0.15em;
  top: 0.8em;
  height: 0.4em;
  border-radius: 2em;
  background: #fffa50;
  z-index: -1;
  width: 120%; /* Change this. */
}

/* Add this. */
span.yellowhighlight:last-child::after {
  width: 100%;
}
<h1 class="elementor-heading-title elementor-size-default">
  <span class="yellowhighlight">Flowster</span>
  <span class="yellowhighlight">Affiliate</span>
  <span class="yellowhighlight">Program</span>
  - Activation Bonus
</h1>

【讨论】:

  • 这看起来不错,但我们需要一个不需要对每个单词都使用 的解决方案。
【解决方案3】:

问题是没有设置宽度。要使黄色突出显示整个文本宽度,只需将width: 100% 添加到span.yellowhighlight::after

最终的 CSS 应该如下所示:

span.yellowhighlight::after {
  content: "";
  position: absolute;
  left: -0.15em;
  right: -0.15em;
  top: 0.8em;
  height: 0.4em;
  border-radius: 2em;
  background: #fffa50;
  z-index: -1;
  width: 100%;
}

编辑:

保持 CSS 不变,尝试像这样编辑 HTML

&lt;span class="yellowhighlight"&gt;Flowster &lt;/span&gt;Affiliate Program - Activation Bonus

【讨论】:

  • 不幸的是,这不起作用。还有其他想法吗?
  • 查看修改后的解决方案
  • 为什么我们只想突出显示“Flowster”?重点是突出显示“Flowster 会员计划”。不,我们不想要一些“黑客”解决方案 - 这需要适用于任何页面上的任何文本,并且可供非技术人员使用;)
猜你喜欢
  • 1970-01-01
  • 2014-06-23
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多