“The ::marker
CSS pseudo-element selects the marker box of a list item, which typically contains a bullet or number. It works on any element or pseudo-element set to display: list-item
, such as the <li>
and <summary>
elements.” [1]
In this tutorial:
<h2>
alternative- Further example
Required knowledge:
<h2>
alternative
In this example, we use the same scenario presented in the Number HTML elements automatically with CSS tutorial where we want to automatically number the steps in a tutorial where the steps are listed as <h2>
elements followed by text in various suitable HTML elements (paragraphs, etc). Rather than manually number them, we can do the following:
h2 { counter-increment: h2counter; display: list-item; } h2::marker { display: list-item; content: counter(h2counter) ". "; } body { counter-reset: h2counter; }
Main heading
Step One
Some text
Step Two
Some more text
Further example
In this second example, we want to automatically “label” the verses in a poem “Verse 1”, “Verse 2” etc.
The HTML:
<h1>Funeral Blues by W.H. Auden</h1> <p>Stop all the clocks, cut off the telephone,<br> Prevent the dog from barking with a juicy bone,<br> Silence the pianos and with muffled drum<br> Bring out the coffin, let the mourners come.</p> <p>Let aeroplanes circle moaning overhead<br> Scribbling on the sky the message He Is Dead,<br> Put crepe bows round the white necks of the public doves,<br> Let the traffic policemen wear black cotton gloves.</p> <p>He was my North, my South, my East and West,<br> My working week and my Sunday rest,<br> My noon, my midnight, my talk, my song;<br> I thought that love would last for ever: I was wrong.</p> <p>The stars are not wanted now: put out every one;<br> Pack up the moon and dismantle the sun;<br> Pour away the ocean and sweep up the wood;<br> For nothing now can ever come to any good.</p>
The CSS:
p { counter-increment: p; display: list-item; } p::marker { content: "Verse " counter(p) ": "; color: DarkGray; } body { counter-reset: p; margin: 100px; }
Results in:
Funeral Blues by W.H. Auden
Stop all the clocks, cut off the telephone,
Prevent the dog from barking with a juicy bone,
Silence the pianos and with muffled drum
Bring out the coffin, let the mourners come.
Let aeroplanes circle moaning overhead
Scribbling on the sky the message He Is Dead,
Put crepe bows round the white necks of the public doves,
Let the traffic policemen wear black cotton gloves.
He was my North, my South, my East and West,
My working week and my Sunday rest,
My noon, my midnight, my talk, my song;
I thought that love would last for ever: I was wrong.
The stars are not wanted now: put out every one;
Pack up the moon and dismantle the sun;
Pour away the ocean and sweep up the wood;
For nothing now can ever come to any good.
References:
- MozDevNet (no date) ::marker – CSS: Cascading Style Sheets: MDN, MDN Web Docs. Available at: https://developer.mozilla.org/en-US/docs/Web/CSS/::marker (Accessed: 18 November 2024).
- W3Schools.com (no date) W3Schools Online Web Tutorials. Available at: https://www.w3schools.com/cssref/sel_marker.php (Accessed: 18 November 2024).