CSS技巧专栏:一日一例 20-纯CSS实现点击会凹陷的按钮

本例图片

案例分析

其实这个按钮非常的简单啊,主要就是利用了box-shadow的inset。

布局代码

html 复制代码
<button class="base">凹下的按钮</button>

基础样式

css 复制代码
:root{
  --main-bg-color: #dcdcdc; /* 将页面背景色调整为浅灰色 */
  --color:#000;
  --hover-color:#993399;
}
button{
  margin: 0.3em;
  outline: 0;
  border: none;
}
.base{
  position: relative;   
  padding: 1rem 3rem; /* 用 padding 撑起按钮的宽度和高度 ,并确保了按钮文字水平方向居中 */
  font-family: "微软雅黑", sans-serif;
  font-size: 1.5rem;  
  line-height: 1.5rem; /* 行高和字号大小相等,可以实现按钮文字在按钮内垂直居中 */ 
  font-weight:700;
  color: var(--color);  /* 文字颜色为预定义的前景色 */
  cursor: pointer;   /* 鼠标移动到按钮上时候的形状:手型 */
  user-select: none;  /* 让用户不能选择按钮上的文字 */
  white-space: nowrap; /* 避免英文单词间的空格导致文字换行 */
  border-radius: 2rem; 
  text-decoration: none; 
  text-transform:uppercase; /* 字母自动修正为大写 */
  transition: all .5s; /* 按钮响应动画效果的持续时间 */
  margin: 1.5rem 2rem;
}

按钮样式,Let's do it!

css 复制代码
/* 凹下去的按钮 */

.push_down{
  color: #333;
  cursor: pointer;  
  border:1px solid var(--main-bg-color);
  height: 60px;
  background: var(--main-bg-color);
  transition: 0.3s;  
  text-shadow: 1px 2px 1px rgba(255, 255, 255, 0.9); 
}

悬浮样式

css 复制代码
.push_down:hover{
  background-color: var(--hover-bg);
  /* box-shadow: 0 0px 30px 0 rgba(225, 225, 225, 1),inset 0 1px 0 0 rgba(255, 255, 255, 0.7);  */
}

点击样式

css 复制代码
.push_down:active{
  color: #666;
  box-shadow:0px 1px 30px rgba(0, 0, 0, 0.03)inset, 2px 2px 2px rgba(0, 0, 0, 0.029)inset,0px 1px 1px rgba(0, 0, 0, 0.06) inset,0px -1px 0px rgba(255, 255, 255, 0.06) inset,5px 5px 6px rgba(0, 0, 0, 0.027) inset,10px 9px 12px rgba(0, 0, 0, 0.07) inset;
  text-shadow: 1px 2px 1px rgba(255, 255, 255,1); 
  transition: 0.3s;
}

大功告成

希望对可爱漂亮又富有爱心的你有所帮助!嘿嘿,别忘了,给我点赞+关注+评论哦!我在下面等你。。咳咳。。。下面评论区等你!

相关推荐
程序视点9 分钟前
Umi-OCR完全指南:开源离线OCR识别软件下载安装使用教程|支持批量PDF/二维码识别
前端·后端·github
瘦瘦的小芝芝10 分钟前
es改动总结
前端
想要学好前端的阿毛12 分钟前
React状态管理库 -- Redux篇
前端
拾光拾趣录13 分钟前
前端宏(微)任务 | 从“为什么我的代码不按顺序执行”说起
前端·javascript
林太白17 分钟前
NestJS-菜单模块
前端·后端·nestjs
程序员ys18 分钟前
微前端(What)
前端·架构
用户7974761127321 分钟前
Mysql RR事务隔离级别引发的生产Bug,你中招了吗?
前端
Mintopia22 分钟前
🧠 三分视界:Three.js 离屏渲染与多重视角的艺术
前端·javascript·计算机图形学
JarvanMo31 分钟前
Dart & Flutter DevTools 扩展
前端
yuko093134 分钟前
【手机验证码】手机号格式化光标异常问题
前端