csspositionvisibilitybulletedlist

Add image icon to left of ordered list number


The issue

I already know how to add image bullets using the CSS background-image property. What I'm trying to do now is display a little graphic icon to the left of the line number in an ordered list.

The issue I keep running into is that when trying to place the image bullet it doesn't show on the screen. I can see that it is there by nudging the image along the X-axis to the right, so I don't know what it is not displaying correctly.

Note: I can get the icon to appear to the right of the number, before the text, but that is not the desired result.

My code

.action-item {
    background-image: url(https://cdn0.iconfinder.com/data/icons/clipboard-1/100/clipboard-10-512.png);
    background-position: -4px 2px;
    background-repeat: no-repeat;
    line-height: 1.5;
    background-size: 1.4em;
    list-style: none;
    margin-left: 0;
    padding-left: 0;
    text-indent: 1.4em;
}
<ol>
  <li class="action-item">item 1</li>
  <li>item 2</li>
  <li>item 3</li>
  <li class="action-item">item 4</li>
  <li class="action-item">item 5</li>
  <li>item 6</li>
</ol>

What I've tried already

I have tried configuring text-indent, padding-left, margin-left, line-height, all to no avail.

Ideas I haven't tried

My question

Is this possible with pure CSS, and if so, what am I doing wrong?


Solution

  • You could use counters and the before pseudo element:

    ol {
      list-style: none;
      padding: 0;
      margin: 0;
      counter-reset: orderedlist;
    }
    ol > li {
      counter-increment: orderedlist;
      padding-left: 1.4em;
      line-height: 1.5;
    }
    ol > li:before {
      content: counter(orderedlist)".";
      display: inline-block;
      width: 1.52em;
    }
    .action-item {
      background-image: url(https://cdn0.iconfinder.com/data/icons/clipboard-1/100/clipboard-10-512.png);
      background-position: -4px 2px;
      background-repeat: no-repeat;
      background-size: 1.4em;
    }
    <ol>
      <li class="action-item">item 1</li>
      <li>item 2</li>
      <li>item 3</li>
      <li class="action-item">item 4</li>
      <li class="action-item">item 5</li>
      <li>item 6</li>
    </ol>