Using Inline List Elements

Posted December 31, 2007 at 9:46 AM by Ben Nadel

Tags: HTML / CSS

List elements (the LI within UL and OL elements) are, by default, block level elements. As we, as a community, have moved more towards web standards, many of us have began to use lists for our navigation claiming that semantically, navigation is a "list" of links to sections of our site. To do this, many of us, myself included, have turned list elements from block level displays to float elements (float: left or float: right). Up until just recently, I had only every considered these two types of display styles.

Then, last weekend, I was working on a project that had a list of navigation links that I was having a lot of trouble formatting. For one, they were not really formatted in any special way - no background images, no crazy padding. And, they were supposed to be aligned right. It was the right-alignment that was really tripping me up. How could I get the links to align right if they were float: left? I couldn't make them float: right, because then the links would have to be in the HTML in reverse order (which just didn't feel like an acceptable solution). I also didn't want to make the UL a "tight" width so as to make it appear that the links were right aligned as that would not leave any room for text size increases.

I kept thinking about how easy this would be if I just made the links standard Anchor tags within in a Paragraph and just aligned the paragraph left. That's when it dawned on me! Inline list elements. What if I could just make the UL act like it was a paragraph and the LI elements were just like Span tags within it?!? That would solve all my text alignment problems for simple navigation lists.

I don't think I have really ever seen that, or if I have, I brushed it off, so I thought I would give it a shot:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Inline List Elements</title>
  •  
  • <style type="text/css">
  •  
  • body {
  • font-family: georgie, verdana, arial ;
  • font-size: 100% ;
  • }
  •  
  • ul {
  • background-color: #F0F0F0 ;
  • border-bottom: 2px solid #999999 ;
  • list-style-type: none ;
  • margin: 0px 0px 20px 0px ;
  • padding: 7px 5px 7px 5px ;
  • }
  •  
  • ul.left {}
  •  
  • ul.right {
  • text-align: right ;
  • }
  •  
  • ul li {
  • display: inline ;
  • padding: 0px 3px 0px 3px ;
  • }
  •  
  • </style>
  • </head>
  • <body>
  •  
  • <h1>
  • Inline List Elements
  • </h1>
  •  
  • <!---
  • This list element will have left-align text (and
  • therefore left aligned list elements).
  • --->
  • <ul class="left">
  • <li>
  • <a href="##">Home</a>
  • </li>
  • <li>
  • <a href="##">Archive</a>
  • </li>
  • <li>
  • <a href="##">Contact Us</a>
  • </li>
  • </ul>
  •  
  •  
  • <!---
  • This list element will have right-aligned text (and
  • therefore right-aligned list elements).
  • --->
  • <ul class="right">
  • <li>
  • <a href="##">Home</a>
  • </li>
  • <li>
  • <a href="##">Archive</a>
  • </li>
  • <li>
  • <a href="##">Contact Us</a>
  • </li>
  • </ul>
  •  
  • <p>
  • This is an example of inline list elements with
  • text alignment.
  • </p>
  •  
  • </body>
  • </html>

Notice that the LI style is display: inline. Running the above code, we get the following web page:


 
 
 

 
Inline List Elements For Page Navigation Links  
 
 
 

That works quite nicely. No need to mess around with floats; no need to take elements out of the document flow. All I want to do is change the way it works and making it inline gets 'er done. This was tested in FireFox 2 and IE 6.



Reader Comments

Rob
Dec 31, 2007 at 11:32 AM // reply »
1 Comments

Ben,
Using display:inline for list items is at least as common as floating them for navigation. The disadvantage is that the elements are now using the inline box model, but in this case, that doesn't really matter, and as you point out, it's the most effective way to achieve your desired result.


Dec 31, 2007 at 11:41 AM // reply »
10,640 Comments

@Rob,

I figured it was common. Maybe my brain just tuned it out whenever I encountered it.


Dec 31, 2007 at 9:20 PM // reply »
44 Comments

There are a whole bunch of display types other than block and inline to check out too, although browser support can vary; inline-block and table-cell to name a couple which I find useful.

For your navigation you might also have been able to put float:right on the UL and float:left on the LI's to achieve what you wanted with the nav on the right and the items appearing in the correct order :)


Jan 2, 2008 at 7:33 AM // reply »
10,640 Comments

@Justin,

I have seen those other displays used before, but I never really looked into them. Cross browser support is very important in my work, most of the time (but not always). I will look into it.

As for the float-right / float-left thing, good suggestion, but it would not quite work. The UL itself is being absolutely positioned within a parent DIV and such the UL on the float would not do anything. But with static positioning, I think that would have worked. Thanks.


Jan 2, 2008 at 9:18 AM // reply »
44 Comments

Oh yeah, if you need to absolutely position the UL then floting it to the right is out (unless you can put it inside another element that is absolutely positioned instead)... Generally it should work fine though :)


Jan 2, 2008 at 9:41 AM // reply »
10,640 Comments

@Justin,

Good call with the parent element.


seb
Jan 28, 2010 at 11:47 AM // reply »
1 Comments

I really like this approach however loosing the block model cannot specify the size (width and height)of the A tag.
Could I use your technique specified the size of the A elements. Very important to me as I need to display image background with different image size.

Thanks


Jan 28, 2010 at 10:19 PM // reply »
10,640 Comments

@Seb,

I have seen some different behavior in the different browsers. In some browsers, I have found that setting the A elements to "display: block" works. However, I think in IE this doesn't always work; in those cases, I typically have the A tag FLOAT in the same direction as the inline list element.

So, I might have something like this:

li { float: left ; }

li a { float: left ; }

This should give you styling ability (width, height, etc).


Feb 11, 2010 at 9:13 AM // reply »
1 Comments

For those looking for a good CSS/List tutorial we have our friends at ALA to thank for this;

http://www.alistapart.com/articles/taminglists/


Mar 19, 2010 at 9:12 AM // reply »
1 Comments

@ben: thank you very much - that helped me a lot ;)


Feb 27, 2011 at 3:28 PM // reply »
1 Comments

That helped me, thanks man!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »