Consider how you can achieve a gradient similar to the following text in your Web page.
The traditional implementation is to use a transparent gradient of the picture overlay on the text. The concrete realization Way can refer to http://www.qianduan.net/css-gradient-text-effect.html. The advantage of this way is that the picture can be controlled, so it can achieve a very complex gradient effect, but the disadvantage is that the image gradient must be consistent with the background color, while the loss of mouse clicks, text selection and other events.
The improved method can use the CSS3 background gradient-webkit-gradient to replace the picture with a DIV with a background gradient. The following is an example of implementation, compared to the advantages of the above scenario is not to use the picture, reduce the volume of requests and traffic, but for these shortcomings, still cannot solve.
Is there a perfect solution?
The following describes the use of the-webkit-mask mask scheme to achieve the text gradient, completely avoid the above scenario deficiencies. Here are the perfect implementations:
Now let's start CSS3 the Text Gradient tour.
1. Build HTML content and basic styles:
We use a H1 tag to package a label:
View Plaincopy to Clipboardprint?
-
JavaScript code
-
The style is defined as follows, and we use Text-shadow to add a shadow to the text:
View Plaincopy to Clipboardprint?
- H1 {
- Font-family:segoe UI, Verdana, Sans-serif;
- font-size:100px;
- line-height:100px;
- Text-shadow: -3px 0 4px #006;
- }
- H1 A:link,
- H1 a:visited,
- H1 A:hover,
- H1 A:active {
- Color: #d12;
- Text-decoration:none;
- }
JavaScript code
- H1 {
- Font-family:segoe UI, Verdana, Sans-serif;
- font-size:100px;
- line-height:100px;
- Text-shadow: -3px 0 4px #006;
- }
- H1 A:link,
- H1 a:visited,
- H1 A:hover,
- H1 A:active {
- Color: #d12;
- Text-decoration:none;
- }
The basic effects are as follows:
2. Add a gradient effect:
We add a linear gradient to the text through the CSS3 mask property. Compared with the background gradient, it can be understood that background is behind the text, and mask is superimposed on the text. Mask can be set to normal color, linear gradient, radial gradient, or picture.
The code is as follows:
View Plaincopy to Clipboardprint?
- H1 A:link,
- H1 a:visited,
- H1 A:hover,
- H1 A:active {
- Color: #d12;
- Text-decoration:none;
- -webkit-mask-image:-webkit-gradient (linear, left top, left bottom, from (Rgba (0,0,0,1)), to (Rgba (0,0,0,0)));
- }
JavaScript code
- H1 A:link,
- H1 a:visited,
- H1 A:hover,
- H1 A:active {
- Color: #d12;
- Text-decoration:none;
- -webkit-mask-image:-webkit-gradient (linear, left top, left bottom, from (Rgba (0,0,0,1)), to (Rgba (0,0,0,0)));
- }
The effect is as follows:
3. The gradient is another color:
Because-webkit-gradient is actually handled as a picture, we cannot set the text color as a gradient to achieve the effect of a text color gradient to another color (you can try it if you don't believe it).
So we're going to construct a pseudo-element that, like our text, uses pseudo-elements because it avoids code redundancy by adding a label of the same content:
View Plaincopy to Clipboardprint?
- H1:after {
- Content: "Jiangyujie";
- Color: #000;
- text-shadow:3px 3px 1px #600;
- }
JavaScript code
- H1:after {
- Content: "Jiangyujie";
- Color: #000;
- text-shadow:3px 3px 1px #600;
- }
We then overlapped the two text with the Position property:
View Plaincopy to Clipboardprint?
- H1 {
- position:relative;
- Font-family:segoe, Verdana, Sans-serif;
- font-size:100px;
- line-height:100px;
- Text-shadow: -3px 0 4px #006;
- }
- H1 A:link,
- H1 a:visited,
- H1 A:hover,
- H1 A:active {
- Position:absolute;
- Text-decoration:none;
- top:0;
- Z-index:2;
- Color: #d12;
- -webkit-mask-image:-webkit-gradient (linear, left top, left bottom, from (Rgba (0,0,0,1)), to (Rgba (0,0,0,0)));
- }
JavaScript code
- H1 {
- position:relative;
- Font-family:segoe, Verdana, Sans-serif;
- font-size:100px;
- line-height:100px;
- Text-shadow: -3px 0 4px #006;
- }
- H1 A:link,
- H1 a:visited,
- H1 A:hover,
- H1 A:active {
- Position:absolute;
- Text-decoration:none;
- top:0;
- Z-index:2;
- Color: #d12;
- -webkit-mask-image:-webkit-gradient (linear, left top, left bottom, from (Rgba (0,0,0,1)), to (Rgba (0,0,0,0)));
- }
The effect is as follows:
4. Add Background:
The advantage of this approach is that we can customize the background completely unaffected by the gradient color. For example, we can add a background to the text, with the following effect:
Great effect, isn't it:)
5. Other:
For more information on CSS3 mask, refer to Webkit.org's article.