WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[next]

How to Style an Unordered List with CSS

By

Social Bookmark

Oracle Report Developer (IL)
Next Step Systems
US-IL-Elk Grove Village

Justtechjobs.com Post A Job | Post A Resume




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 Example List One

Part two will add a pop-up information box to produce a styled lists like 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 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. The 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.

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:

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:

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:

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:

Part One/Step 3

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


[next]

Recent Articles

WebReference.com site name
Use Web Caching to Make Your Web Site Faster
Creating an Online Shopping Cart Mechanism in PHP
Log JavaScript Errors Using an AJAX-driven Web Service
internet.com site name
Configuring Granular Settings for a Database Level Audit
The Perils of a Web 2.0 Transition on Your Business Processes
Facebook Redesigns Site —Again — Nears 400M Mark



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers