0

I'm referring to this answer. What if there is more than one color needed to be strikethrough?

span {
  background: 
    linear-gradient(red 0 0) no-repeat 
    0 60% / var(--s,0%) .1em,
    #000;
  -webkit-background-clip: border-box, text;
          background-clip: border-box, text;
  color: #0000;
  transition: background-size .4s ease;
  font-size: 1.5em;
}

span:hover {
  --s: 100%;
}
<span>
  <a href="#">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</a> At vero eos et accusam et justo duo dolores et ea rebum.
</span>

18
  • What do you mean by more color? means instead of red you need other color for strikethrough? Mar 7 at 13:10
  • Do you mean you want the strike through color to be the same as the underlying text color?
    – A Haworth
    Mar 7 at 13:18
  • @Leothelion No. Because of the link within the span there are two text colors, one for the link and one for the normal text. The red line is behind the link text instead of in front of it. Mar 7 at 15:15
  • @AHaworth No. Because of the link within the span there are two text colors, one for the link and one for the normal text. The red line is behind the link text instead of in front of it. Mar 7 at 15:16
  • 2
    @ThreeYearOld so you want to have red line in front of text? check this plz jsfiddle.net/ky1Lagv5 Mar 7 at 17:16

5 Answers 5

0

As after discussion in the comment box, here is the solution.

Just use position:relative on the text and put z-index:-999 so your text will be behind the red line(strikethrough).

Working example :

span {
  background: 
    linear-gradient(to bottom, red 0 0) no-repeat 
    0 60% / var(--s,0%) .1em,
    #000;
  -webkit-background-clip: border-box, text;
          background-clip: border-box, text;
  color: #0000;
  transition: background-size .4s ease;
  font-size: 1.5em;
 
  
}

span:hover {
  --s: 100%;  
}

a{
  position:relative;
  z-index:-999;
}
<span>
  <a href="#">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</a> At vero eos et accusam et justo duo dolores et ea rebum.
</span>

Hope it helps you.

11
  • Thank you! Is there even a solution without the nasty z-index? Keyword: accessibility. Mar 7 at 18:07
  • But this seems to get rid of the color on the anchor element, which I had thought was the difficulty.
    – A Haworth
    Mar 7 at 18:22
  • @AHaworth sorry but did not get you? You want to remove the color of a tag too? or what? Mar 7 at 20:28
  • With z-index:-999 the link is no longer clickable. Mar 10 at 19:48
  • Sorry din see that you need animation too. Working on it. Mar 11 at 16:36
0

This snippet uses currentcolor to color the background and text-fill-color to set the transparency and applies the background to spans children as well.

However, this is not a perfect answer because it seems that text-decoration-color does not pick up currentcolor if the text has been filled with another color so underline is lost. It’s reinstated here by being given a definite color.

span {
  font-size: 1.5em;
}
span, span *{
  text-decoration-color: purple;
  /* text-decoration-color: currentcolor;
  that use of currentcolor doesn’t work
     at least on IOS 16… Safari for me */
  background: 
    linear-gradient(red 0 0) no-repeat 
    0 60% / var(--s,0%) .1em,
    currentcolor;
  -webkit-background-clip: border-box, text;
          background-clip: border-box, text;
  -webkit-text-fill-color: #0000;
  text-fill-color: #0000;
  transition: background-size .4s ease;    
}

span:hover {
  --s: 100%;
}
<span>
  <a href="#">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</a> At vero eos et accusam et justo duo dolores et ea rebum.
</span>

1
  • This will duplicate the background and the strike-through as well. Use a slow a transition to notice it: jsfiddle.net/zuL2agcj Apr 19 at 7:00
0

Update your code like below:

span.strike {
  background: 
    linear-gradient(red 0 0) no-repeat 
    0 60% / var(--s, 0%) .2em;
  transition: background-size .4s ease;
  font-size: 1.5em;
  isolation: isolate; /* create a stacking context */
  pointer-events: none; /* disable mouse events */
}
span * {
  pointer-events: initial; /* re-enable mouse event on child elements */
  position: relative;
  z-index: -1; /* put link behind strikethrough */
}

