有趣的css – 简约文字风格导航栏

有趣的css - 简约文字风格导航栏
整体效果演示

利用单选按钮来模拟导航栏点击选中效果,实现一个简约的文字类导航栏。

核心代码部分,简要说明了写法思路;完整代码在最后,可直接复制到本地运行。

导航栏主体结构html代码:

<nav class="nav92">
  <label class="label92">
    <input id="sel91-1" class="inp92" type="radio" name="radio92" checked="" />
    <span class="nav-a92">首页</span>
</label>
<label class="label92">
    <input id="sel91-2" class="inp92" type="radio" name="radio92" />
    <span class="nav-a92">设计</span>
</label>
<label class="label92">
    <input id="sel91-3" class="inp92" type="radio" name="radio92" />
    <span class="nav-a92">前端</span>
</label>
</nav>

css 部分代码:

.nav92{
height: 32px;
padding: 4px;
position: relative;
overflow: hidden;
display: flex;
background-color: #eee;
box-shadow: 010px10px -5pxrgba(0, 0, 0, 0.2);
border-radius: 20px;
}
.label92{
cursor: pointer;
}
.label92.inp92{
display: none;
}
.nav-a92{
width: 88px;
height: 32px;
line-height: 32px;
border-radius: 16px;
display: block;
color: #3d3d3d;
font-size: 14px;
text-decoration: none;
transition: all 0.4s linear;
}
.nav-a92:hover{
font-weight: bold;
}
.inp92:checked + .nav-a92{
font-weight: bold;
background-color: #ffffff;
}

注意 :checked + [classname] 选择器这样写时,是表示选中按钮的下一个同级元素,要注意 html 的元素结构,否则就不起作用。

完整代码如下,html 页面:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>92 简约文字风格导航栏</title>
</head>
<body>
    <div class="app">
      <nav class="nav92">
        <label class="label92">
          <input id="sel91-1" class="inp92" type="radio" name="radio92" checked="" />
          <span class="nav-a92">首页</span>
        </label>
        <label class="label92">
          <input id="sel91-2" class="inp92" type="radio" name="radio92" />
          <span class="nav-a92">设计</span>
        </label>
        <label class="label92">
          <input id="sel91-3" class="inp92" type="radio" name="radio92" />
          <span class="nav-a92">前端</span>
        </label>
      </nav>
    </div>
</body>
</html>

完整代码如下,css 样式:

/** style.css **/
.app{
width: 100%;
height: 100vh;
background-color: #fff;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.nav92{
height: 32px;
padding: 4px;
position: relative;
overflow: hidden;
display: flex;
background-color: #eee;
box-shadow: 010px10px -5pxrgba(0, 0, 0, 0.2);
border-radius: 20px;
}
.label92{
cursor: pointer;
}
.label92.inp92{
display: none;
}
.nav-a92{
width: 88px;
height: 32px;
line-height: 32px;
border-radius: 16px;
background-color: transparent;
text-align: center;
display: block;
color: #3d3d3d;
font-size: 14px;
text-decoration: none;
transition: all 0.4s linear;
}
.nav-a92:hover{
font-weight: bold;
}
.inp92:checked + .nav-a92{
font-weight: bold;
background-color: #ffffff;
}
有趣的css - 简约文字风格导航栏
页面渲染效果

转载作品,原作者:设计师工作日常,文章来源:https://mp.weixin.qq.com/s/jDhwWVIs9qDgDCOyKm8MGg

(0)
分享几个CSS4操作小技巧,助你代码开挂
上一篇 2025-01-17 13:14
HTML实现table表格移动端自动转为卡片样式
下一篇 2025-01-23 09:34

相关推荐

发表回复

登录后才能评论
扫码了解
扫码了解
反馈建议
分享本页
返回顶部