Custom bullet symbol for <li> elements in <ul> that is a regular character, and not an image
Asked Answered
F

19

178

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?

Fluid answered 8/10, 2011 at 18:11 Comment(0)
G
96

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

Gratis answered 8/10, 2011 at 18:22 Comment(3)
Your solution worked, in combination with the :before pseudo-selector that you and @Tieson T. both point to. I liked that you called out how the various attributes on <UL> work in concert to mimic bullet indentation.Fluid
This is how I put it together, which worked: ul { font-size: 14px; line-height: 16px; list-style: none; margin-left: 0; padding-left: 1em; text-indent: -1em; } li:before { content: "+ "; } I did have to put a space after the + symbol, but it looks reasonably well aligned.Fluid
Unfortunately with this method the bullet sign gets included in selections which is not the case for the normal bullets.Tedium
W
200

This is a late answer, but I just came across this... To get the indenting correct on any lines that wrap, try it this way:

ul {
  list-style: none;
  margin-left: 0;
  padding-left: 0;
}

li {
  padding-left: 1em;
  text-indent: -1em;
}

li:before {
  content: "+";
  padding-right: 5px;
}
Wallop answered 31/8, 2012 at 14:2 Comment(2)
For me, it works better to use ch as the unit of measurement, specifically to do text-indent: -2ch to account for the + sign and the space after it, then padding-right: 1ch to add that space. Still, a big +1.Heater
This does not work in IOS. Safari's support for marker is limited to color and font-size: developer.mozilla.org/en-US/docs/Web/CSS/::marker (taken from https://mcmap.net/q/144115/-css-pseudo-element-marker-not-visible-on-mobile-phones-ios)Neurovascular
G
96

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

Gratis answered 8/10, 2011 at 18:22 Comment(3)
Your solution worked, in combination with the :before pseudo-selector that you and @Tieson T. both point to. I liked that you called out how the various attributes on <UL> work in concert to mimic bullet indentation.Fluid
This is how I put it together, which worked: ul { font-size: 14px; line-height: 16px; list-style: none; margin-left: 0; padding-left: 1em; text-indent: -1em; } li:before { content: "+ "; } I did have to put a space after the + symbol, but it looks reasonably well aligned.Fluid
Unfortunately with this method the bullet sign gets included in selections which is not the case for the normal bullets.Tedium
M
91

This is the W3C solution. Works in Firefox, Chrome and Edge.

ul { list-style-type: "🔔"; }
/* Sets the marker to a 🔔 emoji character */

http://dev.w3.org/csswg/css-lists/#marker-content

ul { list-style-type: "🔔 "; }
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>

You can also style that inline. Combine single ' and double quotes " to avoid conflict:

<ul style="list-style-type: '🔔 ';">
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>

If you can't risk to write unicode in your source code you still can set the HTML entity (ie: &#x1F514;) in the list-style-type and it will be properly rendered (ie: 🔔) - Run the code snippet to try it out.

<ul style="list-style-type: '&#x1F514; ';">
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
Mezzorelievo answered 24/7, 2013 at 7:58 Comment(4)
Although it seems to work in current versions of Firefox, the result is extremely ugly: custom bullet is placed right in front of text content (snapping to it), almost as if li:before {content:"…";} was used without any other indentation and alignment.Boyd
@AntonSamsonov it seems extremely pretty to me.Mezzorelievo
I needed to an SVG instead of the default marker, so I used ul { list-style-image: url(assets/...); } -> Works perfectly!Hindemith
Wow, didn't know this was a thing way back in 2013! Without a doubt it's the simplest answer to the question—no hacks, full support by all the major browsers. This should be the accepted answer.Ari
T
69

So many solutions.
But I still think there is room for improvement.

Advantages:

  • very compact code
  • works with any font size
    (no absolute pixel values contained)
  • aligns rows perfectly
    (no slight shift between first line and following lines)

ul {
	position: relative;
	list-style: none;
	margin-left: 0;
	padding-left: 1.2em;
}
ul li:before {
	content: "+";
	position: absolute;
	left: 0;
}
<ul>
  <li>Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Pellentesque in ipsum id orci porta dapibus.</li>
  <li>Nulla porttitor accumsan tincidunt. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Aliquet quam id dui posuere blandit. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.</li>