span:hover {
  --s: 100%;
}
<span class="strike">
  <a href="#">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</a> <span>At vero eos et accusam et justo duo dolores et ea rebum.</span>
</span>

2
  • 1
    thank you for your answer but can you please put little bit detail that what did the magic part to solve the issue? Like i am not aware of pointer-events or isolation so it will be great if you will add little bit detail. Thank you Apr 19 at 5:06
  • any chance to get the details about your answer? 4 hours ago
0

In this solution, I've doubled the text so the colored link can be below the span and the clickable clone is rendered transparently atop it. Therefore, you see the colored link below the colored strikethrough line but you can still click it.

div {
  font: bold 2em serif;
  width:98%;
}

div.over {
  position: absolute;
}

div.over span {
  background: 
    linear-gradient(90deg, red, yellow, green, blue, magenta) no-repeat 
    0 60% / var(--s,0%) .1em,
    transparent;
  transition: background-size .4s ease;
}

div.over span:hover {
  --s: 100%;
}

div.under {
  position: relative;
  z-index: -1;
}

div.over :is(span, span *) {
  color:transparent;
}
<div class="over"><span>
  <a href="#">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</a> At vero eos et accusam et justo duo dolores et ea rebum.
</span></div>
<div class="under"><span>
  <a href="#">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</a> At vero eos et accusam et justo duo dolores et ea rebum.
</span></div>

This is made possible with containing <div> elements using varying z-index values. This lets us put elements above or below others. Everything defaults to zero, so I've put the colored link below zero and left the clickable link at the default depth. z-index requires position:relative (keep things where they are) or position:absolute (make the element independent of its parents, render things around it as if it does not exist). It shouldn't matter that I chose the "over" copy to be absolute and the "under" copy to be relative, but the first must be absolute (to not affect the flow) and the second must be relative (to keep the flow correct).

There's a little oddity in which the absolute <div> doesn't wrap the same way, but I solved that by forcing them both to width:98%. A static value would be more reliable.

I initially misread your request as wanting a rainbow of colors for the strike-through effect, which is as simple as changing the parameters in the linear-gradient() function. It's pretty, so I kept that. You can revert it by going back to linear-gradient(red 0 0).

You'll also notice that I removed a bunch of unnecessary CSS cues (color and background-clip). If that breaks things, put them back. Finally, I increased the font to be larger and bolder so you can more clearly see what's on top of what.

3
  • thank you so much for your time and help but there is issue with link. If text after At vero is in one line then whole line is clickable while only link should be clickable. I only want link to clicable but still thank you. :) Apr 18 at 5:04
  • sorry but i did not gave downvote. not sure why some one did. Apr 18 at 7:37
  • 1
    Okay, I solved the block issue. The link is now properly scoped.
    – Adam Katz
    Apr 18 at 16:45
0

Not many changes are needed. Just replace the background's #000 fallback color with the currentcolor and add a -webkit-text-fill-color entry in place of color.

While color changes all of the text, -webkit-text-fill-color affects only the parts that overlap what was clipped with background-clip (the strikethrough line). I prefer transparent over #0000, but they are technically identical (#0000 is black but with zero opacity, which is transparent, just like #fff0).

span, span * {
  background: 
    linear-gradient(red 0 0) no-repeat 
    0 60% / var(--s,0%) .1em,
    currentcolor;
  background-clip: border-box, text;
  -webkit-text-fill-color: transparent;
  transition: background-size .4s ease;    
  font: bold 1.5rem serif;
}

span:hover {
  --s: 100%;
}
<span>
  <a href="#">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</a> At vero eos et accusam et justo duo dolores et ea rebum.
</span>

I've made the text bold so you can more clearly see that the strikethrough line is rendered atop the text. I changed the units to ensure it's not as relative; span, span * { font-size:1.5em } would mean children are half again as big as the container. 1.5rem is instead relative to the document root.

(You may notice that I've removed -webkit-background-clip and I don't include text-fill-color or -moz-text-fill-color. That's because background-clip is now fully supported without a vendor prefix and text-fill-color isn't (yet?) a proposal; it's supported only with the -webkit vendor prefix, even on Firefox.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.