Can someone help me on how to achieve this dynamic shifted color box of a text container in CSS? It should be dynamic to handle long headlines breaking into two lines.
If CSS can’t handle this, I am open to JS solutions.
h1 span {
color: black;
background-color: lightblue;
}
<h1><span>HEADLINE<br>LONGER</span></h1>
With a span I can limit the background color to the actual length of the text, but I don’t know how to shift it ?
tried this and was closest I can get, but can’t wrap my head around the break of text also, as markup won’t work in content property:
h1 span:before {
content:"'HEADLINE'+'<br/>'+'LONGER'";
color: #F4BEBE;
background-color: #F4BEBE;
top: 40px;
left: 20px;
position: absolute;
z-index: -1;
}
<h1><span>HEADLINE<br>LONGER</span></h1>
2
Answers
Hi I think you’re almost there. Here’s an hack I tried out. I know there must be some other solution out there but this is quick and hacky if you don’t mind. I added comments in the code. Take care
You can use a combination of
box-decoration-break
andlinear-gradient()
/background-position
: