简体   繁体   English

背景图像的替代解决方案是什么:重复线性渐变

[英]What will be an alternative solution for background-image: repeating-linear-gradient

I am trying to assign different colors to each line using background-image: repeating-linear-gradient .我正在尝试使用background-image: repeating-linear-gradient为每一行分配不同的 colors 。

 div { --spac: 2em; line-height: var(--spac); color: white; background-image: repeating-linear-gradient(red 0 var(--spac), green var(--spac) calc(var(--spac) * 2)); }
 <div>Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network,[4][5][6] created in 2008 by Jeff Atwood and Joel Spolsky.[7][8] It features questions and answers on a wide range of topics in computer programming.[9][10][11] It was created to be a more open alternative to earlier question and answer websites such as Experts-Exchange. Stack Overflow was sold to Prosus, a Netherlands-based consumer internet conglomerate, on 2 June 2021 for $1.8 billion.[12]</div>

It works fine, but I just wonder if there are any ways I could make the div area that doesn't have text to become transparent (make the area in the photo circle with black to become transparent).它工作正常,但我只是想知道是否有任何方法可以使没有文本的 div 区域变得透明(使照片圈中的黑色区域变得透明)。

在此处输入图像描述

Another solution I could think of is to split the text based on line break and assign different colors to different lines using for loop, but this is too messy.我能想到的另一个解决方案是根据换行符split文本,并使用for循环将不同的 colors 分配给不同的行,但这太混乱了。

I think for background-image: repeating-linear-gradient , there won't be some easy solutions since background-image: repeating-linear-gradient assign the background-color based on the width of the element, not the text.我认为对于background-image: repeating-linear-gradient ,不会有一些简单的解决方案,因为background-image: repeating-linear-gradient根据元素的宽度而不是文本分配背景颜色。 (If there is, please correct me and tell me!) (如果有,请纠正我并告诉我!)

What will be alternative solutions to solve this?解决此问题的替代解决方案是什么?

If you are ready for some hacks, here is one.如果您准备好进行一些黑客攻击,这里有一个。 The trick is to have a white color that covers that area:诀窍是让白色覆盖该区域:

 .box { --spac: 2em; line-height: var(--spac); color: white; background-image: repeating-linear-gradient(red 0 var(--spac), green var(--spac) calc(var(--spac) * 2)); overflow:hidden; }.box:after { content:""; display:inline-block; vertical-align:top; clip-path:inset(0 -100vmax -100vmax 0); box-shadow:0 0 0 100vmax #fff; }
 <div class="box">Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network,[4][5][6] created in 2008 by Jeff Atwood and Joel Spolsky.[7][8] It features questions and answers on a wide range of topics in computer programming.[9][10][11] It was created to be a more open alternative to earlier question and answer websites such as Experts-Exchange. Stack Overflow was sold to Prosus, a Netherlands-based consumer internet conglomerate, on 2 June 2021 for $1.8 billion.[12]</div>

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

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