</ul>
Theorist answered 15/9, 2016 at 10:46 Comment(7)
this is the very bestDesrochers
This is a better answer in my opinion because it points out how you can customize other details as well. And it also has the "Run code snippet" attached!Hebraize
This is should be the answer. Clean and simpleEnwomb
I was looking for a solution using an image and tried lots of answers, but this one worked best for me! I was able to use content: url('link-to-my-asset.svg'); and set the image width and padding between the bullet and content. Thanks!Regenaregency
This is a good answer. However, I needed a solution which could support column-count as wellDyak
Great answer! Like @KushagrArora I was looking for a multi-column answer, but I got it to work by adding absolute positioning to the <li> insteadEliaeliades
This is perfect!X
L
21

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.

Luthern answered 17/2, 2014 at 15:6 Comment(3)
This is easily the best solution I've found.Kotick
ab-use of float again? ... naah.Ala
And it should be li::before with double colons, right?Epithet
S
14

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>
Salsbury answered 4/12, 2015 at 2:24 Comment(0)
L
11

You can use the :before pseudo-selector to insert content in front of the list item. You can find an example on Quirksmode, at http://www.quirksmode.org/css/beforeafter.html. I use this to insert giant quotes around blockquotes...

HTH.

Lucent answered 8/10, 2011 at 18:19 Comment(0)
M
11

My solution uses positioning to get wrapped lines automatically line up correctly. So you don't have to worry about setting padding-right on the li:before.

ul {
  margin-left: 0;
  padding-left: 0;
  list-style-type: none;
}

ul li {
  position: relative;
  margin-left: 1em;
}

ul li:before {
  position: absolute;
  left: -1em;
  content: "+";
}
<ul>
  <li>Item 1 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 2 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 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>
Microtome answered 6/1, 2015 at 10:11 Comment(0)
L
9

It's advisable to qualify the styling of the <li> so it does not affect <ol> list items. So:

ul {
    list-style: none;
    margin-left: 0;
    padding-left: 0;
}

ul li {
    padding-left: 1em;
    text-indent: -1em;
}

ul li:before {
    content: "+";
    padding-right: 5px;
}
Least answered 6/10, 2013 at 21:22 Comment(0)
B
7

You can make use of ::marker pseudo-element. This is useful in situations when you need to have different character entities for each list item.

As @Kal mentions, content on ::marker is not supported in Safari at the time of edit.

ul li::marker {
  content: " "; /* Your symbol here */
}

ul li:nth-child(1)::marker {
  content: "\26BD   ";
}

ul li:nth-child(2)::marker {
  content: "\26C5   ";
}

ul li:nth-child(3)::marker {
  content: "\26F3   ";
}

ul li::marker {
  font-size: 20px;
}

ul li {
  margin: 15px 0;
  font-family: sans-serif;
  background: #BADA55;
  color: black;
  padding-bottom: 10px;
  padding-left: 10px;
  padding-top: 5px;
}
<ul>
  <li>France Vs Croatia</li>
  <li>Cloudy with sunshine</li>
  <li>Golf session ahead</li>
</ul>
Burkitt answered 13/2, 2021 at 10:8 Comment(1)
Definitely an option, but it would be worth mentioning in your answer that Safari still (as of Sep 2022) doesn't support the content property on the ::marker pseudo element. (Otherwise Safari users are wondering why they're seeing standard disc bullets in your code example.)Ari
D
5

.list-dash li, .list-bullet li {
    position: relative;
    list-style-type: none; /* disc circle(hollow) square none */
    text-indent: -2em;
}
.list-dash li:before {
    content: '—  '; /* em dash */
}
.list-bullet li:before {
    content: '• '; /*copy and paste a bullet from HTML in browser into CSS (not using ASCII codes) */
}
<ul class="list-dash">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
</ul>
Donley answered 31/12, 2013 at 8:19 Comment(1)
Up voted this just because of /*copy and paste a bullet from HTML in browser into css (not using ascii codes) */Vintner
C
5

