美文网首页
CSS基础-14-水平导航栏(示例及详细创建过程)

CSS基础-14-水平导航栏(示例及详细创建过程)

作者: 玄德公笔记 | 来源:发表于2022-06-25 23:43 被阅读0次

    1. 内联列表实现

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>hello world</title> 
    <style>
    ul
    {
        /* 不显示列表前的标记 */
        list-style-type:none;
        margin:0;
        padding:0;
    }
    li
    {
      /* 将块元素转变为内联元素 */
      display:inline;
    }
    </style>
    </head>
    
    <body>
    <ul>
    <li><a href="#home">主页</a></li>
    <li><a href="#news">新闻</a></li>
    <li><a href="#contact">联系</a></li>
    <li><a href="#about">关于</a></li>
    </ul>
    
    </body>
    </html>
    
    • 效果


      image.png

    2. 浮动列表实现

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>hello world </title> 
    <style>
    ul
    {
        list-style-type:none;
        margin:0;
        padding:0;
        overflow:hidden;
    }
    li
    {
        float:left;
    }
    a
    {
        display:block;
        padding: 14px 16px;
        text-decoration: none;
    
    }
    </style>
    </head>
    
    <body>
    <ul>
    <li><a href="#home">主页</a></li>
    <li><a href="#news">新闻</a></li>
    <li><a href="#contact">联系</a></li>
    <li><a href="#about">关于</a></li>
    </ul>
    
    </body>
    </html>
    
    • 效果


      image.png

    3. 添加边框

    3.1 思路

    • 添加整体边框
    ul {
        ……
        background-color: #333;
    }
    
    • 给主页添加一个不同的效果
    .active {
        background-color: #4CAF50;
    }
    
    • 添加鼠标悬停效果,排除首页
    li a:hover:not(.active) {
        background-color: #aaaaaa;
    }
    

    3.2 实际效果

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>hello world</title>
    <style>
    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #333;
    }
    
    li {
        float: left;
    }
    
    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
    }
    
    li a:hover:not(.active) {
        background-color: #aaaaaa;
    }
    
    .active {
        background-color: #4CAF50;
    }
    </style>
    </head>
    <body>
    
    <ul>
      <li><a class="active" href="#home">主页</a></li>
      <li><a href="#news">新闻</a></li>
      <li><a href="#contact">联系</a></li>
      <li><a href="#about">关于</a></li>
    </ul>
    
    </body>
    </html>
    
    • 效果


      image.png

    4. 添加分割线

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>hello world</title>
    <style>
    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #333;
    }
    
    li {
        float: left;
        /* 右侧添加一个边框 */
        border-right:1px solid #bbb;
    }
    /* 最后一个li不添加右边框 */
    li:last-child {
        border-right: none;
    }
    
    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
    }
    
    li a:hover:not(.active) {
        background-color: #dddddd;
    }
    
    .active {
        background-color: #4CAF50;
    }
    </style>
    </head>
    <body>
    
    <ul>
      <li><a class="active" href="#home">主页</a></li>
      <li><a href="#news">新闻</a></li>
      <li><a href="#contact">联系</a></li>
      <li><a href="#about">关于</a></li>
    </ul>
    
    </body>
    </html>
    
    • 效果


      image.png

    5. 固定导航栏

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>hello world</title>
    <style>
    body {margin:0;}
    
    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #333;
        /* 固定导航栏 */
        position: fixed;
        top: 0;
        width: 100%;
    }
    
    li {
        float: left;
    }
    
    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
    }
    
    li a:hover:not(.active) {
        background-color: #dddddd;
    }
    
    .active {
        background-color: #4CAF50;
    }
    </style>
    </head>
    <body>
    
    <ul>
      <li><a class="active" href="#home">主页</a></li>
      <li><a href="#news">新闻</a></li>
      <li><a href="#contact">联系</a></li>
      <li><a href="#about">关于</a></li>
    </ul>
    
    <div style="padding:20px;margin-top:30px;background-color:#1abc9c;height:1500px;">
    
    <h2>标题</h2>
    
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    <p>Some text some text some text some text..</p>
    </div>
    
    </body>
    </html>
    
    • 效果


      image.png

    image.png

    相关文章

      网友评论

          本文标题:CSS基础-14-水平导航栏(示例及详细创建过程)

          本文链接:https://www.haomeiwen.com/subject/dwwgmrtx.html