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

Thay đổi màu dấu chấm (bullet) cho danh sách với CSS


Không thể đổi màu list-style trực tiếp. Cách chuẩn: tắt bullet mặc định, dùng ::before tạo bullet tùy chỉnh.

Ví dụ

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }
    ul {
      list-style: none;
      padding-left: 0;
    }
    ul li {
      font-size: 20px;
      font-weight: 500;
      padding-left: 1.5em;
      position: relative;
      margin: 8px 0;
    }
    ul li::before {
      content: "\2022";  /* Unicode bullet • */
      color: rgb(86, 18, 117);
      font-weight: bold;
      font-size: 1.2em;
      position: absolute;
      left: 0;
      top: 0;
      line-height: 1;
    }
  </style>
</head>
<body>
  <h1>Thay Đổi Màu Bullet Của Danh Sách</h1>
  <ul>
    <li>Lạc đà</li>
    <li>Dê</li>
    <li>Kirin</li>
    <li>Sư tử</li>
  </ul>
</body>
</html>

Kết quả

Danh sách 4 con vật với bullet màu tím đậm (rgb 86,18,117), to hơn chữ, căn chỉnh đẹp mắt.

Mở rộng

  • Icon Font Awesome: content: "\f00c"; font-family: "Font Awesome 5 Free"; font-weight: 900;
  • Hình ảnh: content: ""; background: url(...); width: 12px; height: 12px;
  • Emoji: content: "🌟";
  • Mỗi li màu khác: li:nth-child(1)::before { color: red; }