Search code examples
tailwind-csstailwind-in-js

Tailswind css - "list-disc" is not styling <li> bullets correctly (double bullet symbols)


How does one use the list-disc class to style bullets, using Tailwindscss?

My package.json includes:

    "@tailwindcss/aspect-ratio": "^0.2.0",
    "@tailwindcss/forms": "^0.3.2",
    "@tailwindcss/line-clamp": "^0.2.0",
    "@tailwindcss/typography": "^0.4.0",
    "tailwindcss": "^2.1.1",
    "tailwindcss-stimulus-components": "^2.1.2",

I try using <ul class="list-disc"> both without and with the /typography plugin's class="prose" and they look different but neither is as expected, and Firefox and Chrome looks the same:

enter image description here

Without a container (List 1) with class="prose" the bullets are completely unstyled, no indent, and show browsers default bullet point.

With the class="prose" container (List 2) it does create a hanging indent, and a lighter bullet point but also has the browser default bullet point (so double bullet symbol):

Here's the HTML of creating that view:

  <div class="container mx-auto m-4">
    <h3>List 1</h3>
    <div>
       <ul class="list-disc">
        <li>Bullet one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence.</li>
        <li>Shorter second sentence. </li>
      </ul>
    </div>

    <h3>List 2</h3>
    <div class="prose">
      <ul class="list-disc">
        <li>Bullet one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence,  one long sentence.</li>
        <li>Shorter second sentence. </li>
      </ul>
    </div>
  </div>

I'm fairly new to Tailwindcss, so I might have missed some parent element that "resets" the default bullets?

POSSIBLE CULPRIT: The culprit is the m-4 class in the container div, adding margin exposes a browser-default bullet that is dangling off-screen, unless there is any padding or margin in which case it is no longer off-screen.


Solution

  • Tailwind's Preflight reset resets lists to be unstyled by default. Without a list-disc or list-decimal utility class, lists will have no bullet or numbers. Using the list-disc/list-decimal sets the list-style-type property, which sets the ::marker pseudo-element to be bullets, numbers, or other things. This is the behavior you see in your first example. The bullet is the browser's default bullet.

    When using Tailwind Typography, you should not need to use utility classes within the content, and you may run into unexpected issues with conflicting styles/specificity if you do. In Tailwind Typography, lists are styled by default. However, the typography plugin does not set the ::marker pseudo-element with list-style-type. Instead, it uses the ::before pseudo-element, which allows for more control over the appearance of the bullet.

    When using Tailwind Typography and the list-disc utility, these two methods do not conflict, since they do different things, so both are displayed. The darker bullet is likely the ::marker pseudo-element set by list-disc, while the lighter gray bullet is the ::before pseudo-element set by Tailwind Typography. Try using your browser's DevTools to see the pseudo-elements, as well as playing around with what properties are being set and how they affect the appearance.

    To avoid this duplicate behavior, simply remove the list-disc class from your list. If you need to customize your Tailwind Typography styles, see the Customization section in the docs. You can also poke around in the source to see how the default styles are set.