纵向导航css设置主要属性,CSS 导航

CSS 导航

GUI下有一个导航栏或导航系统,可帮助访问者访问信息。它是网页上的UI元素,其中包含网站其他部分的链接。

导航栏通常以水平链接列表的形式显示在页面顶部。可以将其放置在徽标或标题的下方,但应始终将其放置在网页的主要内容之前。

使用易于使用的导航对于网站很重要。它在网站中起着重要作用,因为它允许访问者快速访问任何部分。

让我们详细讨论水平导航栏和垂直导航栏。

水平导航栏

水平导航栏是水平链接列表,通常位于页面顶部。

让我们看看如何使用创建水平导航栏。

示例

在此示例中,我们添加了

overflow:hidden属性,以防止

li元素进入列表之外,而

display:block属性将链接显示为block元素,并使整个链接区域可单击。

我们还添加了

float:left属性,该属性使用float来使块元素彼此相邻滑动。

如果要使用全角背景色,则必须将

background-color属性添加

  • 而不是

元素。

ul {

list-style-type: none;

margin: 0;

padding: 0px;

overflow: hidden;

background-color: lightgray;

}

li {

float: left;

}

li a {

display: block;

color: blue;

font-size:20px;

text-align: center;

padding: 10px 20px;

text-decoration: none;

}

.active{

background-color: gray;

color: white;

}

li a:hover {

background-color: orange;

color: white;

}

输出:

5f40cff143b63a6b4e548160b4ed9d75.png

边界分隔线

我们可以使用

border-right属性在导航栏中的链接之间添加边框。以下示例对其进行了更清晰的说明。

示例

ul {

list-style-type: none;

margin: 0;

padding: 0px;

overflow: hidden;

background-color: lightgray;

}

li {

float: left;

border-right: 1px solid blue;

}

li a {

display: block;

color: blue;

font-size:20px;

text-align: center;

padding: 10px 20px;

text-decoration: none;

}

.active{

background-color: gray;

color: white;

}

li a:hover {

background-color: orange;

color: white;

}

输出:

0e8ee5e190e55d1ff00ebe9f96dbd057.png

固定的导航栏

滚动页面时,固定的导航栏位于页面的底部或顶部。参见相同的示例。

示例

ul {

list-style-type: none;

position: fixed;

width:100%;

top:0;

margin: 0;

padding: 0px;

overflow: hidden;

background-color: lightgray;

}

li {

float: left;

border-right: 1px solid blue;

}

li a {

display: block;

color: blue;

font-size:20px;

text-align: center;

padding: 10px 20px;

text-decoration: none;

}

.active{

background-color: gray;

color: white;

}

li a:hover {

background-color: orange;

color: white;

}

Hello World

Scroll down the page to see the fixed navigation bar

输出:

d8757246e217d08842c86c42d7e510a8.png

粘性导航

position:sticky;属性用于根据用户的滚动位置来定位元素。

此CSS属性允许元素在滚动到达特定点时停留。根据滚动位置,粘性元素可在

固定和

相对属性

之间切换。

例子

ul {

list-style-type: none;

position: sticky;

width:100%;

top:0;

margin: 0;

padding: 0px;

overflow: hidden;

background-color: lightgray;

}

li {

float: left;

border-right: 1px solid blue;

}

li a {

display: block;

color: blue;

font-size:20px;

text-align: center;

padding: 10px 20px;

text-decoration: none;

}

.active{

background-color: gray;

color: white;

}

li a:hover {

background-color: orange;

color: white;

}

Example of sticky navigation bar

Hello World

Scroll down the page to see the sticky navigation bar

输出:

02e0ee8c28ff5574cb97fa50aa3a3a35.png

下拉导航栏

下面的示例说明如何在导航栏中创建下拉菜单。

示例

ul {

list-style-type: none;

margin: 0;

padding: 0;

overflow: hidden;

background-color: lightgray;

}

li {

float: left;

}

li a, .dropbtn {

display: inline-block;

color: blue;

font-size:20px;

text-align: center;

padding: 10px 20px;

text-decoration: none;

}

.active{

background-color: gray;

color: white;

}

li a:hover , .dropdown:hover .dropbtn{

background-color: orange;

color: white;

}

.dropdown-content {

display: none;

position: absolute;

background-color: lightblue;

min-width: 160px;

box-shadow: 5px 8px 10px 0px black;

}

.dropdown-content a {

color: black;

padding: 12px 16px;

text-decoration: none;

display: block;

text-align: left;

}

.dropdown-content a:hover {

background-color: gray;

color:white;

}

.dropdown:hover .dropdown-content {

display: block;

}

h1,h2,h3{

text-align:center;

color: green;

}

Welcome to the lidihuo.com

Example of Dropdown Menu inside a Navigation Bar

Move your cursor on the "web-designing" to see the dropdown effect.

输出:

ea2e73dcbedbdc4ab2089153654ad186.png

垂直导航栏

在此示例中,我们将了解如何构建垂直导航栏。

示例

ul {

list-style-type: none;

margin: 0;

padding: 0;

width: 200px;

background-color: lightblue;

}

li a {

display: block;

color: blue;

font-size:20px;

padding: 8px 16px;

text-decoration: none;

}

.active{

background-color: orange;

color: white;

}

li a:hover {

background-color: orange;

color: white;

}

Vertical Navigation Bar

输出:

5f5fdd998d8444b480d27b4127353970.png

我们可以将链接对准中心并在它们之间添加边框。

示例

ul {

list-style-type: none;

margin: 0;

padding: 0;

width: 200px;

background-color: lightblue;

border: 1px solid blue;

}

li a {

display: block;

color: blue;

font-size:20px;

padding: 10px 20px;

text-decoration: none;

border-bottom: 1px solid blue;

}

ul:last-child {

border-bottom: none;

}

.active{

background-color: orange;

color: white;

}

li a:hover {

background-color: orange;

color: white;

}

Vertical Navigation Bar

输出:

55e1195ac9fb41901ab5725a8ba8fcb0.png

全高固定垂直导航栏

我们还可以使用属性

height:100%;创建固定全高侧面导航栏和

位置:固定;

示例

body{

background-color: pink;

}

ul {

list-style-type: none;

margin: 0;

padding: 0;

height:100%;

top:0;

width:150px;

overflow: auto;

background-color: lightblue;

border: 1px solid blue;

position: fixed;

}

li a {

display: block;

color: blue;

font-size:20px;

padding: 10px 20px;

text-decoration: none;

border-bottom: 1px solid blue;

}

.active{

background-color: orange;

color: white;

}

li a:hover {

background-color: orange;

color: white;

}

Welcome to the lidihuo.com

Side navigation bar with height: 100%; and position: fixed;

Scroll the page, and see how the sidenav sticks to the page

输出:

c5dd7b900ab31ffb9e699dc7882f1871.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值