Contact!

Changing bullet colour and icon of a html list element using CSS

Changing bullet colour and icon of a html list element using CSS

Changing bullet colour and icon of a html list element using CSS used to be quite a ‘hacky’ affair. The easiest way that I knew to achieve this effect was to change the ‘list-style’ to ‘none’ and use a ‘::before’ pseudo-element instead. As of time of writing, the top hit on google still suggests this approach.

As with most things, I hadn’t been asked to do anything like this for many years. Recently I’ve become aware of a much better and more modern way to achieve this, using the ‘::marker’ pseudo-element.

Changing bullet colour

To demonstrate this we will first need some simple html:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
</ul>

Now using css we can simply target the ‘::marker’ pseudo-element of the list element:

li::marker {
    color: green;
}

As you’ll be able to see, all the markers have changed to green.

Changing bullet icon

Using the same html we can change the symbol used to denote the marker using the ‘content’ attribute. Add the following to the CSS:

li::marker {
    color: green;
    content: "⇢";
}

In this example we’re adding a unicode character to the content so that we see a ‘Rightwards Dashed Arrow’ instead of the normal filled circles.

Here is a quick fiddle to demonstrate this approach.

Note: You can also change things like the font-size and font-family of a marker pseudo-element. Why not give it a go?

Bonus: Adding markers to other elements

In theory, we can use the marker pseudo-element on whatever elements we like. However, to do this we have to change the display property of the element to ‘list-item’.

h1 {
    display: list-item;
}

h1::marker {
    content: "😊";
}

Here we’ve made some happy h1 tags 😊.

Join the discussion!

You might like:

Create, register and use shortcodes in WordPress

Create, register and use shortcodes in WordPress

by Gav 18/03/2023

Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.

How to use guard clauses in JavaScript

How to/why use guard clauses in JavaScript

by Gav 16/03/2023

Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.

Implements and Extends, Object Oriented TypeScript

Implements and Extends, Object Oriented TypeScript

by Gav 15/03/2023

Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.

Reading/Parsing and Writing YAML files in PHP, Symfony

Reading/Parsing and Writing YAML files, PHP Symfony

by Gav 14/03/2023

In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.

Measuring code execution performance in JavaScript

Measuring code execution performance in JavaScript

by Gav 13/03/2023

Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.

Measuring script/code execution time in PHP using microtime

Measuring script/code execution time in PHP, microtime

by Gav 06/03/2023

Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

by Gav 25/02/2021

Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.

Magic Constants in PHP. What they are and how to use them.

Magic Constants in PHP. What they are and how to use them

by Gav 15/02/2021

Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.

Detecting Keypress JavaScript

Detect single and multiple keypress events: JavaScript

by Gav 16/10/2019

Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!