Custom bullet symbol for
  • elements in
      tha
  • 2020-01-24 10:18发布

    I realize one can specify a custom graphic to be a replacement bullet character, using CSS attribute:

    list-style-image
    

    And then giving it a URL.

    However, in my case, I just want to use the '+' symbol. I don't want to have to create a graphic for that and then point to it. I'd rather just instruct the unordered list to use a plus symbol as the bullet symbol.

    Can this be done or am I forced to make it a graphic first?

    15条回答
    再贱就再见
    2楼-- · 2020-01-24 11:11

    Font-awesome provides a great solution out of the box:

    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
    
    <ul class='fa-ul'>
      <li><i class="fa-li fa fa-plus"></i> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</li>
      <li><i class="fa-li fa fa-plus"></i> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</li>
    </ul>

    查看更多
    混吃等死
    3楼-- · 2020-01-24 11:12

    Here is the best solution I've found so far. It works great and it's cross-browser (IE 8+).

    ul {
        list-style: none;
        margin-left: 0;
        padding-left: 1.2em;
        text-indent: -1.2em;
    }
    
    li:before {
        content: "►";
        display: block;
        float: left;
        width: 1.2em;
        color: #ff0000;
    }
    

    The important thing is to have the character in a floating block with a fixed width so that the text remains aligned if it's too long to fit on a single line. 1.2em is the width you want for your character, change it for your needs.

    查看更多
    可以哭但决不认输i
    4楼-- · 2020-01-24 11:14

    Interestingly enough I do not thing any of the posted solutions are good enough, because they rely on the fact that the character used is 1em wide, which does not need to be so (with maybe exception of John Magnolia's answer which however uses floats which can complicate things another way). Here is my attempt:

    ul {
      list-style-type: none;
      margin-left: 0;
      padding-left: 30px; /* change 30px to anything */
      text-indent: -30px;
    }
    ul li:before {
      content: "xy";
      display: inline-block; 
      width: 30px;
      text-indent: 0;
      text-align: center; /* change this for different bullet position */
    }
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam</li>
      <li>Item 4</li>
      <li>Item 5</li>
    </ul>

    This has these advantages (over other solutions):

    1. It does not rely on the width of the symbol as you see in the example. I used two characters to show that it works even with wide bullets. If you try this in other solutions you get the text misaligned (in fact it is even visible with * symbol in higher zooms).
    2. It gives you total control of the space used by bullets. You can replace 30px by anything (even 1em etc). Just do not forgot to change it on all three places.
    3. If gives you total control of positioning of the bullet. Just replace the text-align: center; by anything to your liking. For example you may try color: red; text-align: right; padding-right: 5px; box-sizing: border-box; or if you do not like playing with border-box, just subtract the padding (5px) from width (i.e. width:25px in this example). There are lots of options.
    4. It does not use floats so it can be contained anywhere.

    Enjoy.

    查看更多
    我想做一个坏孩纸
    5楼-- · 2020-01-24 11:15
    .single-before {
      list-style: "                                                                    
    查看更多
    【Aperson】
    6楼-- · 2020-01-24 11:16

    Em dash style:

    ul.emdash {
      list-style-type: none;
      list-style-position: inside;
      text-indent: -1.25em;
    }
    ul.emdash > li:before {
      content: "\2014\00A0"; /* em dash + space*/
    }
    
    查看更多
    Deceive 欺骗
    7楼-- · 2020-01-24 11:17

    The following is quoted from Taming Lists:

    There may be times when you have a list, but you don’t want any bullets, or you want to use some other character in place of the bullet. Again, CSS provides a straightforward solution. Simply add list-style: none; to your rule and force the LIs to display with hanging indents. The rule will look something like this:

    ul {
       list-style: none;
       margin-left: 0;
       padding-left: 1em;
       text-indent: -1em;
    }
    

    Either the padding or the margin needs to be set to zero, with the other one set to 1em. Depending on the “bullet” that you choose, you may need to modify this value. The negative text-indent causes the first line to be moved to the left by that amount, creating a hanging indent.

    The HTML will contain our standard UL, but with whatever character or HTML entity that you want to use in place of the bullet preceding the content of the list item. In our case we'll be using », the right double angle quote: ».

    » Item 1
    » Item 2
    » Item 3
    » Item 4
    » Item 5 we'll make
       a bit longer so that
       it will wrap

    查看更多
    登录 后发表回答