Download Horizontal_Nav_Menu.zip
A basic navigational menu can be created from a list (ordered or unordered) by making each list item a link and then "stylize" the whole list to make it behave like a menu.
There are two ways to create a horizontal navigation bar:
Notice the systematic steps in the accordion below:
<body> <p>Home</p>
<p>Products</p>
<p>Services</p>
<p>Contact Us</p>
</body>
Home
Products
Services
Contact Us
<ul>
<li>Home</li>
<li>Products</li>
<li>Services</li>
<li>Contact Us</li>
</ul>
NOTE: Notice how the "p" elements were converted to "li" elements and wrapped within a "ul" element.
Next, let's convert the list to a list of links by applying an anchor tag <a> to the text in each of the list item (highlighted in bold).
NOTE: We could have named the first line home.html; however, it is best practice to name the "home page" index.html.
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="products.html">Products</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="contact_us.html">Contact Us</a></li>
</ul>
NOTE: Notice how each word got wrapped within an "a" element with an "href" attribute.
However, the list still looks more like a bullet list with links associated with it. Let's correct this problem.
<body>
<ul id="nav">
<style> /*----- Style List ------- */
#nav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: antiquewhite; }
</style>
NOTE: The #nav CSS rule performed the following tasks:
<style> /*----- Style List ------- */
#nav { height: 40px;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: antiquewhite; }
</style>
By default, "li" elements are block elements. There are two ways to force them to display as inline so they can display side-by-side:
Now, let's address the list Items so that they sit side-by-side instead on top of one another
/*------ STYLE LIST ITEM -----*/ #nav li {float: left;} </style>
Now, let's style the "a" element: to look like menu items instead of links:
/*------ STYLE LINKS -----*/ #nav li a {
display: block;
color: white; text-decoration: none; padding: 14px 16px;
text-align: center; font-family:Arial; font-weight: bold; } </style>
NOTE: The "li a" CSS rules performed the following tasks:
Change the background color in the #nav from antiquewhite to #333 so that the text can be seen better:
#nav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color:#333;
}
/* -------- STYLE MENU HOVER --------*/ #nav li a:hover {background-color: black;}
</style>
Add an "active" id to the current link to let the user know which page he or she is on:
<li><a id="active" href="index.html">Home</a></li>NOTE: Notice again, how an object is used (<li> element), given a name (class="active") and will be told to do something (change background color for current page) in the next step
/* --------- ACTIVE MENU ITEM ---------*/ #active { background-color: green; }
</style>
A menu item can be right-align links by floating a list item to the right:
<li class="right"><a href="contact_us.html">Contact Us</a></li>
Add the following class to the style element:
/* --------- RIGHT ALIGN MENU ITEM -------*/ #nav li.right{float:right;} </style>
<li class="right"><a href="services.html">Services</a></li> <li><a href="contact_us.html">Contact Us</a></li>
To make it obvious the the menu items are separate, dividers are added.
/* -------- MENU DIVIDERS --------*/NOTE: The second selector is used to prevent the last menu item (Contact Us) that has already been floated to the right to have a right border on it. While this is not a big problem if the current background color is white, if the divider color or background color was changed it would become obvious.
#nav li {border-right: 1px solid white;}
#nav li:last-child {border-right: none;} </style>
The menu can be fixed to the top or bottom of the page.
In Code View, BELOW the closing tag (</ul>), type p>Lorem800 and then press the tab key in Dreamweaver to add some Lorem Ipsum placeholder text.
</ul>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos vel ut corporis assumenda consectetur at possimus tenetur quo adipisci corrupti, temporibus perferendis reiciendis, provident fuga quam amet quas iste quisquam? Magnam facere fuga vitae reiciendis nihil tempora delectus maxime, porro perferendis dolorem doloribus quia debitis eius deserunt qui, amet explicabo quaerat blanditiis necessitatibus dicta hic totam officiis perspiciatis? Totam q dignissimos dolorem voluptas...
/* -------- FIXED NAVIGATION BAR --------*/ ul {position:fixed; top:0; width:100%;}
</style>
#nav ul {position:fixed; bottom:0; width:100%;}
</style>
/* -------- FIXED NAVIGATION BAR --------*/ #nav ul {position:fixed; top:0; width:100%;}
A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport, then it is positioned fixed (e.g., position:fixed).
<div style="margin:50px 0;text-align:center;font-family:Arial">
<h1> RICH MEDIA CREATIVE SERVICES</h1>
<h2>Building Richer Web Experience</h2>
<button>Learn More</button>
</div>
<ul id="nav">
Comment out the previous CSS and add the following CSS rules to the style element:
/* -------- FIXED NAVIGATION BAR --------*/
/* ul {position:fixed; top:0; width:100%;} */ /* -------- STICKY MENU --------*/ ul { position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;}
</style>
CAUTION: Internet Explorer, Edge 15 and earlier versions do not support sticky positioning. Safari requires a -webkit- prefix (see example above). You must also specify at least one of top, right, bottom or left for sticky positioning to work.
TIP: Check the caniuse website and do a search on "Sticky" to find the current standards.
CSS media queries can be used to create a responsive design for mobile and tablets.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
Add the following media query within the style element
/* ----- Responsive Design ----- */
@media screen and (max-width: 600px) {ul#nav li.right, ul#nav li {float: none;}}
</style>