web前端学习(二十七)——CSS3导航栏的相关设置

简介: web前端学习(二十七)——CSS3导航栏的相关设置

1.CSS导航栏


垂直

水平

导航栏

熟练使用导航栏,对于任何网站都非常重要。

使用CSS你可以转换成好看的导航栏而不是枯燥的HTML菜单。


2.导航栏==链接列表


作为标准的HTML基础一个导航栏是必须的,在我们的例子中我们将建立一个标准的HTML列表导航栏。

导航条基本上是一个链接列表,所以使用 <ul> <li>元素非常有意义:

·       list-style-type:none - 移除列表前小标志。一个导航栏并不需要列表标记

·       移除浏览器的默认设置将边距(margin)和填充(padding)设置为0

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>CSS简单学习</title>
    <style type="text/css">
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
      }
    </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>
    <p>注意:这里我们用 href="#"作为测试连接。但在一个真正的 web 站点上需要真实的 url。</p>
  </body>
</html>

3.垂直导航栏


display:block - 显示块元素的链接,让整体变为可点击链接区域(不只是文本),它允许我们指定宽度。

创建一个简单的垂直导航条实例,在鼠标移动到选项时,修改背景颜色:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CSS简单学习</title>
    <style type="text/css">
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
      }
      a:link,a:visited {
        display: block;
        padding: 4px;
        width: 120px;
        font-weight: bold;
        text-align: center;
        text-decoration: none;
        text-transform: uppercase;
        color: white;
        background-color: seagreen;
      }
      a:hover,a:active {
        background-color: limegreen;
      }
    </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>

4.激活当前导航条


在点击了选项后,我们可以添加"active" 类来标准哪个选项被选中。 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CSS简单学习</title>
    <style type="text/css">
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        width: 200px;
        background-color: lightgrey;
      }
      li a {
        display: block;
        color: #000000;
        padding: 6px 15px;
        text-decoration: none;
      }
      li a.active {
        background-color: green;
        color: white;
      }
      li a:hover:not(.active) {
        background-color: dimgray;
        color: white;
      }
    </style>
  </head>
  <body>
    <h2>垂直导航条</h2>
    <p>在点击了选项后,我们可以添加 "active" 类来标准哪个选项被选中。</p>
    <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>
    <p>背景颜色添加到链接中显示链接的区域</p>
    <p>注意,整个区域是可点击的链接,而不仅仅是文本。</p>
  </body>
</html>

5.创建链接并添加边框


可以在 <li> or <a> 上添加text-align:center样式来让链接居中。

可以在border <ul>上添加border属性来让导航栏有边框。如果要在每个选项上添加边框,可以在每个 <li> 元素上添加border-bottom

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CSS简单学习</title>
    <style type="text/css">
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        width: 200px;
        background-color: whitesmoke;
        border: 1px solid dimgrey;
      }
      li a {
        display: block;
        color: #000000;
        padding: 6px 15px;
        text-decoration: none;
      }
      li {
        text-align: center;
        border-bottom: 1px solid dimgrey;
      }
      li:last-child {
        border-bottom: none;
      }
      li a.active {
        background-color: #008000;
        color: white;
      }
      li a:hover:not(.active) {
        background-color: dimgrey;
        color: white;
      }
    </style>
  </head>
  <body>
    <h2>垂直导航条</h2>
    <p>以下实例让每个链接居中,并给每个列表选项添加边框。</p>
    <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>

6.全屏高度的固定导航条


创建一个左边是全屏高度的固定导航条,右边是可滚动的内容。 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CSS简单学习</title>
    <style type="text/css">
      body {
        margin: 0;
      }
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        width: 15%;
        height: 100%;
        overflow: auto;
        position: fixed;
        background-color: darkgray;
      }
      li a {
        display: block;
        text-decoration: none;
        padding: 8px 15px;
        color: #000000;
      }
      li a.active {
        background-color: #008000;
        color: white;
      }
      li a:hover:not(.active) {
        background-color: dimgrey;
        color: white;
      }
    </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="margin-left:15%; padding:1px 16px; height:1000px;">
      <h2>Fixed Full-height Side Nav</h2>
      <h3>Try to scroll this area, and see how the sidenav sticks to the page</h3>
      <p>Notice that this div element has a left margin of 15%. This is because the side navigation is set to 15% width. If you remove the margin, the sidenav will overlay/sit on top of this div.</p>
      <p>Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when the sidenav is too long (for example if it has over 50 links inside of it).</p>
      <p>Some text..</p>
      <p>Some text..</p>
      <p>Some text..</p>
      <p>Some text..</p>
      <p>Some text..</p>
      <p>Some text..</p>
      <p>Some text..</p>
    </div>
  </body>
</html>

7.水平导航栏


有两种方法创建横向导航栏。使用内联(inline)浮动(float)的列表项。

这两种方法都很好,但如果你想链接到具有相同的大小,你必须使用浮动的方法。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CSS简单学习</title>
    <style type="text/css">
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: dodgerblue;
      }
      li {
        float: left;
      }
      li a {
        display: block;
        text-decoration: none;
        text-align: center;
        padding: 15px 16px;
        color: white;
      }
      /*li a:hover {
        background-color: skyblue;
      }*/
      li a:hover:not(.active) {
        background-color: skyblue;
        color: white;
      }
      .active {
        background-color: #008000;
        color: white;
      }
    </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>
      <!--
        <li><a href="#home">主页</a></li>
        <li><a href="#news">新闻</a></li>
        <li><a href="#contact">联系</a></li>
        <li style="float: right;"><a class="active" href="#about">关于</a></li>
      -->
    </ul>
  </body>
</html>


