I have a Carousel Object which contains Buttons. These buttons represent the "Semester" content that is shown below (an image is provided below).
I want the "semester 1" button to be Dark Blue by default and when another button is clicked I want that button to Dark Blue.
At the moment I can achieve this when I add the following focus:
class to the Button Component but since it’s a focus class it turn lightblue the moment I click somewhere else and the button "semseter 1" is not darkblue by default:
class="rounded-full w-64 dark:bg-blue-300 dark:hover:bg-blue-500 focus:bg-blue-500"
I tried adding an class="active:bg-blue-500"
but it was unsuccessful.
Why doesn’t simply adding an active
class won’t work and how can I make this work?
2
Answers
Tailwind’s
active
is about the CSS pseudo :active state and this is different from what you expect as an active button in your situation. For example, in plain JS:If you are using React, you’ll have to use a
useState
to store which button is active:The pseudo class
active
is corresponding to when the user has an interaction with an element (in this case, when the user is clicking on the button), but once the interaction is over the pseudo class won’t be active.What you want cannot be done with only css, you need some javascript to add a stlye or a class to the button you clicked on (and remove those class / style on the last clicked button)
For example if you are in vanilla js and you have three buttons with the class "navigation-buttons", what can do what you want is to add a class ‘active’ when one is clicked.