3、css3 手写nav导航条(互相学习)

效果例图:

1、首先呈现的是html代码:

html 复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./3.css">
    <title>Document</title>
</head>
<body>
    <nav class="navbar">
        <ul class="nav-list">
            <li class="nav-item">
                <a href="#" class="nav-link">home</a></li>
            <li class="nav-item">
                <a href="#" class="nav-link">about</a>
                <ul class="dropdown">
                    <li class="dropdown-item"><a href="#" class="dropdown-link">Our Team</a></li>
                    <li class="dropdown-item"><a href="#" class="dropdown-link">Our Story</a></li>
                    <li class="dropdown-item"><a href="#" class="dropdown-link">Careers</a></li>
                </ul>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Services</a>
                <ul class="dropdown">
                    <li class="dropdown-item"><a href="#" class="dropdown-link">web</a></li>
                    <li class="dropdown-item"><a href="#" class="dropdown-link">html</a></li>
                    <li class="dropdown-item"><a href="#" class="dropdown-link">dockewr</a></li>
                    <li class="dropdown-item"><a href="#" class="dropdown-link">nginx</a></li>
                    <li class="dropdown-item"><a href="#" class="dropdown-link">hahah</a></li>
                </ul>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Contact</a>
            </li>
        </ul>
    </nav>
</body>
</html>

2、其次最关键的css3样式代码:

html 复制代码
body{
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
    background-color: #f0f0f0;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}
.navbar{
    background-color: #333;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
}
.nav-list{
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
}
.nav-item{
    position: relative;
}
.nav-link{
    display: block;
    padding: 15px 20px;
    color: white;
    text-decoration: none;
    font-size: 18px;
    transition: background-color 0.3s,transform 0.3s;
    position: relative;
    z-index: 1;
}
.nav-link:hover{
    background-color: #008c8c;
    transform: scale(1.1);
}
.nav-link::after{
    content: "";
    position: absolute;
    left: 50%;
    width: 0;
    height: 3px;
    background-color: #f8f400;
    transition: all 0.4s;
    bottom: 0;
    z-index: 1;
}
.nav-link:hover::after{
    left: 0;
    width: 100%;
}

.dropdown{
    display: none;
    /* height: 0px; */
    position: absolute;
    top: calc(100% + 2px);
    left: -5px;
    background-color: #444;
    list-style: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border-radius: 3px;
    min-width: 150px;
    z-index: 1000;
    opacity: 0;
    transform: translateY(-10px);
    transition: background-color 0.6s ease-in-out;
}
.dropdown-item{
    position: relative;
}
.dropdown-link{
    display: block;
    padding: 15px 20px;
    color: white;
    text-decoration: none;
    font-size: 18px;
    transition: background-color 0.3s;
}
.dropdown-link::after{
    content: "";
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 0px;
    height: 2px;
    transition: all 0.4s ease-in-out;
}
.dropdown-link:hover{
    background-color: #008c8c;
}
.dropdown-link:hover::after{
    left: 0;
    width: 100%;
    background-color: #f8f400;
}

.nav-item:hover .dropdown{
    display: block;
    /* height: 300px; */
    opacity: 1;
    transform: translateY(0);
}

总结:CSS太强大了,只是我们现在的学习知识不够,不能够很好的驾驭它,

活到老学到老

相关推荐
计算机魔术师2 分钟前
Dario Amodei 发文呼吁为前沿 AI 降速并提出三点计划
前端
计算机魔术师17 分钟前
OpenAI的AI代理偷偷给RubyGems下毒,我们却毫无察觉
前端
甲维斯26 分钟前
0代码,0建模,3句话开发一个3D游戏!
前端·游戏·游戏开发
IT_陈寒38 分钟前
Vue的响应式更新把我坑惨了,原来问题出在这
前端·人工智能·后端
tingke43 分钟前
别再堆 AGENTS.md 了:前端团队的 Agent 上下文分层落地指南
前端
李明卫杭州1 小时前
React 复合事件系统对比解析
前端·javascript·react.js
葡萄城技术团队2 小时前
SpreadJS V19.2 新特性揭秘:FILTERXML 函数
前端
袋鼠云数栈UED团队2 小时前
AI Coding 方法论分析:同一个需求 SpecKit 、 Superpowers、 MattpocockSkills 不同设计
前端·aigc·ai编程
掰头战士2 小时前
Prompt Cache,如果agent全靠LLM方做隐式缓存实在不够用。
前端·llm·agent
LEE3 小时前
前端转型全栈 01:数据建模,前端最大的盲区
前端·javascript·后端