Зміст курсу
Tailwind CSS for Web Development
Tailwind CSS for Web Development
Buttons
Buttons are fundamental interactive elements in web design. Tailwind CSS provides a variety of utilities to style buttons effectively and manage their different states, such as hover, focus, and active.
Basic Button Styling
To style a basic button, you can use utilities for background color, text color, padding, border radius, and font weight.
index
index
index
Explanation
bg-yellow-500
: Sets the background color to a shade of blue;text-white
: Sets the text color to white;font-bold
: Makes the text bold;py-2
: Adds vertical padding (0.5rem or 8px);px-4
: Adds horizontal padding (1rem or 16px);rounded
: Adds small rounded corners.
Hover State
To change the appearance of a button when the user hovers over it, you can use the hover:
prefix with any utility class.
index
index
index
hover:bg-blue-700
: Changes the background color to a dark blue when the button is hovered over.
Focus State
To style a button when it is focused (e.g., when it is selected using the keyboard), you can use the focus:
prefix with any utility class.
index
index
index
In this example, when you hover the mouse cursor over a button, it changes to blue. If you use the Tab button, the button will turn red.
Active and Disabled States
Active and disabled states are used more seldom. However, we should consider it as well.
To style a button when it is active (e.g., when it is being clicked), you can use the active:
prefix with any utility class.
To style a button when it is disabled (e.g., when it cannot be interacted with), you can use the disabled:
prefix with any utility class.
index
index
index
Explanation
active:bg-green-800
: Changes the background color to a darker shade of green when the button is active (pressed);opacity-50
: Reduces the button's opacity to make it look disabled;cursor-not-allowed
: Changes the cursor to indicate that the button is not clickable;disabled
attribute: Maks it non-interactive.
Example of All States
Combining all states into one example
index
index
index
Explanation
bg-blue-500
: Sets the default background color;hover:bg-blue-700
: Changes the background color on hover;focus:outline-none
: Removes the default focus outline;focus:ring-2
: Adds a focus ring with a width of 2 pixels;focus:ring-blue-600
: Sets the color of the focus ring;focus:ring-opacity-50
: Sets the opacity of the focus ring;active:bg-blue-800
: Changes the background color when the button is active;text-white
: Sets the text color to white;font-bold
: Makes the text bold;py-2
: Adds vertical padding;px-4
: Adds horizontal padding;rounded
: Adds small rounded corners.
Note
If you need to delve deeper into the Tailwind Button Component, here is a link to the official documentation.
Дякуємо за ваш відгук!