使用纯CSS实现3D按钮效果
.button-3d {
position: relative;
display: inline-block;
padding: 15px 30px;
font-size: 16px;
font-weight: bold;
text-transform: uppercase;
color: white;
background: #007bff; /* Blue */
border: none;
border-radius: 5px;
cursor: pointer;
transition: transform 0.3s ease;
/* Add a subtle box-shadow for depth */
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
}
.button-3d::before {
content: "";
position: absolute;
top: 2px;
left: 2px;
right: 2px;
bottom: 2px;
background: #0056b3; /* Darker Blue */
border-radius: 5px;
transform: translateZ(-4px); /* Move the pseudo-element back */
z-index: -1; /* Place the pseudo-element behind the button */
transition: transform 0.3s ease;
}
.button-3d:hover {
transform: translateY(-2px); /* Move the button up slightly */
}
.button-3d:hover::before {
transform: translateZ(-2px); /* Move the pseudo-element less back on hover */
}
/* Example variations with different colors */
.button-3d.green {
background: #28a745; /* Green */
}
.button-3d.green::before {
background: #1e7e34; /* Darker Green */
}
.button-3d.red {
background: #dc3545; /* Red */
}
.button-3d.red::before {
background: #bd2130; /* Darker Red */
}
/* Active state - adds a pressed effect */
.button-3d:active {
transform: translateY(1px);
box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.4); /* Darker shadow on active */
}
.button-3d:active::before {
transform: translateZ(-1px); /* Bring pseudo-element closer on active */
}
Explanation:
-
position: relative;
on the button: This allows us to absolutely position the pseudo-element (::before
) relative to the button. -
::before
pseudo-element: This creates the "back" of the 3D effect. We give it a slightly darker background color and usetransform: translateZ(-4px);
to move it backwards in 3D space.z-index: -1;
ensures it's behind the main button. -
transform: translateY(-2px);
on hover: This moves the button slightly up on hover, creating the illusion of it being lifted. -
transform: translateZ(-2px);
on hover for::before
: This moves the back face less backwards on hover, enhancing the 3D effect. -
box-shadow
: A subtle box shadow adds to the depth effect. -
Active State: The
:active
styles simulate a button press by moving the button down slightly and darkening the box shadow. -
Color Variations: The example includes CSS classes for green and red button variations. You can easily customize these or add more.
How to use it:
<button class="button-3d">Blue Button</button>
<button class="button-3d green">Green Button</button>
<button class="button-3d red">Red Button</button>
This CSS creates a convincing 3D button effect without any JavaScript or images, relying solely on CSS transforms and pseudo-elements. Remember to adjust colors and sizes to fit your design.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通