CSS styling for horizontal list with bullet only between elements

后端 未结 11 2340
刺人心
刺人心 2020-12-09 07:23

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

\"Centered

相关标签:
11条回答
  • 2020-12-09 07:52

    user2511031's solution is almost ideal... it's just not a valid HTML. There should not be any SPAN outside LI, inside UL.

    But it doesn't mean that there is no really ideal solution. I found one!

    No need to put the spans allover and clean white-spaces in the markup. Place the needed space into the ":after" pseudo element content, apply the background image to it.

    It does the same!

    ul { max-width: 700px; padding: 0; text-align: center; }
    ul li { display: inline; white-space: nowrap; }
    ul li:after {
        content: " ";
        word-spacing: 2em;
        background-repeat: no-repeat;
        background-position: 50% 60%;
        background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAOElEQVQI113M0Q3AIBRC0aM76P7jmHSmSj/6mibyc4EQkEEWuYtDmU1SXO1d6H7tjgupIl8+P+cD22cff9U1wbsAAAAASUVORK5CYII=);
    }
    

    Here's the Fiddle

    0 讨论(0)
  • 2020-12-09 07:53

    For almost all browsers, you can use the CSS3 selector last-child instead of JavaScript:

    ul li { display: inline; white-space: pre; }
    ul li:after { content: "  \00b7  "; }
    ul li:last-child:after { content: ""; }
    

    The white-space: pre stops wrapping within list items (because usually you want it to wrap between list items), and is a hack that allows you to increase the space between list items by adding spaces on the second line.

    u00b7 ⋅ (MIDDLE DOT) is the standard unicode character for interpuncts, but you could also use u2022 • (BULLET),   u2b24 ⬤ (BLACK LARGE CIRCLE),   U+2043 ⁃ (HYPHEN BULLET), or any other unicode character you choose.

    Note that some characters may not be supported on all systems.

    0 讨论(0)
  • 2020-12-09 07:54

    This solution matches all of OP's requirements, except IE8 compatibility (that was 2013).

    Simple markup. No JavaScript. No :last-child

    Link to CodePen

    <ul>
        <li><a>Profile Image</a></li>
        <li><a>Name</a></li>
        <li><a>Activity Information</a></li>
        <li><a>Distance</a></li>
        <li><a>Pace</a></li>
        <li><a>Points Earned</a></li>
    </ul>
    
    ul { display:inline-block; padding:0; text-align:center }
    li { display:inline }
    li a { white-space:nowrap }
    li:after { content:" "; letter-spacing:1em; background:center center no-repeat url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwAAADsABataJCQAAABl0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC4xMkMEa+wAAAAnSURBVBhXY/Dz89MA4sNA/B9Ka4AEYQIwfBgkiCwAxjhVopnppwEApxQqhnyQ+VkAAAAASUVORK5CYII=); }
    

    0 讨论(0)
  • 2020-12-09 07:55

    http://jsfiddle.net/caramba/tSnnP/

    <div>
        <ul>
            <li><span class="icon bull"></span>xxx</li>
            <li><span class="icon bull"></span>xxx</li>
            <li><span class="icon bull"></span>xxx</li>
        </ul>
        <ul>
            <li><span class="icon bull"></span>xxx</li>
            <li><span class="icon bull"></span>xxx</li>
            <li><span class="icon bull"></span>xxx</li>
            <li><span class="icon bull"></span>xxx</li>
            <li><span class="icon bull"></span>xxx</li>
        </ul>
    </div>
    
    <style type="text/css">
    div {
        white-space: nowrap;
        width: 100%;
    }
    span {
        display:inline-block;
        margin:0 5px;
    }
    ul {
        text-align:center;
    }
    ul li {
        display:inline;
        margin:20px;
    
    }
    .hide {
        display:none;
    }
    
    .icon {
        position:relative;
        display:inline-block;
        background-position:-1000px -1000px;
        background-image:url(http://www.alexander-bown.com/wp-content/uploads/2011/05/big-black-dot.jpg);
        background-repeat:no-repeat;
        background-size:5px 5px;
        width:5px;
        height:5px;
    }
    .icon {
        background-position:0px 0px;
        top:-2px;
    }
    </style>
    
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('ul li:first-child').children('span').addClass("hide");
        });
    </script>
    
    0 讨论(0)
  • 2020-12-09 07:56

    For those of you who don't have to worry about IE8, this is as simple as:

    ul li { list-style: none; display: inline; }
    ul li:after { content: " \00b7"; }
    ul li:last-child:after { content: none; }
    
    0 讨论(0)
  • 2020-12-09 08:07

    If you don't mind creating a PNG image (with transparent background) of the bullet (or other separator), then you can use a natural space between the list items painted with this as the background.

    Where the list items wrap onto the next line, the space---and thus its background---won't be rendered.

    This avoids layout issues relating to the space taken up by the separator, as well as avoiding any Javascript/jQuery, taking advantage of the browser's own layout engine to do the work. You can adjust the space for the separator with the word-spacing attribute.

    You'll need to ensure there is no other whitespace within the markup that might otherwise be used as the natural space. You could use a higher-res image than the 5x5 here, in conjunction with background-size, so that it still looks ok when zooomed, but note IE8 doesn't support scaling of background images. The other drawback is that if you want to change the colour you'll have to edit the PNG.

    FIDDLE

    Code based on modifying @bleuscyther's answer:

    CSS :

    ul { max-width: 700px; padding: 0; text-align: center; }
    ul li { display: inline; white-space: nowrap; }
    ul .separator {
      word-spacing: 1.1em;
      background-repeat: no-repeat;
      background-position: 50% 60%;
      background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAOElEQVQI113M0Q3AIBRC0aM76P7jmHSmSj/6mibyc4EQkEEWuYtDmU1SXO1d6H7tjgupIl8+P+cD22cff9U1wbsAAAAASUVORK5CYII=);
    }
    

    HTML :

    <ul>
        <li><a href="http://google.com">Harvard Medical School</a></li><span class='separator'>
        </span><li><a href="http://google.com">Harvard College</a></li><span class='separator'>
        </span><li><a href="http://google.com">Harvard Medical School</a></li><span class='separator'>
        </span><li><a href="http://google.com">Harvard College</a></li><span class='separator'>
        </span><li><a href="http://google.com">Harvard Medical School</a></li><span class='separator'>
        </span><li><a href="http://google.com">Harvard College</a></li><span class='separator'>
        </span><li><a href="http://google.com">Harvard Medical School</a></li><span class='separator'>
        </span><li><a href="http://google.com">Harvard College</a></li>
    </ul>
    
    0 讨论(0)
提交回复
热议问题