Initial Query:
How can you achieve the effect of text with different colors on each side without duplicating content?
Conventional Approach:
A common method involves creating two separate text elements and positioning them side-by-side, each with its unique background and foreground colors.
Alternative Solution:
To minimize content duplication, consider leveraging CSS properties like background-clip:text. This property allows you to apply a gradient to the text itself, enabling the following:
#main { background: linear-gradient(to right, red 50%, #fff 50%); } #main > p { background: linear-gradient(to left, blue 50%, #fff 50%); display: inline-block; -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; color: transparent; }
<div>
element has a reversed gradient applied to its background, which is set as the text's background color.
This technique creates a dual-color text effect without requiring duplicated content. By using blending effects, it achieves a seamless transition between colors without the need for additional text elements.
The above is the detailed content of How can you achieve dual-color text with CSS without duplicating content?. For more information, please follow other related articles on the PHP Chinese website!