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;
}

大功告成

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

相关推荐
Hello-Mr.Wang15 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
程序员凡尘43 分钟前
完美解决 Array 方法 (map/filter/reduce) 不按预期工作 的正确解决方法,亲测有效!!!
前端·javascript·vue.js
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
(⊙o⊙)~哦6 小时前
JavaScript substring() 方法
前端
无心使然云中漫步6 小时前
GIS OGC之WMTS地图服务,通过Capabilities XML描述文档,获取matrixIds,origin,计算resolutions
前端·javascript
Bug缔造者6 小时前
Element-ui el-table 全局表格排序
前端·javascript·vue.js
xnian_7 小时前
解决ruoyi-vue-pro-master框架引入报错,启动报错问题
前端·javascript·vue.js
麒麟而非淇淋8 小时前
AJAX 入门 day1
前端·javascript·ajax
2401_858120538 小时前
深入理解MATLAB中的事件处理机制
前端·javascript·matlab
阿树梢8 小时前
【Vue】VueRouter路由
前端·javascript·vue.js