Clipping is an excellent solution.
But if you have the freedom of applying the gradient on the text of h2
, then it can be done with a little switcheroo trick.
h2 {
background: linear-gradient(30deg, #adf175 50%, #FFF 50%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
Basically, one applies the linear-gradient
background on the text element , h2
in this case, and use background-clip
property to clip the background to extend to the text only . Finally use the text-fill-color
to set the color of the h2 to transparent
I had just reversed the gradient colors from the question above for the h2 and the div .
More info can be seen here
body {
font-size: 16px;
font-family: Verdana, sans-serif;
}
.wrap {
width: 50%;
margin: 0 auto;
border: 1px solid #ccc;
text-align: center;
background: linear-gradient(30deg, #FFF 50%, #adf175 50%);
}
h2 {
background: linear-gradient(30deg, #adf175 50%, #FFF 50%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
<div class="wrap">
<h2>Hello World</h2>
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…