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

4

8

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.

Isopiestic answered 2/6, 2021 at 3:30 Comment(3)
play.tailwindcss.com/8dgGwBhAOU?file=config has you can see i added all plugins but not stimulus , you might check if it doesn't add some extra stylingLatitudinarian
that is cool I didnt realize you could do that.Isopiestic
had a similar issue, but had a link embedded to an outdated minified version on a cdn for the typography plugin but also had it properly setup in the tailwind config and saw this exact behaviour. It was a residual from trying out the prose plugin before properly configuring the whole setup. Removing the hard link to the css in the head section fixed it.Inside
E
9

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.

Elidaelidad answered 2/6, 2021 at 8:16 Comment(1)
It seems like styling the ::before pseudo is not possible when using the typography plugin?Yentai
D
16

By default Tailwind will set <ul>'s left padding to zero. That's why bullets are not showing by default. Try adding pl-5 for example to it and that would solve the problem.

<ul class="pl-5 list-disc">
  <li>Item</li>
</ul>
Downandout answered 24/5, 2023 at 10:45 Comment(2)
haha thanks somehow this is the only one that worked for meMing
Certainly in the conversation for the dumbest default setting for list items I have ever seen.Visibility
K
15

As a solution you could add these two classes in your ol or ul tag

<ul class="list-disc list-inside">
            ...
            <li>...<li>
            ...
</ul>

Hope this will works for you.

Kenay answered 27/7, 2022 at 6:10 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Tartu
E
9

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.

Elidaelidad answered 2/6, 2021 at 8:16 Comment(1)
It seems like styling the ::before pseudo is not possible when using the typography plugin?Yentai
C
2

Don't know if this helps, but I had a "block" style applied to my "ul li" element that made the bullet disappear.

Once, I fixed that, the bullets showed up again.

Chihli answered 24/2, 2023 at 1:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.