ORIGIN

导航条练习 Navigation Bar

Web Study 4 mins756 words

This is an easy Navigation bar with no so many functions. When you hover on one item, the background change and all the items are able to click. And here is a preview of the navigation bar.

HTML Frame

First, we write a frame of html.

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en">

<head>
<title>Navigation bar</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
</body>

</html>

Content

Usually, the items of navigation bar are unordered list, you can use div but its not suggested.

So we create the list.

1
2
3
4
5
6
7
8
<body>
<ul id="nav">
<li><a href="#">首页</a></li>
<li><a href="#">新闻</a></li>
<li><a href="#">联系</a></li>
<li><a href="#">关于</a></li>
</ul>
</body>

CSS style

The content is simple, and it’s displayed in a row in browser. If we want to get the format, we need to give styles to the content.

*

1
2
3
4
* {
margin: 0;
padding: 0;
}

We use this to erase the original style given by the browser.

1
2
3
4
5
6
7
8
9
10
11
12
#nav {
/* clear the circle shape before list */
list-style: none;
/* set a background color for the ul */
background-color: skyblue;
/* In IE6, if you set a width to it, then it's height won't crash */
width: 1000px;
/* use auto to make it display in the center of browser */
margin: 50px auto;
/* solve for the height crash problem */
overflow: hidden;
}
1
2
3
4
5
6
#nav li {
/* let the item display in one line */
float: left;
/* set the width for each item, since we have four items, width is 1/4 */
width: 25%;
}

If we want the hole area clickable, we need to set width to <a>. However, since we use float, <a> is disengaged in document flow so its parent has no width. So we need to set the the width to li and make width of <a> 100%.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#nav a {
/* make it not inline item */
display: block;
/* its parent is li which we already set the width*/
width: 100%;
/* display in the center */
text-align: center;
/* set height of the bar */
padding: 5px 0px;
/* get rid of the underline */
text-decoration: none;
color: white;
font-weight: bold;
}

When the mouse is over, the block changed into a different color. We use Pseudo class.

1
2
3
#nav a:hover {
background-color: brown;
}

Summary

Merge the code into a whole thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="en">

<head>
<title>Navigation bar</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/style.css" rel="stylesheet">
<style type="text/css">
* {
margin: 0;
padding: 0;
}

#nav {
list-style: none;
background-color: skyblue;
/* In IE6, if you set a width to it, then it's height won't crash */
width: 1000px;
margin: 50px auto;
/* solve for the height crash problem */
overflow: hidden;
}

#nav li {
float: left;
width: 25%;
}

#nav a {
display: block;
width: 100%;
text-align: center;
padding: 5px 0px;
text-decoration: none;
color: white;
font-weight: bold;
}

#nav a:hover {
background-color: brown;
}
</style>
</head>

<body>
<ul id="nav">
<li><a href="#">首页</a></li>
<li><a href="#">新闻</a></li>
<li><a href="#">联系</a></li>
<li><a href="#">关于</a></li>
</ul>
</body>

</html>

Now, we finished the simple navigation bar.

TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|