htmlcss

How to style a horizontal list with bullets between items using CSS?


I'm not sure how to build a horizontal list that looks like this:

Centered list with bullets between items

Here are the rules:

The thing I'm not sure how to do is to make the bullets appear only between items, and not also before or after each row of items.


Solution

  • Here is a further improved version. I kept getting an inconsistency at certain page widths where two bullets would be missing rather than just the last one. i.e.

    link1 · link2 · link3 link4

    link5 · link6

    I think the issue was that removing the last bullet separator could itself affect the text flow if the page width was just right. The new script locks the original text flow by adding and removing literal line breaks.

    I have the same script to run every time the screen is resized so you don't get stuck with awkward line breaks.

    <style>
    ul { width: 700px; text-align : center }
    ul li { display: inline; white-space: nowrap; }
    ul li:after { content: " \00b7"; }
    ul li.nobullet:after { content: none; }
    </style>
    
    <body onresize="processBullets()" onload="processBullets()">
    <ul>
        <li><a href="http://google.com">Harvard Medical School</a></li>
        <li><a href="http://google.com">Harvard College</a></li>
        <li><a href="http://google.com">Harvard Medical School</a></li>
        <li><a href="http://google.com">Harvard College</a></li>
        <li><a href="http://google.com">Harvard Medical School</a></li>
        <li><a href="http://google.com">Harvard College</a></li>
        <li><a href="http://google.com">Harvard Medical School</a></li>
        <li><a href="http://google.com">Harvard College</a></li>
    </ul>
    <body>
    
    <script>
    function processBullets() {
        var lastElement = false;
        $("br").remove(".tempbreak");
        $("ul li").each(function() {
            $(this).removeClass("nobullet");
            if (lastElement && lastElement.offset().top != $(this).offset().top) {
                $(lastElement).addClass("nobullet");
                $(lastElement).append('<br class="tempbreak" />');
            }
            lastElement = $(this);
        }).last().addClass("nobullet");
    }
    
    </script>