css之为文本添加线性渐变和外描边

时间:2019-03-15
本文章向大家介绍css之为文本添加线性渐变和外描边,主要包括css之为文本添加线性渐变和外描边使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

css之为文本添加线性渐变和外描边

 一、效果:

描边:描边+渐变:

二、描边:

api:text-stroke

问题:text-stroke的描边是居中描边,无法直接设置外描边

解决:在before中添加文本,设置字体描边,绝对定位在文本下方

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{background:blue}
        h2 {
            font-size: 31px;
            font-weight: 800;
            color: #70b4d9;
            position: relative;
            z-index: 1;
        }
        h2::before {
            content: attr(data-text);
            position: absolute;
            -webkit-text-stroke: 6px #fff;
            z-index: -1;
        }
    </style>
</head>
<body>
    <h2 data-text="数据采集">数据采集</h2>
</body>
</html>

三、添加渐变

api:

background-image:-webkit-gradient(linear,0% 0%, 0% 100%, from(#999999), to(#333333), color-stop(0.5,#336600));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;

原理:字体渐变的原理是将背景图片剪切为字体,然后将字体颜色取消。

问题:字体渐变的实质是背景图片,所以无法在其下面再垫一层背景

解决:给文本套一层span,使渐变为span的背景,然后在父标签垫描边背景。

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{background:blue}
        h2 {
            font-size: 31px;
            font-weight: 800;
            color: #70b4d9;
            position: relative;
            z-index: 1;
        }
        h2>span{
            background-image:-webkit-gradient(linear,0% 0%, 0% 100%, from(#999999), to(#333333), color-stop(0.5,#336600));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        h2::before {
            content: attr(data-text);
            position: absolute;
            -webkit-text-stroke: 6px #fff;
            z-index: -1;
        }
    </style>
</head>
<body>
    <h2 data-text="数据采集"><span>数据采集</span></h2>
</body>
</html>

  

钻研不易,转载请注明出处、、、、、、