Home >Web Front-end >CSS Tutorial >A detailed look at CSS gradients, shadows, and filters

A detailed look at CSS gradients, shadows, and filters

WBOY
WBOYforward
2022-02-10 18:11:323963browse

This article brings you relevant knowledge about gradients, shadows and filters in css, including linear gradients, radial gradients, conical gradients and other related issues. I hope it will be helpful to everyone helpful.

A detailed look at CSS gradients, shadows, and filters

Recommended learning: css video tutorial

1. First introduction to CSS gradient

CSS Gradient is a special type of image represented by gradient, consisting of a gradual transition between two or more colors.

Three gradient types:

  • Linear: created by the linear-gradient() function
  • Radial: created by radial -gradient() function creates
  • Cone: created by conic-gradient() function

ps: You can also use repeating- The linear-gradient() and repeating-radial-gradient() functions create repeating gradients.

Gradient can be used anywhere image is used, such as in the background.

2. CSS linear gradient

2.1 Introduction to linear gradient

  • Instructions

    • To create a linear gradient, just Just specify two colors. These are called color stops. Specify at least two color stops. You can also set the starting point and direction (or angle) and the gradient effect.
  • Syntax

    • background-image: linear-gradient(direction(方向), color1(颜色值), color2(颜色值), ...);

2.2 Linear Gradient Application

2.2.1 Linear gradient - from top to bottom (default)

  • Code example

    • <style>
          .box {
              width: 300px;
              height: 100px;
              background-image: linear-gradient(red, yellow);
          }</style>
      
      
          <p></p>
      
    • The effect is as follows

A detailed look at CSS gradients, shadows, and filters

##2.2.2 Linear gradient - from left to right

  • Code example

      background-image: linear-gradient(to right, red, yellow);
    • The effect is as follows

    A detailed look at CSS gradients, shadows, and filters##2.2. 3 Linear Gradient - Diagonal

      Code Example
    • background-image: linear-gradient(to bottom right, red, yellow);
      • The effect is as follows

    A detailed look at CSS gradients, shadows, and filters2.2.4 Linear Gradient - Set Angle

      Description
    • If If you want more precise control over the direction of the gradient, you can set a specific angle for the gradient.
      • When using angles,
      • 0deg
      • represents the gradient direction from bottom to top, 90deg represents the gradient direction from left to right, and other positive angles are normal. clockwise direction. And a negative angle means counterclockwise.
    • Code example
    • background-image: linear-gradient(180deg, red, yellow);
      • The effect is as follows

    A detailed look at CSS gradients, shadows, and filters2.2.5 Linear Gradient - Using Multiple Color Stops

      Code Example
    • background-image: linear-gradient(red, yellow, green);
      • The effect is as follows

    ##2.2.6 Linear gradient - Repeat linear gradientA detailed look at CSS gradients, shadows, and filters

    Description
    • repeating-linear-gradient()
        The function is used to repeat linear gradients.
      Code example
    • background-image: repeating-linear-gradient(red, yellow 10px);
      • The effect is as follows

    3. CSS radial gradientA detailed look at CSS gradients, shadows, and filters

    3.1 Introduction to radial gradient

    Description
    • A radial gradient is similar to a linear gradient, except that the radial gradient radiates outward from the center point.

        You can specify the location of the center point, and you can also set them to be circular or elliptical.
      Syntax
    • background-image: radial-gradient(shape(设置形状,默认为椭圆形), size(最远角), position(中心), color1(颜色值), color2(颜色值));
      3.2 Radial Gradient Application
    3.2.1 Radial Gradient - Evenly spaced color scales (default)

    Code Example
    • <style>
          .box {
              width: 300px;
              height: 100px;
              background-image: radial-gradient(red, yellow, green);
          }</style>
      
      
          <p></p>
      
      • The effect is as follows
    ##3.2.2 Radial gradient - color scales with different spacing

    A detailed look at CSS gradients, shadows, and filters

    Code example

    • background-image: radial-gradient(red 10%, yellow 20%, green 50%);

        The effect is as follows
    3.2.3 Radial gradient-set shape

    A detailed look at CSS gradients, shadows, and filters

    Code example

    • /* 设置为圆形形状 */background-image: radial-gradient(circle, red 10%, yellow 20%, green 50%);

        The effect is as follows
    • ##3.2.4 Radial Gradient-Set the center of the gradient

    A detailed look at CSS gradients, shadows, and filters

    Instructions

      You can position the center of the gradient using keywords, percentages or absolute length, length and percentage value repetitions if there is only one, otherwise in order of position from left to right.
      • Code example
    background-image: radial-gradient(at 10% 30%, red 10%, yellow 20%, green 50%);
  • The effect is as follows
    • A detailed look at CSS gradients, shadows, and filters

      3.2.5 径向渐变-重复径向渐变

      • 代码示例

        • background-image: repeating-radial-gradient(black, black 5px, #fff 5px, #fff 10px);
        • 效果如下

        A detailed look at CSS gradients, shadows, and filters

      四、CSS 圆锥渐变

      4.1 介绍圆锥渐变

      • 说明

        • 圆锥渐变类似于径向渐变,两者均为圆形,并使用元素的中心作为色标的源点。
        • 它是围绕中心点按照扇形方向进行旋转的渐变。
      • 语法

        • background-image: conic-gradient(from angle(表示起始的角度,默认为从上到下) at position(设置圆锥中心点的位置), start-color(定义开始颜色), stop-color(定义结束颜色))

      4.2 圆锥渐变的应用

      4.2.1 圆锥渐变-顺时针方向旋转(默认方式)

      • 代码示例

        • <style>
              .box {
                  width: 300px;
                  height: 300px;
                  background-image: conic-gradient(red,yellow);
              }</style>
          
          
              <p></p>
          
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      4.2.2 圆锥渐变-设置渐变的中心点

      • 代码示例

        • background-image: conic-gradient(at 30% 20%, red,yellow);
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      4.2.3 圆锥渐变-使用多个色标

      • 代码示例

        • background-image: conic-gradient(red, orange, yellow, green, teal, blue, purple);
        • 效果如下

          A detailed look at CSS gradients, shadows, and filters

      4.2.4 圆锥渐变-重复圆锥渐变

      • 代码示例

        • background-image: repeating-conic-gradient(red 10%, yellow 20%);
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      五、CSS 渐变补充知识

      5.1 创建实线

      • 说明

        • 要在不同颜色之间创建一条单一的硬线,即不同颜色的颜色不是不同的,可以将不同的位置颜色设置为相同
      • 代码示例

        • background: linear-gradient(to bottom left, red 50%, yellow 50%);
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      5.2 使用透明度

      • 说明

        • 如需添加透明度,我们使用 rgba() 函数来定义色标。 rgba() 函数中的最后一个参数可以是 0 到 1 的值,它定义颜色的透明度:0 表示全透明,1 表示全彩色(无透明)。
        • 也可以使用 transparent参数,代表全透明。
      • 代码示例

        • background-image: linear-gradient(to right, transparent, red);
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      CSS 阴影

      一、初识 CSS 阴影

      CSS阴影主要的作用是可以让页面中的文字和元素具有立体的效果,从而被突出出来。

      两种阴影属性:

      • box-shadow:用于给元素添加阴影
      • text-shadow:用于给文本添加阴影

      ps:还有一个 filter 滤镜的函数drop-shadow()也可以添加阴影,它主要用于给透明图像的非透明部分添加阴影效果。

      二、box-shadow属性

      2.1 介绍box-shadow属性

      • 说明

        • 用于在元素的框架上添加阴影效果,还可以在同一个元素上设置多个阴影效果,用逗号隔开。
      • 语法

        • box-shadow: OffsetX(水平偏移), OffsetY(垂直偏移), Blur(模糊半径), Spread(扩展距离,阴影的尺寸), Color(阴影颜色), Position(阴影位置,默认在外部(outset));

      2.2 box-shadow属性的应用

      2.2.1 box-shadow属性-基本使用

      • 代码示例

        • <style>
              .box {
                  width: 300px;
                  height: 300px;
                  background-color: yellow;
                  box-shadow: 10px 10px;
              }</style>
          
          
              <p></p>
          
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      2.2.2 box-shadow属性-多重阴影与定向阴影

      • 代码示例

        • box-shadow: -5px 0 5px 0px #000, 0 -5px 5px 0px #000;
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      2.2.3 box-shadow属性-模拟边框

      • 代码示例

        • box-shadow: 0px 0px 0px 10px #000, 0px 0px 0px 20px red;
        • 效果如下

          A detailed look at CSS gradients, shadows, and filters

      2.2.4 box-shadow属性-内阴影

      • 代码示例

        • box-shadow: 0px 0px 30px 10px red inset;
        • 效果如下

          A detailed look at CSS gradients, shadows, and filters

      三、text-shadow属性

      3.1 介绍text-shadow属性

      • 说明

        • 为文字添加阴影,也可以添加多个阴影,用逗号隔开。
      • 语法

        • text-shadow: OffsetX(水平偏移), OffsetY(垂直偏移), Blur(模糊半径),  Color(阴影颜色));
      • 注意

        • text-shadow没有扩展距离属性值,阴影位置属性值。

      3.2 text-shadow属性的应用

      3.2.1 text-shadow属性-基本使用

      • 代码示例

        • <style>
              .box {
                  width: 300px;
                  height: 300px;
                  background-color: yellow;
                  text-shadow: 0px 0px 5px red;
              }</style>
          
          
              <p>hello world</p>
          
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      text-shadow属性基本与box-shadow属性一样,就不多举例了

      CSS 滤镜

      一、初识 CSS 滤镜

      滤镜这两个字我相信大家都很熟悉,平时爱自拍,拍照的同学肯定都会打开滤镜修饰一下图片吧,那么CSS滤镜也是这样,直接用filter属性来修饰图像。

      二、CSS 滤镜方法

      • blur():模糊

        • 可以任何长度单位,值为 0 显示原图,值越大越模糊
      • brightness():亮度

        • 百分比,可用 0~1 代替,值为 0 显示全黑,值为 100% 显示原图
      • contrast():对比度

        • 百分比,可用 0~1 代替,值为 0 显示全黑,值为 100% 显示原图
      • drop-shadow():阴影

        • 说明

          • box-shadow属性类似
          • 没有内阴影效果
          • 不能阴影叠加
        • 代码示例

          •   /* 代码示例 */
              <style>
                  .box1 {
                      width: 300px;
                      height: 300px;
                      border: 3px solid red;
                      box-shadow: 5px 5px 10px 0 black;
                  }
              
                  .box2 {
                      width: 300px;
                      height: 300px;
                      border: 3px solid red;
                      filter: drop-shadow(5px 5px 10px black);
                  }
              </style>
              
              
                  <p></p>
                  <p></p>
              

      A detailed look at CSS gradients, shadows, and filters

      • grayscale():灰度

        • 百分比,可用 0~1 代替,值为 0 显示原图,值为 100% 显示全灰
      • hue-rotate():色相旋转

        • 角度,值为 0 显示原图,值为 0~360deg 减弱原图色彩,
      • invert():反相

        • 百分比,可用 0~1 代替,值为 0 显示原图,值为 100% 完全反转原图色彩
      • opacity():透明度

        • 百分比,可用 0~1 代替,值为 0 显示透明,值为 100% 显示原图
      • saturate():饱和度

        • 百分比,可用 0~1 代替,值为 0 完全不饱和原图,值为 100% 显示原图
      • sepia():褐色

        • 百分比,可用 0~1 代替,值为 0 显示原图,值为 100% 显示褐色

      三、CSS 滤镜的应用

      3.1 CSS滤镜-将图片设置为灰色

      • 代码示例

        • <style>
              .box {
                  filter: grayscale(1);
              }</style>
          
          
              <p>
                  <img  alt="A detailed look at CSS gradients, shadows, and filters" >
              </p>
          
        • 效果如下

      A detailed look at CSS gradients, shadows, and filters

      具体的滤镜调制方法可以参照CSSgram的官网进行学习

      (学习视频分享:css视频教程web前端教程

      The above is the detailed content of A detailed look at CSS gradients, shadows, and filters. For more information, please follow other related articles on the PHP Chinese website!

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