CSS Basic
CSS ::marker Selector
Photo Credit to CodeToFun
π Introduction
The ::marker
pseudo-element in CSS is used to target and style the marker of list items. This includes the bullets in unordered lists (<ul>
) or the numbers/letters in ordered lists (<ol>
).
By using the ::marker
selector, you can customize the appearance of list markers beyond their default styling, giving you more control over list design in your web pages.
π‘ Syntax
The signature of the ::marker
Selector is as follows:
::marker {
/* CSS properties */
}
The ::marker
pseudo-element can only be applied to list items (<li>
), as these are the elements that contain markers.
π Example
Hereβs an example demonstrating the usage of the ::marker
selector:
β οΈ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS ::marker Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ul>
</body>
</html>
π¨ CSS
/* Style for the list markers */
ul li::marker {
color: red;
font-size: 20px;
content: "β"; /* Replaces the default bullet with an arrow */
}
In this example:
- The list markers (bullets) are changed to red with a larger font size.
- The default bullet is replaced with a right arrow (
β
), giving a customized look to the list.
π¬ Usage Tips
- The
::marker
pseudo-element can only change specific properties likecolor
,font-size
, andcontent
. You cannot apply other styles, such as background color or box-shadow, to the marker. - Use
::marker
to enhance the readability or aesthetic of lists in documents and websites. It's particularly useful when you want to match the design theme of your webpage. - You can change the marker style for ordered lists (
<ol>
) as well, customizing how numbers or letters appear.
Example for Ordered Lists
ol li::marker {
font-weight: bold;
color: blue;
content: "[" counter(list-item) "]"; /* Custom format for numbered items */
}
This will display the numbers in a bold, blue format with square brackets around them.
β οΈ Common Pitfalls
- The
::marker
selector is supported in most modern browsers, but older versions of Internet Explorer do not support this pseudo-element. Ensure you check browser compatibility for wider support. - Keep in mind that the
::marker
pseudo-element is quite limited in the range of styles it supports. If you need more customization, consider using inline content or pseudo-elements like::before
and::after
on the<li>
element.
π Conclusion
The ::marker
selector allows for greater control over the appearance of list markers, helping you align your list styles with the overall design of your webpage. While it has its limitations in terms of styling options, it provides a simple and effective way to make lists visually distinct.
By using ::marker
, you can customize lists without affecting the content, creating a cleaner and more appealing presentation.
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS ::marker Selector), please comment here. I will help you immediately.