.single-before {
  list-style: "👍";
  list-style-position: outside!important;
}
<ul class="single-before">
  <li> is to manifest perfection already in man.</li>
  <li> is to bring out the best facets of our students personalities.</li>
</ul>
Calcutta answered 18/11, 2019 at 12:43 Comment(1)
Thank you--This is very clean and I forgot about the !important, which was vital to get my custom CSS in Blogspot to workOleneolenka
P
4

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*/
}
Profession answered 30/1, 2019 at 8:42 Comment(0)
C
2

I prefer to use negative margin, gives you more control

ul {
  margin-left: 0;
  padding-left: 20px;
  list-style: none;
}

li:before {
  content: "*";
  display: inline;
  float: left;
  margin-left: -18px;
}
Candiot answered 4/8, 2015 at 9:14 Comment(0)
W
2

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.

Wed answered 27/3, 2016 at 7:42 Comment(0)
F
1

In Bootstrap you can use class list-unstyled. Then, instead bullet you can use for example HTML emoji or something else:

<ul class="text-dark display-4 list-unstyled">
    <li>&#127774; Phasellus iaculis neque</li>
    <li>&#127803; Purus sodales ultricies</li>
    <li>&#127774; Vestibulum laoreet porttitor sem</li>
    <li>&#127803; Ac tristique libero volutpat at</li>
</ul>
Foreskin answered 22/4, 2021 at 12:16 Comment(0)
D
1

First, thanks to @Jpsy as my answer is based on that.

I have extended that answer to support the Google Material Icons pack as the custom icon.

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<ul>
  <li>Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Pellentesque in ipsum id orci porta dapibus.</li>
  <li>Nulla porttitor accumsan tincidunt. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Aliquet quam id dui posuere blandit. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.</li>
</ul>
ul {
    position: relative;
    list-style: none;
    margin-left: 0;
    padding-left: 1.2em;
}

ul li:before {
    font-family: 'Material Icons';
    content: "double_arrow"; /* Change this to whichever icon you would like from https://fonts.google.com/icons?selected=Material+Icons */
    position: absolute;
    left: 0;
}

@font-face {
    font-family: 'Material Icons';
    font-style: normal;
    font-weight: 400;
    src: url(https://example.com/MaterialIcons-Regular.eot); /* For IE6-8 */
    src: local('Material Icons'),
      local('MaterialIcons-Regular'),
      url(https://example.com/MaterialIcons-Regular.woff2) format('woff2'),
      url(https://example.com/MaterialIcons-Regular.woff) format('woff'),
      url(https://example.com/MaterialIcons-Regular.ttf) format('truetype');
}
Dismissive answered 12/8, 2021 at 17:52 Comment(0)
F
0

Not the preetiest answer but i like minimal css.

ul { list-style-type: '\002B\00a0\00a0'; }

unicode for +: 002B

unicode for white space: 00a0

You can search for other symbols in unicode and just add white space until you are satisfied with positioning.

P.S.: Someone gave almost the same answer but I couldn't comment on it.

Frisbee answered 29/9, 2022 at 19:42 Comment(0)
B
-3

try this

    ul.a {
        list-style-type: circle;
    }
    
    ul.b {
        list-style-type: square;
    }
    
    ol.c {
        list-style-type: upper-roman;
    }
    
    ol.d {
        list-style-type: lower-alpha;
    }
    
<!DOCTYPE html>
    <html>
    <head>
    
    </head>
    <body>
    
    <p>Example of unordered lists:</p>
    <ul class="a">
      <li>Coffee</li>
      <li>Tea</li>
      <li>Coca Cola</li>
    </ul>
    
    <ul class="b">
      <li>Coffee</li>
      <li>Tea</li>
      <li>Coca Cola</li>
    </ul>
    
    <p>Example of ordered lists:</p>
    <ol class="c">
      <li>Coffee</li>
      <li>Tea</li>
      <li>Coca Cola</li>
    </ol>
    
    <ol class="d">
      <li>Coffee</li>
      <li>Tea</li>
      <li>Coca Cola</li>
    </ol>
    
    </body>
    </html>
Bennybenoit answered 22/2, 2016 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.