DEV Community

Cover image for Design buttons like a Pro with Tailwind CSS
Sergej Samsonenko
Sergej Samsonenko

Posted on

Design buttons like a Pro with Tailwind CSS

Let's get down to business, today I'll show you some easy steps for creating clean, functional buttons with Tailwind CSS.

1. HTML markup

First, we need some basic HTML markup, all styling will happen through Tailwind classes.

<a href="#" class="...">
  Button
</a>
Enter fullscreen mode Exit fullscreen mode

2. Display and spacing

Now on to adding classes to the class attribute. On mobile our button will be full-width otherwise, it will have some sane spacing defaults. The content is centered vertically as well as horizontally.

flex sm:inline-flex justify-center items-center px-5 py-2
Enter fullscreen mode Exit fullscreen mode

3. Color

Next, we are adding background and text colors.

bg-blue-500 text-white
Enter fullscreen mode Exit fullscreen mode

4. Text

Some simple text styling.

font-semibold text-center
Enter fullscreen mode Exit fullscreen mode

5. Decoration

We are adding rounded borders and removing the default outline for a more polished look.

rounded-md outline-none
Enter fullscreen mode Exit fullscreen mode

6. States

Don't forget to add states. Hover, active plus focus. These are important to give the user proper feedback.

hover:bg-blue-600 active:bg-blue-700 focus-visible:ring ring-blue-300
Enter fullscreen mode Exit fullscreen mode

7. Transition

Finally adding a transition between the different states. Faster transition times usually work better for buttons (50ms to 150ms).

transition duration-100
Enter fullscreen mode Exit fullscreen mode

Final result

Tailwind CSS Buttons


<div class="bg-gray-100 py-16">
  <div class="flex flex-wrap justify-center gap-4 p-4">
    <a href="#" class="flex sm:inline-flex justify-center items-center bg-gradient-to-tr from-indigo-500 to-purple-400 hover:from-indigo-600 hover:to-purple-500 active:from-indigo-700 active:to-purple-600 focus-visible:ring ring-indigo-300 text-white font-semibold text-center rounded-md outline-none transition duration-100 px-5 py-2">
      Button 1
    </a>

    <a href="#" class="flex sm:inline-flex justify-center items-center bg-gradient-to-tr from-pink-500 to-red-400 hover:from-pink-600 hover:to-red-500 active:from-pink-700 active:to-red-600 focus-visible:ring ring-pink-300 text-white font-semibold text-center rounded-md outline-none transition duration-100 px-5 py-2">
      Button 2
    </a>

    <a href="#" class="flex sm:inline-flex justify-center items-center bg-blue-500 hover:bg-blue-600 active:bg-blue-700 focus-visible:ring ring-blue-300 text-white font-semibold text-center rounded-md outline-none transition duration-100 px-5 py-2">
      Button 3
    </a>

    <a href="#" class="flex sm:inline-flex justify-center items-center bg-green-500 hover:bg-green-600 active:bg-green-700 focus-visible:ring ring-green-300 text-white font-semibold text-center rounded-md outline-none transition duration-100 px-5 py-2">
      Button 4
    </a>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

More

šŸ‘‰ If you want to see more Tailwind CSS components and examples head over to Flowrift. It's a library full of beautifully designed Tailwind CSS UI blocks.

Top comments (0)