Styling Elements of Nested HTML Lists - Building a hierarchical navigational system
(Page 4 of 4 )
Undoubtedly, this introductory guide on nesting HTML lists wouldn’t be complete if didn't show you how to use them for building a hierarchical navigation bar. Therefore, the last example that I’m going to code will be aimed at creating this links bar, by using some nested lists.
The sample code that renders this navigational bar on the browser is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example on building a navigation bar with nested HTML lists</title>
<style type="text/css">
body{
padding: 0;
margin: 0;
background: #fff;
}
h1{
font: bold 14pt Arial, Helvetica, sans-serif;
color: #00f;
}
/* style lists */
ul{
list-style: outside;
margin: 0 15px 10px 0;
}
/* item lists */
li{
font: bold 10pt Arial, Helvetica, sans-serif;
color: #000;
}
/* sub items */
li li{
margin: 0 0 5px 0;
}
a:link,a:visited{
font: normal 10pt Arial, Helvetica, sans-serif;
color: #00f;
text-decoration: none;
}
a:hover{
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Example on building a navigation bar with nested HTML lists</h1>
<ul>
<li>List Item 1
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li>List Item 2
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li>List Item 3
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li>List Item 4
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li>List Item 5
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li>List Item 6
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
</ul>
</body>
</html>
As shown above, a simple navigation mechanism has been built by using some nested HTML lists. Despite its simplicity, this example is actually pretty illustrative. It recreates a real world scenario that you can see on the web every day, where many sites utilize this structure as the building block for seating their links sections.
In addition to the previous code sample, below I included an extra image that depicts the visual aspect of the links bar just created:

And lastly, with this hands-on example I’m concluding this article about building HTML lists. As always, feel free to copy and edit all of the code samples included in this tutorial, so you can acquire a more solid grounding in working with these web page elements.
Final thoughts
In this second chapter of the series, I showed you a couple of interesting things regarding the use of nested HTML lists in concrete situations, such as enhancing their visual appearance and creating a hierarchical navigation system.
Naturally, these aren’t the only situations in which nested list can be useful. They can be employed, for instance, for building standard drop-down menus. This will be the topic that I plan to cover in the last part of this series; you won't want to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |