玄之 发表于 2025-3-9 22:31:47

HTML&CSS :移动端导航栏

这段代码通过纯CSS实现了一个水平排列的按钮组,每个按钮包含一个SVG图标。按钮在鼠标悬停时会轻微上移,增强用户体验。整体设计简洁美观,适合用于导航栏、工具栏或其他需要图标按钮的场景。


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 {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #e8e8e8;
            overflow: hidden;
      }

      .container {
            display: flex;
            background-color: rgba(245, 73, 144);
            width: 250px;
            height: 40px;
            align-items: center;
            justify-content: space-around;
            border-radius: 10px;

      }

      .button {
            outline: 0 !important;
            border: 0 !important;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: transparent;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            transition: all ease-in-out 0.3s;
            cursor: pointer;
      }

      .button:hover {
            transform: translateY(-3px);
      }

      .icon {
            font-size: 20px;
      }
    </style>
</head>

<body>
    <div class="container">
      <button class="button">
            <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 1024 1024" stroke-width="0"
                fill="currentColor" stroke="currentColor" class="icon">
                <path
                  d="M946.5 505L560.1 118.8l-25.9-25.9a31.5 31.5 0 0 0-44.4 0L77.5 505a63.9 63.9 0 0 0-18.8 46c.4 35.2 29.7 63.3 64.9 63.3h42.5V940h691.8V614.3h43.4c17.1 0 33.2-6.7 45.3-18.8a63.6 63.6 0 0 0 18.7-45.3c0-17-6.7-33.1-18.8-45.2zM568 868H456V664h112v204zm217.9-325.7V868H632V640c0-22.1-17.9-40-40-40H432c-22.1 0-40 17.9-40 40v228H238.1V542.3h-96l370-369.7 23.1 23.1L882 542.3h-96.1z">
                </path>
            </svg>
      </button>
      <button class="button">
            <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" aria-hidden="true" viewBox="0 0 24 24"
                stroke-width="2" fill="none" stroke="currentColor" class="icon">
                <path d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" stroke-linejoin="round" stroke-linecap="round">
                </path>
            </svg>
      </button>
      <button class="button">
            <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" stroke-width="0"
                fill="currentColor" stroke="currentColor" class="icon">
                <path
                  d="M12 2.5a5.5 5.5 0 0 1 3.096 10.047 9.005 9.005 0 0 1 5.9 8.181.75.75 0 1 1-1.499.044 7.5 7.5 0 0 0-14.993 0 .75.75 0 0 1-1.5-.045 9.005 9.005 0 0 1 5.9-8.18A5.5 5.5 0 0 1 12 2.5ZM8 8a4 4 0 1 0 8 0 4 4 0 0 0-8 0Z">
                </path>
            </svg>
      </button>

      <button class="button">
            <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" stroke-linejoin="round"
                stroke-linecap="round" viewBox="0 0 24 24" stroke-width="2" fill="none" stroke="currentColor"
                class="icon">
                <circle r="1" cy="21" cx="9"></circle>
                <circle r="1" cy="21" cx="20"></circle>
                <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
            </svg>
      </button>
    </div>
</body>
</html>

HTML 结构


[*]container:按钮组的容器,用于水平排列按钮。
[*]button:定义一个按钮,包含SVG图标。
[*]svg:定义按钮中的SVG图标,使用path定义图标形状。


CSS 样式


[*]body:设置页面背景颜色、布局方式(居中对齐)和全屏高度。
[*].container:定义按钮组的样式,包括背景颜色、宽高、圆角和水平排列。
[*].button:定义按钮的样式,包括大小、圆角、背景颜色、鼠标悬停效果和过渡动画。
[*].button:hover:鼠标悬停时,按钮向上移动3像素,增强交互性。
[*].icon:定义SVG图标的样式,包括字体大小和颜色。
页: [1]
查看完整版本: HTML&CSS :移动端导航栏