spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / css_style2 / 1 current pageTo page 2To page 3
[next]

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

How to Style an Unordered List with CSS

By Stu Nicholls.

Introduction

With this article I will show you how to style an unordered navigation list using CSS, then take it a step further and show you how to add a pop-up information box for each link item so that your visitors will get an indication of the content for the page link.

We begin with an unstyled unordered list (old masters again):

Part one of this tutorial will show how, using just CSS, we can produce a list like this example list one.

Part two will add a pop-up information box to produce a styled lists like this example list two. The third part of this tutorial will show you how (with just a small change of style), we can have the pop-up information box looking like this example list three.

Methods

Part one

The !DOCTYPE

Firstly and most importantly, make sure that you have the correct (X)HTML !DOCTYPE. Without this most browsers will be thrown into 'quirks' mode which will lead to all sorts of incompatibility problems. W3C QA - List of valid DTDs has a list of valid DOCTYPES that can be used. Select from XHTML1.0 or XHTML1.1 as these are more suitable for this styling. I use XHTML1.1 for all my current web pages.

The (x)html for the basic unordered list is as below. This is a simple unordered list with no frills. The links would normally take you to another page giving information about each painter but I have used #nogo which will have no effect on this page.

<ul>
  <li>
    <a href="#nogo">Paul Cézanne</a>
  </li>
  <li>
    <a href="#nogo">Henri Matisse</a>
  </li>
  <li>
    <a href="#nogo">William Turner</a>
  </li>
  <li>
    <a href="#nogo">John Constable</a>
  </li>
  <li>
    <a href="#nogo">Claude Monet</a>
  </li>
</ul>

The only change necessary to the above (x)html is to add a unique id to the <ul> tag. This is done so that we can target the list with our CSS.

So the (x)html list becomes:

<ul id="menu">
  <li>
    <a href="#nogo">Paul Cézanne</a>
  </li>
  <li>
    <a href="#nogo">Henri Matisse</a>
  </li>
  <li>
    <a href="#nogo">William Turner</a>
  </li>
  <li>
    <a href="#nogo">John Constable</a>
  </li>
  <li>
    <a href="#nogo">Claude Monet</a>
  </li>
</ul>

Styling part one

Step 1

Removing the bullets

The first step is to style the unordered list to remove the bullets and the indentation.

Browsers have different ways of doing this; Internet Explorer and Opera use margin values for the indentation whereas Mozilla/Netscape/Firefox all use padding values, so to cater to this we need to style the list as follows:

#menu {
  padding:0;
  margin:0;
  }
#menu li {
  list-style-type:none;
  }

Part one step 1

In the above example I have added a light grey background to the body and a white background to the unordered list so that you can see exactly what effect this styling has on the list.

The bullets are gone and the list is now left aligned with no indentation. The unordered list stretches the full width of the screen (the white background of the <ul> tag is 100% by default).

Step 2

Styling the link width

The next step is to style the <a> and the <a:visited> tags which will be identical and can be grouped together.

Firstly, we will give the links a width just wide enough to hold the text in one line. In this case I have chosen to use em values for all sizes so that the menu will stay in shape when larger or smaller text sizes are chosen. It is also possible to use pixel or percentage values.

We can also add a border to each link just to show the size chosen:

#menu a, #menu a:visited {
  display:block;
  width:9em;
  border:1px solid #808;
  }

Part one step 2

The list items now have a fixed width and are enclosed in purple boxes. The unordered list is still the full width of the screen.

Step 3

Styling the link font

We can now style the font used for the links:

#menu a, #menu a:visited {
  display:block;
  width:9em;
  border:1px solid #808;
  font-family:arial, verdana, sans-serif; /* ADDED */
  font-size:0.8em; /* ADDED */
  text-align:center; /* ADDED */
  text-decoration:none; /* ADDED */
  }

Part one step 3

Our text is now centered within the link box and we have lost the underline.


home / programming / css_style2 / 1 current pageTo page 2To page 3
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes

Created: March 27, 2003
Revised: July 08, 2005

URL: http://webreference.com/programming/css_style2/1