-webkit-background-clip: text; This means that if you crop out the text inside the box as the clipping area, everything outside the text will be clipped out.

First: Set a gradient for the outermost div.

Use webkit-background-clip: text; The text of div is clipped outwards as the clipping area;

Finally: make the text transparent: use color: transparent; This can achieve text gradient effect

<! DOCTYPE html><html>
	<head>
		<meta charset="utf-8">
		<title>Text gradient</title>
		<style type="text/css">
			#test{
				margin: 0 auto;
				width: 300px;
				height: 100px;
                                /* * * */
				background: -webkit-linear-gradient(left,red,orange,blue,green);
				-webkit-background-clip: text;
				color: transparent;
			}
		</style>
	</head>
	<body>
		<div id="test">Test text Test text Test text Test text test text</div>
	</body>
</html>
Copy the code