Home arrow HTML arrow Page 5 - Preloading HTML Content with CSS

Preloading HTML Content with CSS

Website designers often use JavaScript to expose dynamic content to visitors. Sadly, updating JavaScript can be painful at best. CSS can help to overcome JavaScript's limitations -- and the two together open up some lovely possibilities. Alejandro Gervasio explains, with dynamic examples.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 48
January 12, 2005
  1. · Preloading HTML Content with CSS
  2. · Rotating HTML content with JavaScript
  3. · Hiding HTML content with CSS
  4. · Building a HTML content rotator with CSS
  5. · Building a simple drop-down menu with CSS
  6. · Code explanation to the rescue

print this article

Preloading HTML Content with CSS - Building a simple drop-down menu with CSS
(Page 5 of 6 )

So far, we‘ve shown a couple of examples to clearly demonstrate how powerful and handy this hiding technique can be for generating on demand content. It would be good to expand out those capabilities to build up different applications, exposing the whole gamut of possibilities when using CSS to hide Web page elements. So, let’s start increasing our creativity by creating a simple drop-down menu, which is very suitable for our sample needs.

The basic logic to creating the drop-down menu consists of hiding and displaying selected <div> elements using the CSS display property, accordingly to the occurrence of an event -- generally, when the user clicks on a specific area or passes the mouse over it. Having defined the general concept, all we have to do is create our menu.
As usual, here is the code for our JavaScript functions:

<script language="javascript">
// loads global functions
 // switches on-off menu display
 // gets all <a> elements
 // makes array from <a> elements with class name 'navbar'
   // assigns onclick event handler to navbar <a> elements
// executes code once page is loaded

The CSS rules are the following:

<style type="text/css">
#button0 {
 position: absolute;
 width: 15%;
 top: 10px;
 left: 0px;
#button1 {
 position: absolute;
 width: 15%;
 top: 10px;
 left: 15%;
#button2 {
 position: absolute;
 width: 15%;
 top: 10px;
 left: 30%;
#menu0 {
 position: absolute;
 width: 14%;
 top: 30px;
 left: 0px;
#menu1 {
 position: absolute;
 width: 14%;
 top: 30px;
 left: 15%;
#menu2 {
 position: absolute;
 width: 14%;
 top: 30px;
 left: 30%;
.hidden {
 display: none;
a.navbar:link,a.navbar:visited {
 display: block;
 background: #0f0;
 font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
 color: #000;
 padding: 2px;
 text-align: center;
 text-decoration: none;
 border: 1px solid #000;
a.navbar:hover {
 text-decoration: underline;
.menu {
 display: block;
 background: #ffc;
 font: normal 11px "Verdana", Arial, Helvetica, sans-serif;
 color: #000;
 padding: 4px; 
 border: 1px solid #000;

Finally, the HTML markup is listed below:

<div id="button0"><a href="#" class="navbar" id="0">Company</a></div>
<div id="button1"><a href="#" class="navbar" id="1">Products</a></div>
<div id="button2"><a href="#" class="navbar" id="2">Contact</a></div>
<div class="hidden" id="menu0">
Profile<br />
<div class="hidden" id="menu1">
Web Compilers<br />
HTML Editors<br />
PHP Editors<br />
Database Software
<div class="hidden" id="menu2">
Technical Support<br />
Knowledge Base<br />
Administrative Support

In order to explain the logic behind this code, we’ll see in detail how it works, since this is the subject of the next section.

blog comments powered by Disqus

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials