nicegui: Tailwind text colors set via ".classes" selectively don't work for buttons

Description

Trying to specify text color via “.classes” selector works well for labels and other elements but doesn’t work for buttons when the color has a specified hue as can be seen on examples below:

Example code:

from nicegui import ui

def main():
    ui.label('Label color works').classes("text-red-400")
    ui.button("Button color doesn't work").classes("text-red-400")
    ui.button("Unless it's pure color").classes("text-red")
    ui.run()

if __name__ in {"__main__", "__mp_main__"}:
    main()

Result: image

More examples using the styling function in the documentation, just to make sure it’s not an issue with my environment

For a label the “text-red-400” tailwind class works:

image

For a button, the same class doesn’t work:

image

Although this way it works:

image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I feel your pain @anatoli26! We’ve always had problems with coloring buttons. The color parameter should make it easier, but somehow it even adds to the confusion.

There’s been a similar problem with padding in ui.card. Quasar’s CSS rules made it very difficult to built NiceGUI’s layout system on top. In PR #3444 we finally solved it by manipulating Quasar’s CSS via JavaScript: https://github.com/zauberzeug/nicegui/blob/637e79585638daf2a2b02890b20105af5c4718fa/nicegui/static/nicegui.js#L438-L445

Maybe there’s a similar solution for buttons. We should check if there are any negative side effects, but maybe we can remove Quasar’s “!” important marker from this specific rule.

For reference, in #4415 we see an excerpt of quasar.css and its !important madness. There I refused to modify Quasar’s default behavior. But if we better understand the implications, it might be a viable solution - maybe for NiceGUI 3.0 due to breaking changes. I’m struggling to find an explanation for why they chose to do it this way.

In https://github.com/quasarframework/quasar/issues/5566 we see how Quasar encourages to set custom colors: Define a new “text-” or “bg-” class and apply it to the UI element. This doesn’t seem viable for NiceGUI because every new color would need to add new CSS definitions.

The ui.button element applies a few color classes as part of its default instantiation. Specifically the text-white class is giving you trouble here: image

You can override these by instantiating it with color=None:

from nicegui import ui


def main():
    ui.label("Label color works").classes("text-red-400")
    ui.button("Button color works with more steps", color=None).classes("text-red-400")
    ui.button("Unless it's pure color").classes("text-red")
    ui.run()


if __name__ in {"__main__", "__mp_main__"}:
    main()