CSS: Control space between bullet and
  • 2019-01-03 12:35发布

    I'd like to control how much horizontal space a bullet pushes its <li> to the right in an <ol> or <ul>.

    That is, instead of always having

    *  Some list text goes
       here.
    

    I'd like to be able to change that to be

    *         Some list text goes
              here.
    

    or

    *Some list text goes
     here.
    

    I looked around but could only find instructions for shifting the entire block left or right, for example, http://www.alistapart.com/articles/taminglists/

    15条回答
    叛逆
    2楼-- · 2019-01-03 12:57

    To summarise the other answers here – if you want finer control over the space between bullets and the text in a <li> list item, your options are:

    (1) Use a background image:

    <style type="text/css">
    li {
        list-style-type:none;
        background-image:url(bullet.png);
    }
    </style>
    
    <ul>
        <li>Some text</li>
    </ul>
    

    Advantages:

    • You can use any image you want for the bullet
    • You can use CSS background-position to position the image pretty much anywhere you want in relation to the text, using pixels, ems or %

    Disadvantages:

    • Adds an extra (albeit small) image file to your page, increasing the page weight
    • If a user increases the text size on their browser, the bullet will stay at the original size. It'll also likely get further out of position as the text size increases
    • If you're developing a 'responsive' layout using only percentages for widths, it could be difficult to get the bullet exactly where you want it over a range of screen widths

    2. Use padding on the <li> tag

    <style type="text/css">
    ul {padding-left:1em}
    li {padding-left:1em}
    </style>
    
    <ul>
        <li>Some text</li>
    </ul>
    

    Advantages:

    • No image = 1 less file to download
    • By adjusting the padding on the <li>, you can add as much extra horizontal space between the bullet and the text as you like
    • If the user increases the text size, the spacing and bullet size should scale proportionally

    Disadvantages:

    • Can't move the bullet any closer to the text than the browser default
    • Limited to shapes and sizes of CSS's built-in bullet types
    • Bullet must be same colour as the text
    • No control over vertical positioning of the bullet

    (3) Wrap the text in an extra <span> element

    <style type="text/css">
    li {
        padding-left:1em;
        color:#f00; /* red bullet */
    }
    li span {
        display:block;
        margin-left:-0.5em;
        color:#000; /* black text */
    }
    </style>
    
    <ul>
        <li><span>Some text</span></li>
    </ul>
    

    Advantages:

    • No image = 1 less file to download
    • You get more control over the position of the bullet than with option (2) – you can move it closer to the text (although despite my best efforts it seems you can't alter the vertical position by adding padding-top to the <span>. Someone else may have a workaround for this, though...)
    • The bullet can be a different colour to the text
    • If the user increases their text size, the bullet should scale in proportion (providing you set the padding & margin in ems not px)

    Disadvantages:

    • Requires an extra unsemantic element (this will probably lose you more friends on SO than it will in real life ;) but it's annoying for those who like their code to be as lean and efficient as possible, and it violates the separation of presentation and content that HTML / CSS is supposed to offer)
    • No control over the size and shape of the bullet

    Here's hoping for some new list-style features in CSS4, so we can create smarter bullets without resorting to images or exta mark-up :)

    查看更多
    倾城 Initia
    3楼-- · 2019-01-03 12:59

    You can also use a background image replacement as an alternative, giving you total control over vertical and horizontal positioning.

    See the answer to this Question

    查看更多
    我欲成王,谁敢阻挡
    4楼-- · 2019-01-03 13:13

    It seems you can (somewhat) control the spacing using padding on the <li> tag.

    <style type="text/css">
        li { padding-left: 10px; }
    </style>
    

    The catch is that it doesn't seem to allow you to scrunch it way-snug like your final example.

    For that you could try turning off list-style-type and using &bull;

    <ul style="list-style-type: none;">
        <li>&bull;Some list text goes here.</li>
    </ul>
    
    查看更多
    叛逆
    5楼-- · 2019-01-03 13:14

    Here is another solution using css counter and pseudo elements. I find it more elegant as it doesn't require use of extra html markup nor css classes :

    ol,
    ul {
        list-style-position: inside;
    }
    
    li {
        list-style-type: none;
    }
    
    ol {
        counter-reset: ol; //reset the counter for every new ol
    }
    
    ul li:before {
            content: '\2022 \00a0 \00a0 \00a0'; //bullet unicode followed by 3 non breakable spaces
    }
    
    ol li:before {
            counter-increment: ol;
            content: counter(ol) '.\00a0 \00a0 \00a0'; //css counter followed by a dot and 3 non breakable spaces
    }
    

    I use non breakable spaces so that the spacing only affects the first line of my list elements (if the list element is more than one line long). You could use padding here instead.

    查看更多
    爱情/是我丢掉的垃圾
    6楼-- · 2019-01-03 13:15

    For list-style-type: inline:

    It's almost the same like DSMann8's answer but less css code.

    You just need to

    <style>
        li:before {
            content: "";
            padding-left: 10px;
        }
    </style>
    
    <ul>
        <li>Some content</li>
    </ul>
    

    Cheers

    查看更多
    爷的心禁止访问
    7楼-- · 2019-01-03 13:15

    You can use the padding-left attribute on the list items (not on the list itself!).

    查看更多
    登录 后发表回答