This solution may not be perfect as it relies heavily on fixed positions and background sizes, but it does serve as a quick workaround.
The text within each span element seems to be cut out from the black rectangles around them.
In reality, every span
text has its unique background that aligns precisely with the gradient background of the underlying h1
element.
h1 {
position: relative;
display: block;
width: 200px;
height: 200px;
background-image: linear-gradient(
red,orange,yellow,green,blue,indigo,violet
);
}
h1 span {
display: block;
position: relative;
top: 10px;
width: 180px;
height: 50px;
margin: 20px 10px;
line-height: 50px;
font-size: 40px;
text-align: center;
background-color: rgb(0, 0, 0);
}
h1 span::after {
position: absolute;
top: 0;
left: 0;
width: 100%;
background-image: linear-gradient(
red,orange,yellow,green,blue,indigo,violet
);
background-size: 200px 200px;
background-clip: text;
-webkit-text-fill-color: transparent;
}
h1 span:nth-of-type(1)::after {
content: 'Example';
background-position: -20px -20px;
}
h1 span:nth-of-type(2)::after {
content: 'Text';
background-position: -20px -80px;
}
<h1>
<span>Example</span>
<span>Text</span>
</h1>