HTML&CSS:超炫!逼真的3D 键盘按钮

905 阅读2分钟

这段代码用于创建一个模拟键盘按键的视觉效果,每个按键都有 3D 效果,都有一个渐变背景和阴影效果,使其看起来像是凸起的物理按钮。这种效果适用于游戏界面或其他需要键盘操作的场景,为用户提供视觉上的反馈和交互体验。点击获取更多

演示效果

HTML&CSS

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>公众号关注:前端Hardy</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: #212121;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }

        .button {
            outline: none;
            border: none;
            display: flex;
            width: 400px;
            justify-content: center;
            flex-wrap: wrap;
            background-color: transparent;
        }

        .button div:first-child {
            width: 100%;
            display: flex;
            justify-content: center;
        }

        .button span {
            cursor: pointer;
            position: relative;
            height: 60px;
            width: 60px;
            padding: 8px 15px;
            margin: 8px 4px;
            display: inline-block;
            border-radius: 10px;
            background: linear-gradient(180deg, #282828, #202020);
            box-shadow: inset -8px 0 8px rgba(0, 0, 0, 0.15),
                inset 0 -8px 8px rgba(0, 0, 0, 0.25), 0 0 0 2px rgba(0, 0, 0, 0.75),
                10px 20px 25px rgba(0, 0, 0, 0.4);
            overflow: hidden;
        }

        .button span::before {
            content: "";
            position: absolute;
            top: 3px;
            left: 4px;
            bottom: 14px;
            right: 12px;
            background: linear-gradient(90deg, #232323, #4a4a4a);
            border-radius: 10px;
            box-shadow: -10px -10px 10px rgba(255, 255, 255, 0.25),
                10px 5px 10px rgba(0, 0, 0, 0.15);
            border-left: 1px solid #0004;
            border-bottom: 1px solid #0004;
            border-top: 1px solid #0009;
        }

        .button span i {
            font-family: "Montserrat", sans-serif;
            position: relative;
            font-style: normal;
            font-size: 1.5rem;
            text-transform: uppercase;
            color: white;
        }
    </style>
</head>

<body>
    <button class="button">
        <div>
            <span>
                <i>W</i>
            </span>
        </div>
        <div>
            <span>
                <i>A</i>
            </span>
        </div>
        <div>
            <span>
                <i>S</i>
            </span>
        </div>
        <div>
            <span>
                <i>D</i>
            </span>
        </div>
    </button>

</body>

</html>

HTML 结构

  • button:定义了一个按钮元素,用于包含四个子按钮,每个子按钮代表一个键盘按键,并应用了类名 button 来应用 CSS 样式。
  • div:包含四个子 div 元素,每个 div 包含一个 span 元素,代表一个键盘按键。
  • span:包含一个 i 元素,显示键盘按键的字符。

CSS 样式

  • body:设置页面的外边距、内边距、背景色、显示方式、对齐方式和高度。
  • .button:定义了主按钮的基本样式,包括去除轮廓线、边框、宽度、显示方式和包装方式。
  • .button div:first-child:选择第一个 div 元素,并设置其宽度和显示方式。
  • .button span:定义了子按钮的样式,包括光标样式、位置、尺寸、内边距、外边距、显示方式、边框半径、背景渐变、阴影和溢出。
  • .button span::before:使用伪元素为子按钮添加额外的视觉效果,包括背景渐变、边框半径、阴影和边框。
  • .button span i:定义了子按钮内 i 元素的样式,包括字体、位置、字体样式、字体大小、文本转换和颜色。