Home  >  Article  >  Web Front-end  >  A brief analysis of how to implement linear gradient (linear-gradient) in CSS

A brief analysis of how to implement linear gradient (linear-gradient) in CSS

青灯夜游
青灯夜游forward
2022-04-02 11:22:403633browse

How to implement linear gradient in CSS? The following article will introduce to you how to use the CSS linear-gradient() linear gradient function, and talk about the various applications of linear gradients. I hope it will be helpful to everyone!

A brief analysis of how to implement linear gradient (linear-gradient) in CSS

linear-gradient

1. Syntax

linear- gradient([[to <direction>|<angle>],]? <color stop>, <color stop>[, ...]?)</color></color></angle></direction>

-webkit-linear-gradient([[<direction>|<angle>],]? <color stop>, <color stop>[, ...]?)</color></color></angle></direction>

These two types are different in usage and expression. When using direction, the former should be accompanied by to, and the latter without; use angle, the performance is inconsistent. [Recommended learning: css video tutorial]

1) Default

Both defaults are from top to bottom

background-image:linear-gradient(#00ffff, #ff1493, #006699);
background-image:-webkit-linear-gradient(#00ffff, #ff1493, #006699)

A brief analysis of how to implement linear gradient (linear-gradient) in CSS

2) <direction>: [left|right]|[top|bottom]</direction>Use of both

The opposite direction of expression

background-image:linear-gradient(to left, #00ffff, #ff1493, #006699);
background-image:-webkit-linear-gradient(left, #00ffff, #ff1493, #006699);

A brief analysis of how to implement linear gradient (linear-gradient) in CSS

background-image:linear-gradient(to left top, #00ffff, #ff1493, #006699);
background-image:-webkit-linear-gradient(left top, #00ffff, #ff1493, #006699);

A brief analysis of how to implement linear gradient (linear-gradient) in CSS

##3) The use of

Correspondence between degree and direction. -webkit-The corresponding direction is 450°-angle

##

background-image:linear-gradient(275deg, #ff1493, #000000, #006699);
background-image:-webkit-linear-gradient(175deg, #ff1493, #000000, #006699);
A brief analysis of how to implement linear gradient (linear-gradient) in CSS

450°-175° =275°

, so the two behave the same, as shown below:

A brief analysis of how to implement linear gradient (linear-gradient) in CSS

4)

= The use of

stop

can use a percentage or a specific value to indicate that this color reaches saturation## at this position #

background-image:linear-gradient(to right, #ff1493 10%, #000000 40%, #006699 60%);
background-image:-webkit-linear-gradient(to right, #ff1493 10%, #000000 40%, #006699 60%);

You can see the color change process from the picture above: A brief analysis of how to implement linear gradient (linear-gradient) in CSS

0% --> 10%: #ff1493

Has been

saturated10% --> 40%: #ff1493

gradually changes to

#000000, at 40%# At ##, #000000 reaches saturation 40% --> 60%: #000000 gradually becomes

#006699

, at 60%, #006699 reaches saturation##60% --> 100%: # 006699Always in Saturation

Using this feature, you can draw stripes

  background-image:linear-gradient(to right, #ff1493 33%, #000000 33%, #000000 66%, #006699 66%);
  background-image:-webkit-linear-gradient(to right, #ff1493 33%, #000000 33%, #000000 66%, #006699 66%);

Note:

stopA brief analysis of how to implement linear gradient (linear-gradient) in CSSYou can also set 2 values ​​at the same time, such as

linear-gradient(to right, #ff1493 0% 33%, #000000 33% 66%, #006699 66% 100%);-webkit- linear-gradient(to right, #ff1493 33%, #000000 33% 66%, #006699 66% 100%);

, the effect is consistent with the picture above. If the latter value is smaller than the former, the former shall prevail. As follows, 20px is smaller than 60px

, and is actually displayed as

60px. The effect is as shown below:

background-image:linear-gradient(right, #ff1493 60px, #000000 20px);
background-image:-webkit-linear-gradient(right, #ff1493 60px, #000000 20px);
##Extension 1: Gradient center

A brief analysis of how to implement linear gradient (linear-gradient) in CSSThe default is the center of 2 colors, but we can set it Gradient center

/* 3种颜色平分,渐变中心为1/3和2/3处 */
background-image:linear-gradient(to right, #ff1493, #000000, #006699); 
/* 渐变中心在10%和20%处 */
background-image:linear-gradient(to right, #ff1493, 10%, #000000, 20%, #006699);

Note: -webkit-linear-gradient

does not support this usage

Extension 2: repeating-linear-gradient

A brief analysis of how to implement linear gradient (linear-gradient) in CSSWe can use the attribute to draw repeated color blocks

background-image:repeating-linear-gradient(0deg, #ff1493, #000000 10px, #006699 20px);
background-image:-webkit-repeating-linear-gradient(0deg, #ff1493, #000000 10px, #006699 20px)

2. Commonly used styles

A brief analysis of how to implement linear gradient (linear-gradient) in CSS

(1) Multi-color starry sky

background-image:
    linear-gradient(45deg, rgba(255, 0, 76, 0.7), rgba(0, 0, 255, 0) 80%),
    linear-gradient(135deg, rgba(106, 0, 128, 1), rgba(0, 128, 0, 0) 80%),
    linear-gradient(225deg, rgba(0, 255, 255, 1), rgba(0, 255, 255, 0) 80%),
    linear-gradient(315deg, rgba(255, 192, 203, 0.7), rgba(255, 192, 203, 0) 80%);
Set multiple values ​​at the same time to make the entire background color look more gorgeous

(2) Plaid pattern

background-image:
    repeating-linear-gradient(0deg, rgba(0, 255, 255, 0.3) 0px 5px, transparent 5px 10px),
    repeating-linear-gradient(90deg, rgba(0, 255, 255, 0.3) 0px 5px, transparent 5px 10px);
    
background-image:
    repeating-linear-gradient(45deg, rgba(0, 255, 255, 0.3) 0px 5px, transparent 5px 10px),
    repeating-linear-gradient(135deg, rgba(0, 255, 255, 0.3) 0px 5px, transparent 5px 10px);
1A brief analysis of how to implement linear gradient (linear-gradient) in CSSUse color and transparent color to alternately render

(3)边框渐变

<div id="wrap"></div>
<style>
 #wrap {
    width: 180px;
    height: 40px;
    border: 5px solid transparent;
    border-image: linear-gradient(45deg, aqua, pink, purple) 1;
}

</style>

内部背景透明,但是不支持设置border-radius

1A brief analysis of how to implement linear gradient (linear-gradient) in CSS

<div id="wrap"></div>
<style>
 #wrap {
    width: 180px;
    height: 40px;
    border: 5px solid transparent;
    border-image: linear-gradient(45deg, aqua, pink, purple) 1;
    clip-path: inset(0 round 5px);
}

</style>

注:可以使用clip-path裁剪出圆角, 但是这种方式不适用于角度较大的圆角

1A brief analysis of how to implement linear gradient (linear-gradient) in CSS

<div id="wrap">
    <div id="content"></div>
</div>
<style>
#wrap {
    width: 180px;
    height: 40px;
    border-radius: 20px;
    background: #FFF;
    position: relative;
}

#wrap::before {
    content: &#39;&#39;;
    position: absolute;
    left: -5px;
    right: -5px;
    top: -5px;
    bottom: -5px;
    background-image: linear-gradient(45deg, aqua, pink, purple);
    border-radius: 25px;
    z-index: -1
}

/*或者*/

#wrap {
    width: 180px;
    height: 40px;
    border-radius: 20px;
    background: #FFF;
    position: relative;
    border: 5px solid transparent;
    background-origin: border-box;
    background-image: linear-gradient(#FFF, #FFF), linear-gradient(45deg, aqua, pink, purple);
    background-clip: padding-box, border-box;
}

/*或者*/

#wrap {
    width: 180px;
    height: 50px;
    border: 5px solid transparent;
    border-radius: 25px;
    background-image: linear-gradient(45deg, aqua, pink, purple);
    background-origin: border-box;
}

#content {
    width: 100%;
    height: 100%;
    border-radius: 20px;
    background: #FFF;
}

</style>

这几种方式都能做到圆角渐变边框,但是无法做到内部背景透明

1A brief analysis of how to implement linear gradient (linear-gradient) in CSS

(4)文字渐变

<div id="wrap"> Darker CMJ</div>
<style>
#wrap {
    font-size: 40px;
    line-height: 40px;
    font-weight: bold;
    background-clip: text;
    -webkit-background-clip: text;
    // color: transparent;
    -webkit-text-fill-color: transparent;
    background-image: linear-gradient(45deg, aqua, pink, purple);
}
</style>

background-clip规定背景的绘制区域,我们设置其值为text,就是在文字区域绘制,然后将文字color或者-webkit-text-fill-color设置为透明色,渐变区域就能显示出来了

1A brief analysis of how to implement linear gradient (linear-gradient) in CSS

好了,over,第一次写文章,希望能坚持下去=.=

(学习视频分享:web前端

The above is the detailed content of A brief analysis of how to implement linear gradient (linear-gradient) in CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete