简体   繁体   English

CSS中的嵌套直通

[英]Nested line-through in css

In my scenario, I have sections of text with deleted sections, visualized by line-through . 在我的场景中,我有一些带有删除部分的文本部分,通过line-through可视化。 Sometimes, these sections are nested. 有时,这些部分是嵌套的。 I would like to produce an output like this (please run snippet): 我想产生这样的输出(请运行代码段):

 span.strike1 { text-decoration:line-through; text-decoration-style:solid; } span.strike2 { text-decoration:line-through; text-decoration-style:double; } 
 <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <span class="strike1">Aenean commodo ligula eget dolor. </span><span class="strike2">Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.</span><span class="strike1"> Nulla consequat massa quis enim.</span> Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</p> 

However, I would like to reproduce the same result with nested elements. 但是,我想使用嵌套元素重现相同的结果。 Without applying javascript, is this actually achievable? 如果不使用JavaScript,这实际上可以实现吗? Nesting text-decoration-style:double into text-decoration-style:solid will produce a triple line (double + solid), see here: text-decoration-style:double嵌套到text-decoration-style:solid中将产生三行(double + solid),请参见此处:

 span.strike { text-decoration:line-through; text-decoration-style:solid; } span.strike span.strike { text-decoration:line-through; text-decoration-style:double; } 
 <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <span class="strike">Aenean commodo ligula eget dolor. <span class="strike">Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.</span> Nulla consequat massa quis enim.</span> Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</p> 

Further, it seems impossible to influence the position of text-decoration . 此外,似乎不可能影响text-decoration的位置。 I have also tried a workaround with border and :after , but this does not work with more than one line. 我还尝试了border:after的解决方法,但这不适用于多行。 I appreciate any help. 感谢您的帮助。

You could use a background linear-gradient painted via currentcolor to match text color instead text-decoration : 您可以使用通过currentcolor绘制的background linear-gradient来匹配文本color而不是text-decoration

 p { line-height:1.6em; font-size:16px; } span.strike {background:linear-gradient( to top, transparent 35%, currentcolor 35%, currentcolor calc(35% + 1px ) , transparent calc(35% + 1px ) ); } span.strike span.strike { background:linear-gradient( to top, transparent 5px, currentcolor 5px, currentcolor 6px , white 6px, /* hide other bg */ white 9px, /* hide other bg */ currentcolor 9px, currentcolor 10px, transparent 10px ); } /* why currentcolor ? , hover tripleed striked span */ span span:hover { color:purple; } 
 <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <span class="strike">Aenean commodo ligula eget dolor. <span class="strike">Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.</span> Nulla consequat massa quis enim.</span> Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</p> 

@BoltClock says : This works provided the background is a solid color and you don't mind the less-than-precise "lines" ;) @BoltClock说: 如果背景是纯色,并且您不介意不够精确的“线条”,则可以使用此功能;)

Unfortunately, it is not possible to prevent decorations from being drawn over descendant inline boxes (see CSS2.2 and css-text-decor-3 ). 不幸的是,无法防止在后代内联框上绘制装饰(请参阅CSS2.2css-text-decor-3 )。 Your only recourse, if you don't want to (or can't) cheat by drawing fake decorations yourself, is splitting your outer .strike element at the boundaries of the inner .strike element in such a way as to produce a structure similar to your reference example. 如果您不想(或无法)自己绘制假装饰来作弊,唯一的办法就是在内部.strike元素的边界处拆分外部.strike元素,以产生类似的结构参考示例。

I have found another solution that works well in my case. 我发现了另一种适用于我的情况的解决方案。 I post it here as it might be of interest for others. 我将其发布在这里,因为它可能会让其他人感兴趣。 While the background-gradient solution displayed excellently in browsers, it did not work well when printing the document. 尽管背景渐变解决方案在浏览器中显示出色,但是在打印文档时效果不佳。 This solution separates the second strikethrough line from its text by surrounding it by another <span> element, which is moved a little upwards by scaling the font size. 此解决方案通过用另一个<span>元素将第二条删除线与其文本分开,该元素通过缩放字体大小向上移动一点。 The font size is then reset in the nested span element to display the text correctly. 然后,在嵌套的span元素中重置字体大小以正确显示文本。

 p { line-height:1.5em; } span.strike { text-decoration:line-through; text-decoration-style:solid; } span.strikeLine { text-decoration:line-through; text-decoration-style:solid; font-size:1.43em; line-height:0; } span.strikeText { font-size:0.7em; line-height:1.5em; } 
 <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <span class="strike">Aenean commodo ligula eget dolor. <span class="strikeLine"><span class="strikeText">Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.</span></span> Nulla consequat massa quis enim.</span> Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM