CSS中linear-gradient 的用法

linear-gradientCSS 渐变背景 的一种写法,用于生成沿着直线方向的颜色过渡效果。它常和 backgroundbackground-image 属性一起使用。


基本语法

css 复制代码
background: linear-gradient(direction, color-stop1, color-stop2, ...);
  • direction(方向) :可以是角度(如 45deg)、关键字(如 to right, to bottom 等)。
  • color-stop(颜色停靠点):颜色值,可以指定位置(百分比或长度),决定渐变分布。

方向写法

  1. 默认(从上到下)

    css 复制代码
    background: linear-gradient(red, blue);

    → 从上方的红色过渡到底部的蓝色。

  2. 使用关键字

    css 复制代码
    background: linear-gradient(to right, red, blue);

    → 从左到右渐变。

    css 复制代码
    background: linear-gradient(to bottom right, red, blue);

    → 从左上到右下渐变。

  3. 使用角度

    css 复制代码
    background: linear-gradient(45deg, red, blue);

    → 沿 45° 方向渐变。


颜色停靠点

你可以通过百分比或长度控制颜色分布:

css 复制代码
background: linear-gradient(to right, red 0%, yellow 50%, green 100%);
  • 0%:最左侧是红色
  • 50%:中间是黄色
  • 100%:最右侧是绿色

重复渐变

使用 repeating-linear-gradient 可以创建重复的渐变:

css 复制代码
background: repeating-linear-gradient(45deg, red 0, red 10px, blue 10px, blue 20px);

→ 形成条纹效果。


常见应用

  1. 按钮背景

    css 复制代码
    button {
      background: linear-gradient(to right, #ff7e5f, #feb47b);
      border: none;
      padding: 10px 20px;
      color: white;
      border-radius: 8px;
    }
  2. 渐变分隔线

    css 复制代码
    hr {
      border: 0;
      height: 3px;
      background: linear-gradient(to right, transparent, #333, transparent);
    }

相关推荐
咩咩啃树皮7 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
阳光是sunny7 小时前
LangGraph中的Reducer是什么
前端·人工智能·后端
触底反弹7 小时前
一文搞懂 Tailwind CSS 弹性布局:从原理到实战
前端·css·html
阳光是sunny7 小时前
从链到图:LangGraph 入门基础全解析
前端·人工智能·后端
小林ixn8 小时前
从 ??= 到 onKeyDown:一个 React 组件的“自我修养”
前端·javascript·react.js
REDcker8 小时前
显示分辨率标准对照详解
前端·网络·分辨率·显示·屏幕
এ慕ོ冬℘゜8 小时前
前端实战:jQuery 多条件联合搜索(标题模糊查询 + 日历时间段筛选)
前端·javascript·jquery
武子康9 小时前
Search Console Platform Properties 扩大 SEO 资产边界:从 Page Ranking 到 Topic Coverage(5 类误读边界 + 3 表数据层设计)
前端·人工智能·后端
一点一木9 小时前
从60首歌到1个网站:输入你的故事,还你一首歌
前端·github
IT_陈寒10 小时前
Vue这个特性差点让我加班到凌晨,谁懂啊
前端·人工智能·后端