4个列表项换成代码中注释部分的时候,将会出现如下结果:👇👇👇

8.在导航条之间添加分割线


<li>通过border-right样式来添加分割线。 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CSS简单学习</title>
    <style type="text/css">
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: red;
      }
      li {
        float: left;
        border-right: 1px solid papayawhip;
      }
      li:last-child {
        border-right: none;
      }
      li a {
        display: block;
        text-align: center;
        text-decoration: none;
        padding: 15px 16px;
        color: white;
      }
      li a:hover:not(.active) {
        background-color: orange;
        color: white;
      }
      .active {
        background-color: #1E90FF;
      }
    </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 style="float:right"><a href="#about">关于</a></li>
    </ul>
  </body>
</html>

9.将导航栏固定在顶部(top:0;)或底部(bottom:0;)


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CSS简单学习</title>
    <style type="text/css">
      body {
        margin: 0;
      }
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        width: 100%;
        position: fixed;
        overflow: hidden;
        background-color: black;
        top: 0;
        /*bottom: 0;*/
      }
      li {
        float: left;
      }
      li a {
        display: block;
        text-align: center;
        text-decoration: none;
        padding: 14px 16px;
        color: white;
      }
      li a:hover:not(.active) {
        background-color: red;
        color: white;
      }
      .active {
        background-color: blue;
        color: white;
      }
    </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;">
    <!-- 当使用代码第19行的注释时,可将上方div内联样式中的第二个换成margin-bottom:30px; 
       此时运行结果网页的导航栏将位于底部 -->
      <h1>Fixed Top Navigation Bar</h1>
      <h2>Scroll this page to see the effect</h2>
      <h2>The navigation bar will stay at the top of the page while scrolling</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>


将代码中的第18行用第19行的注释替换,同时根据代码第51行的注释,修改div标签中的内容,即可得到如下的运行结果:👇👇👇

相关文章
|
前端开发
如何设置 CSS 盒子模型的边框样式?
CSS盒子模型的边框样式可以通过`border`属性设置,包括边框宽度、样式和颜色。例如:`border: 2px solid red;` 设置了2像素宽的红色实线边框。也可分别设置四边,如`border-top`、`border-right`等。
|
2月前
|
前端开发 JavaScript 算法
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(八):学习transition过渡属性;本文学习property模拟、duration过渡时间指定、delay时间延迟 等多个参数
transition过渡属性 早期在Web中要实现动画效果,都是依赖于JavaScript或Flash来完成。 但在CSS3中新增加了一个新的模块transition,它可以通过一些简单的CSS事件来触发元素的外观变化, 让效果显得更加细腻。简单点说,就是通过鼠标经过、获得焦点,被点击或对元素任何改变中触发, 并平滑地以动画效果改变CSS的属性值。 在CSS中创建简单的过渡效果可以从以下几个步骤来实现: 在默认样式中声明元素的初始状态样式; 声明过渡元素最终状态样式,比如悬浮状态; 在默认样式中通过添加
220 1
|
2月前
|
前端开发 JavaScript 算法
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(七):学习ransform属性;本文学习 rotate旋转、scale缩放、skew扭曲、tanslate移动、matrix矩阵 多个参数
transform变形 css3在原来的基础上新增了变形和动画相关属性,通过这些属性可以实现以前需要大段JavaScript才能实现的 功能。 CSS3的变形功能可以对HTML组件执行位移、旋转、缩放、倾斜4种几何变换,这样的变换可以控制HTML组件 呈现出丰富的外观。 借助于位移、旋转、缩放、倾斜这4种几何变换,CSS3提供了transition动画。 transition动画比较简单,只要指定HTML组件的哪些CSS属性需要使用动画效果来执行变化,并指定动画时间,就可保证动画播放。 比transitio
158 1
|
JavaScript 前端开发 程序员
前端学习笔记——node.js
前端学习笔记——node.js
349 0
|
10月前
|
前端开发 Java Shell
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
674 20
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
11月前
|
前端开发 Java 开发工具
【03】完整flutter的APP打包流程-以apk设置图标-包名-签名-APP名-打包流程为例—-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈 章节内容【03】
【03】完整flutter的APP打包流程-以apk设置图标-包名-签名-APP名-打包流程为例—-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈 章节内容【03】
946 18
【03】完整flutter的APP打包流程-以apk设置图标-包名-签名-APP名-打包流程为例—-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈 章节内容【03】
|
10月前
|
前端开发
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
295 1
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
|
11月前
|
Dart 前端开发 架构师
【01】vs-code如何配置flutter环境-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈-供大大的学习提升
【01】vs-code如何配置flutter环境-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈-供大大的学习提升
554 26
|
前端开发 JavaScript 搜索推荐
HTML与CSS在Web组件化中的核心作用及前端技术趋势
本文探讨了HTML与CSS在Web组件化中的核心作用及前端技术趋势。从结构定义、语义化到样式封装与布局控制,两者不仅提升了代码复用率和可维护性,还通过响应式设计、动态样式等技术增强了用户体验。面对兼容性、代码复杂度等挑战,文章提出了相应的解决策略,强调了持续创新的重要性,旨在构建高效、灵活的Web应用。
280 6
|
前端开发 JavaScript UED
在数字化时代,Web 应用性能优化尤为重要。本文探讨了CSS与HTML在提升Web性能中的关键作用及未来趋势
在数字化时代,Web 应用性能优化尤为重要。本文探讨了CSS与HTML在提升Web性能中的关键作用及未来趋势,包括样式表优化、DOM操作减少、图像优化等技术,并分析了电商网站的具体案例,强调了技术演进对Web性能的深远影响。
191 5