17

The question pretty much explains it but I have list items I want to put a simple diamond icon before them but when I try to use ::before it ends up putting the image above instead of the same line and I can't really seem to find out how to put it right before the list icon on the same line.

Like I said the image is just a small diamond, its 5px by 5px

.list-menu::before {
  content: url('../images/menu-icons/image-before.png');
}
<div class="sb-slidebar sb-left sb-style-push">
  <nav>
    <ul>
      <li class="list-menu"><a href="#">Home</a></li>
    </ul>
  </nav>
</div>

1
  • What about changing the <li> icon itself.. Commented Oct 18, 2014 at 15:39

3 Answers 3

25

There's no need to use the ::before pseudo-element here at all. You can just use a background image:

.list-menu {
  background-image: url('https://place-hold.it/16'); /* your image */
  background-position: left center;
  background-repeat: no-repeat;
  padding-left: 20px; /* Adjust according to image size to push text across. */
}
<div class="sb-slidebar sb-left sb-style-push">
    <nav>
        <ul>
            <li class="list-menu"><a href="#">Home</a></li>
        </ul>
    </nav>
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! Can you explain when I should use :before and :after?
@Bob :before and :after are two pseudo-elements which were created when CSS introduced Generated Content. This specification documents what the precise use of such content is, but in short those are used to not expose certain information to the DOM tree whilst still making the content visible on the page.
But what if you want to do that on a different element? Say, a <p>
@TheRealChx101 you can use the same approach.
@JamesDonnelly Thanks. I had figured it out already. I hadn't looked at it that well.
8

Well, list-style-image is made for that.

Supported since IE 4.0. That should be enough I guess.

ul {
  list-style-image: url('http://placehold.it/12x12');
}
<ul>
  <li> Text content </li>
  <li> Text content </li>
  <li> Text content </li>
</ul>

Comments

2

Answer 2022

Nowadays you can use ::marker pseudo element

li::marker {
  content: ' 🤖 '
}
<ul>
  <li>item 1</li>
  <li>item 2</li>
  <li>item 3</li>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.