Vai al contenuto

Horizontal centered list (CSS)

Versione italiana

In order to help my friend Xmario to build his web site I modified this css bar, and because they are useful hacks, I make them public.
The problem of centered css bars usually is that between an element of the menù and the other there is an horizontal space. The author of the bar I modified used a negative margin of <li> tags to remove approximately this space. The problem is that it isn’t sharp and also if you change the text size the result is awful.

So, first of all I removed the modification:

div#navcontainer ul li
{
display: inline;
}

Then I tried to understand why there were those spaces and after many ideas I understood it. The fact is that in HTML code, when you start a new line of code without anything else as for example a <br> tag, the browser interprets that “return” as a simple space between a word and another. This is true also for the list elements.

So you have to put them all stitched together:

<li id="active">...</li><li>...</li>

But in this way the code is very uncomfortable to read and modify.

So I tried to find a way to put on every line of the code a different element of the list without problems. I resolved by creating a class (it will be not used) that lets me break the line inside the <li> tag.

If we name it zzzz the code becomes:

<div id="navcontainer">
<ul id="navlist"><li id="active"
class="zzzz"><a href="#" id="current">Element 1</a></li><li
class="zzzz"><a href="#">Element 2</a></li><li
class="zzzz"><a href="#">Element 3</a></li><li
class="zzzz"><a href="#">Element 4</a></li><li
class="zzzz"><a href="#">Element 5</a></li></ul>
</div>

And the CSS:

div#navcontainer
{
background-color: #1F00CA;
border-top: solid 1px #FFFFFF;
border-bottom: solid 1px #FFFFFF;
}
div#navcontainer ul
{
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
color: white;
text-align: center;
margin: 0;
padding-bottom: 5px;
padding-top: 5px;
padding-left: 0px;
}
div#navcontainer ul li
{
display: inline;
}
div#navcontainer ul li a
{
padding: 5px 10px 5px 10px;
color: white;
text-decoration: none;
border-left: 1px solid #fff;
border-right: 1px solid #fff; /* the borders have the same size */
margin-right: -1px; /* the value has to be like the above but negative */
}
div#navcontainer ul li a:hover
{
background-color: #16008D;
color: white;
}

Note: the code contains also a small hack for the borders of the menù elements.

04/06/2007 Update: Added a hack to remove a bug, which made the list to be a bit too much on the right.

19 commenti leave one →
  1. BDM permalink
    4 giugno 2007 12:25 pm

    Andrea,

    Thank you for this modified horizontal list, it is excellent!

    Cheers, Ben.

    Brisbane, Australia.

  2. 4 giugno 2007 1:56 pm

    @ BDM, thank you too.
    And note that I’m going to update it to be even more centered. :-)

  3. EbS permalink
    12 giugno 2007 10:39 am

    Simple yet very ingenious. I’m impressed!

  4. 12 giugno 2007 10:40 am

    Thanks. :-)

  5. 15 giugno 2007 11:54 am

    I tried to get something like this working,,, yet here it is!

    Great stuff mate.

    Grazie

  6. 15 giugno 2007 12:01 pm

    Prego (it means “you’re welcome” ;-) )
    But a part of the work was done by Rick Dyer, the author of the original list. :-)

  7. Mateusz permalink
    4 novembre 2007 2:41 am

    Great idea! Pity it’s not a thing to work in my case :)

    But then, adding to your idea, isn’t it possible to remove the “class=’foobar’” at all and leave only the line break in the “li” element’s angle brackets? haven’t tested this for standards conformity, but on first sight that might work :)

    Good luck

  8. 4 novembre 2007 12:48 pm

    Uhm… maybe yes. Try it. ;-)

  9. 3 febbraio 2008 11:28 am

    Brilliant! Thank you very much, this is exactly what I’ve been looking for!

  10. 20 febbraio 2008 10:52 pm

    Ah, curious! I always wondered how those annoying gaps got in there.. Thanks for the heads up! :)

  11. 21 febbraio 2008 2:52 pm

    You’re welcome.

  12. 20 ottobre 2008 10:55 am

    Hi Andrea

    The code works brilliantly in IE and Firefox but just wanted to know if you had a fix for Chrome which seems to create a bulleted list running vertically

    Thanks

    Alun

  13. 20 ottobre 2008 2:48 pm

    To be fair… I don’t know. I don’t use Chrome as it is not officially supported on Linux and it lacks a lot of needed features (at least by my POV). May you post a screenshot on a site like ImageShack so I can have a look? Thanks.

  14. Susan permalink
    22 aprile 2009 1:52 am

    Andrea, this is great. You saved me from pulling all my hair out trying to get some old modified (broken!) code to work, and you answered some questions too. Many thanks!

  15. 28 settembre 2009 7:07 pm

    I have a horizontal nav bar that has sub items. How do i center this navbar?

  16. 29 settembre 2009 11:23 am

    The same way you do that with this list. You just need to apply the css to the main list (not the sublists).

  17. 26 agosto 2010 10:33 am

    Thank You. That was very clever.

Trackback

  1. 2010 in review « Il blog di Andrea Lazzarotto
  2. CSS Listamatic: Back to the Future! (when it wasn’t obnoxious) « dogsmeat

Che cosa ne pensi?

Fill in your details below or click an icon to log in:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Connessione a %s...

Iscriviti

Ricevi al tuo indirizzo email tutti i nuovi post del sito.

Unisciti agli altri 78 follower

%d bloggers like this: