CSS绘制卡券、优惠券样式

一般做商城类的项目,为了商品促销,都会有优惠券的需求,如果有UI组可以切图那能省不少功夫,如果没有,那么我们自己也可使用CSS画出这些仿淘宝、京东的优惠券样式。

radial-gradient()

使用radial-gradient径向渐变函数,可以实现一个圆形镂空的样子,在移动端,radial-gradient的兼容性也是相当完美。

CSS 语法

background: radial-gradient(shape size at position, start-color, ..., last-color);

单个圆形镂空效果如下:

css 复制代码
.base-one-circle {
    width: 100px;
    height: 100px;
    position: relative;
    background: radial-gradient(circle at 0px 50px, transparent 10px, #28A4F2 0) top
  }

圆形的位置也可修改,只需要改"circle at..."后面这个两个数值即可,上,右,下的数值分别是:50px,0、100px,50px、50px,100px。

如果想要一个阴影状,带有立体感,可加上这个代码

css 复制代码
filter: drop-shadow(3px 3px 3px rgba(0,0,0,.3))

完整的卡券样式

利用background: radial-gradient的特性,我们可以组合成更复杂、完整的优惠券样式:

css 复制代码
base-coupons {
    width: 250px;
    height: 100px;
    position: relative;
    background: radial-gradient(circle at right top, transparent 10px,  #28A4F2 0) top left / 60px 51% no-repeat,
      radial-gradient(circle at right bottom, transparent 10px,  #28A4F2 0) bottom left /60px 51% no-repeat,
      radial-gradient(circle at left top, transparent 10px, #eeeeee 0) top right /190px 51% no-repeat,
      radial-gradient(circle at left bottom, transparent 10px, #eeeeee 0) bottom right /190px 51% no-repeat;
    filter: drop-shadow(3px 3px 3px rgba(0,0,0,.3));
  }

如果想要在卡券的拼接处加上虚线效果,我们可以利用伪类去实现:

css 复制代码
.base-coupons::before {
    content: '';
    height: 80px;
    border: 1px dashed #fff;
    position: absolute;
    left: 60px;
    top: 0;
    bottom: 0;
    margin: auto;
  }

那么最终的优惠券样式效果:

linear-gradient()

CSS 语法

background: linear-gradient(direction, color-stop1, color-stop2, ...);

linear-gradient 函数是一个线性渐变函数,我们可以使用这个线性渐变加伪类来实现一个优惠券的锯齿状效果,如下:

css 复制代码
.base-coupons::after {
    content: '';
    position: absolute;
    height: 100%;
    width:5px;
    top: 0;
    right: -5px;
    background-image: linear-gradient(to bottom, #eeeeee 5px, transparent 5px, transparent),
    radial-gradient(10px circle at 5px 10px, transparent 5px, #eeeeee 5px);
    background-size: 5px 15px;
  }
相关推荐
阿阳微客27 分钟前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro1 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom1 小时前
javaweb -html -CSS
前端·javascript·html
打小就很皮...2 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡3 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜054 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx4 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9994 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
后海 0_o5 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构
Scabbards_5 小时前
CPT304-2425-S2-Software Engineering II
前端