0

I want to be able to style my li list items when converted from markdown without having to tag them.

Specifically: I want to make bold the first part of a list item when it is formed like a key value pair.

The visual result should look like the equivalent of this:

<ul>
    <li><strong>key: </strong>value</li>
</ul>

So, is there a way to select the string inside the li item up to the colon?

1 Answers1

0

This isn't possible in CSS alone, but it is quite straightforward to deploy a snippet of javascript which surrounds the key: part of the text with a <span>:

<li><span class="key">key:</span> value</li>

which then enables you to style that part of the text.

Working Example:

const myList = document.getElementsByTagName('ul')[0];
const myListItems = myList.getElementsByTagName('li');

for (let i = 0; i < myListItems.length; i++) {

  let myListItemColonPosition = myListItems[i].textContent.indexOf(':')

  markup = '';
  markup += '<span class="my-key">';
  markup += myListItems[i].textContent.substring(0, myListItemColonPosition);
  markup += '</span>';
  markup += myListItems[i].textContent.substring(myListItemColonPosition);
  
  myListItems[i].innerHTML = markup;
  
}
li {
  font-size: 16px;
  line-height: 24px;
}

.my-key {
  font-weight: 700;
}
<ul>
<li>key0: value0</li>
<li>key1: value1</li>
<li>key2: value2</li>
<li>key3: value3</li>
<li>key4: value4</li>
</ul>
Rounin
  • 27,134
  • 9
  • 83
  • 108