Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Thay đổi màu Bullet (Dấu đầu dòng) cho List với CSS ::marker

Giới thiệu

CSS ::marker pseudo-element (hỗ trợ Firefox 68+, Chrome 86+, Safari 15.1+, Edge 86+) cho phép style riêng cho bullet/number của list (<ul>, <ol>) mà không ảnh hưởng đến nội dung <li>. Thay thế hack cũ dùng ::before + list-style: none.

Cú pháp

li::marker {
    color: red;           /* Màu bullet/number */
    font-size: 1.2em;     /* Kích thước bullet */
    font-weight: bold;    /* Độ đậm */
    content: "→ ";        /* Thay đổi ký tự bullet (chỉ Firefox) */
}

Ví dụ 1: Đổi màu bullet tròn (circle)

<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>::marker Bullet Color</title>
    <style>
        body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: 2rem; max-width: 600px; margin: 0 auto; }
        h2 { color: #2c3e50; margin-bottom: 1rem; }
        
        ul {
            list-style: circle;     /* Hoặc disc, square */
            font-size: 1.2rem;
            padding-left: 1.5rem;
        }
        li::marker {
            color: #27ae60;         /* Màu xanh lá cho bullet */
            font-size: 1.5em;       /* Bullet to hơn text */
        }
        li { margin: 0.5rem 0; color: #333; }
    </style>
</head>
<body>
    <h2><strong>Ninja Rùa (TMNT)</strong></h2>
    <ul>
        <li>Raphael</li>
        <li>Donatello</li>
        <li>Michaelangelo</li>
        <li>Leonardo</li>
    </ul>
</body>
</html>

Ví dụ 2: Bullet vuông (square) + Layout ngang (Grid)

<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>::marker Square Bullets Grid</title>
    <style>
        body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: 2rem; background: #fafafa; }
        ul {
            list-style: square;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 1rem;
            padding-left: 1.5rem;
            max-width: 800px; margin: 0 auto;
        }
        li {
            background: white;
            padding: 1rem;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.06);
            transition: transform 0.2s;
        }
        li:hover { transform: translateY(-2px); box-shadow: 0 4px 16px rgba(0,0,0,0.1); }
        li::marker {
            color: #27ae60;
            font-size: 1.3em;
        }
    </style>
</head>
<body>
    <ul>
        <li>Thiết kế</li>
        <li>Phát triển</li>
        <li>Kiểm thử</li>
        <li>Triển khai</li>
        <li>Bảo trì</li>
        <li>Hỗ trợ</li>
    </ul>
</body>
</html>

Ví dụ 3: Ordered List (Số thứ tự) với ::marker

<style>
    ol {
        list-style: decimal;
        padding-left: 1.5rem;
        counter-reset: custom-counter; /* Không cần nếu dùng ::marker */
    }
    li::marker {
        color: #8e24aa;
        font-weight: 700;
        font-size: 1.2em;
        font-family: 'Georgia', serif; /* Font khác cho số */
    }
    li { margin: 0.75rem 0; color: #333; }
</style>

<ol>
    <li>Bước 1: Phân tích yêu cầu</li>
    <li>Bước 2: Thiết kế hệ thống</li>
    <li>Bước 3: Viết code</li>
    <li>Bước 4: Test & Deploy</li>
</ol>

Nâng cao: Custom Counter với CSS Counters (Không cần ::marker)

Nếu cần kiểm soát hoàn toàn định dạng số (VD: "01", "02", "Step 1:"):

ol.customCounter {
    list-style: none;
    padding-left: 0;
    counter-reset: step;
}
ol.customCounter li {
    position: relative;
    padding-left: 3rem;
    margin: 1rem 0;
}
ol.customCounter li::before {
    counter-increment: step;
    content: "Step " counter(step) ":";
    position: absolute; left: 0; top: 0;
    color: #8e24aa; font-weight: 700; font-size: 1.1rem;
}

Browser Support & Fallback

Trình duyệt Hỗ trợ ::marker Fallback
Chrome86+li::before + list-style:none
Firefox68+-
Safari15.1+li::before + list-style:none
Edge86+-

Fallback an toàn (hỗ trợ mọi trình duyệt):

li { list-style: none; position: relative; padding-left: 1.5em; }
li::before {
    content: "•"; /* Hoặc "\2022" */
    position: absolute; left: 0; top: 0;
    color: #27ae60; font-size: 1.2em; line-height: 1;
}

Các thuộc tính được hỗ trợ trên ::marker

  • color, font-size, font-weight, font-family
  • content (chỉ Firefox - thay đổi ký tự bullet)
  • direction, unicode-bidi, white-space
  • Không hỗ trợ: margin, padding, width, height, background, border, transform.