当前位置: 首页 > news >正文

CSS中linear-gradient 的用法

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


基本语法

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

方向写法

  1. 默认(从上到下)

    background: linear-gradient(red, blue);
    

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

  2. 使用关键字

    background: linear-gradient(to right, red, blue);
    

    → 从左到右渐变。

    background: linear-gradient(to bottom right, red, blue);
    

    → 从左上到右下渐变。

  3. 使用角度

    background: linear-gradient(45deg, red, blue);
    

    → 沿 45° 方向渐变。


颜色停靠点

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

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

重复渐变

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

background: repeating-linear-gradient(45deg, red 0, red 10px, blue 10px, blue 20px);

→ 形成条纹效果。


常见应用

  1. 按钮背景

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

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

http://www.lryc.cn/news/623605.html

相关文章:

  • 【Python】Python 面向对象编程详解​
  • 多线程—飞机大战(加入播放音乐功能版本)
  • macos 安装nodepad++ (教程+安装包+报错后的解决方法)
  • Sentinel和12.5米高程的QGIS 3D效果
  • scikit-learn/sklearn学习|套索回归Lasso解读
  • scikit-learn RandomizedSearchCV 使用方法详解
  • scikit-learn 中的均方误差 (MSE) 和 R² 评分指标
  • .NET 中的延迟初始化:Lazy<T> 与LazyInitializer
  • 『搞笑名称生成器』c++小游戏
  • Spring Cloud整合Eureka、ZooKeeper、原理分析
  • 云计算-k8s实战指南:从 ServiceMesh 服务网格、流量管理、limitrange管理、亲和性、环境变量到RBAC管理全流程
  • 【Kubernetes系列】Kubernetes中的resources
  • 脉冲计数实现
  • vue3 ref和reactive的区别和使用场景
  • Nightingale源码Linux进行跨平台编译
  • 数学建模 15 逻辑回归与随机森林
  • 大模型微调【2】之使用AutoDL进行模型微调入门
  • 工具测试 - marker (Convert PDF to markdown + JSON quickly with high accuracy)
  • 深入理解 uni-app 页面导航:switchTab、navigateTo、redirectTo、reLaunch 与 navigateBack
  • 回溯剪枝的 “减法艺术”:化解超时危机的 “救命稻草”(一)
  • 基于径向基函数神经网络的数据回归预测 RBF
  • 【Jenkins】02 - 自动化部署配置
  • Matlab数字图像处理——梯度稀疏性和泊松方程求解的反光/倒影去除系统
  • C#中List、Path、字符串操作等常用方法总结
  • Git登录配置的详细方法
  • Python入门第7课:异常处理机制:让你的程序更健壮(try-except详解)
  • uniapp中uni.showToast和 uni.showLoading同时使用时出现提示中断冲突问题。
  • 《告别 if-else 迷宫:Python 策略模式 (Strategy Pattern) 的优雅之道》
  • 【Tech Arch】Hive技术解析:大数据仓库的SQL桥梁
  • 在 Element UI 的 el-table 中实现某行标红并显示删除线