Published on

Advanced Guide: Make A User Card With Tailwind CSS

Tags
User Card

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that provides a set of pre-defined classes to create responsive and customizable user interfaces. It allows developers to build complex layouts and designs with ease, without writing any custom CSS. Tailwind CSS is gaining popularity among developers due to its simplicity and flexibility.

The description of User Card ui component

A user card is a common UI component used in many web applications. It displays information about a user, such as their name, profile picture, and other relevant details. User cards are often used in social media platforms, e-commerce websites, and other online communities.

Why use Tailwind CSS to create a User Card ui component?

Tailwind CSS provides a set of pre-defined classes that can be used to create a user card UI component quickly and easily. It eliminates the need to write custom CSS, which can be time-consuming and error-prone. With Tailwind CSS, developers can focus on the functionality of the user card and not worry about the styling.

The preview of User Card ui component

To create a user card UI component with Tailwind CSS, we will use the following classes:

  • bg-white: sets the background color to white
  • rounded-lg: rounds the corners of the card
  • overflow-hidden: hides any overflow content
  • shadow-md: adds a shadow to the card
  • p-6: adds padding to the card
  • flex: enables flexbox layout
  • items-center: centers the content vertically
  • gap-4: adds a gap between the elements
  • text-gray-700: sets the text color to gray

Free download of the User Card's source code

The source code of User Card ui component

To create a user card UI component with Tailwind CSS, we will use the following HTML code:

<div class="flex items-center justify-center min-h-screen bg-gray-100">
    <div class="flex flex-col items-center w-full max-w-xs p-4 bg-white rounded-3xl md:flex-row">
        <div class="-mt-28 md:-my-16 md:-ml-32" style="clip-path: url(#roundedPolygon)">
        <img
            class="w-auto h-48"
            src="https://avatars.githubusercontent.com/u/57622665?s=460&u=8f581f4c4acd4c18c33a87b3e6476112325e8b38&v=4"
            alt="Ahmed Kamel"
        />
        </div>
        <div class="flex flex-col space-y-4">
        <div class="flex flex-col items-center md:items-start">
            <h2 class="text-xl font-medium">Ahmed Kamel</h2>
            <p class="text-base font-medium text-gray-400">Full Stack Developer</p>
        </div>
        <div class="flex items-center justify-center space-x-3 md:justify-start">
            <!-- Icons source => https://boxicons.com/ -->
            <a
            href="https://twitter.com/ak_kamona"
            target="_blank"
            class="transition-transform transform hover:scale-125"
            >
            <span class="sr-only">Twitter</span>
            <svg
                aria-hidden="true"
                class="w-8 h-8 text-blue-500"
                fill="currentColor"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 24 24"
            >
                <path
                d="M19.633,7.997c0.013,0.175,0.013,0.349,0.013,0.523c0,5.325-4.053,11.461-11.46,11.461c-2.282,0-4.402-0.661-6.186-1.809 c0.324,0.037,0.636,0.05,0.973,0.05c1.883,0,3.616-0.636,5.001-1.721c-1.771-0.037-3.255-1.197-3.767-2.793 c0.249,0.037,0.499,0.062,0.761,0.062c0.361,0,0.724-0.05,1.061-0.137c-1.847-0.374-3.23-1.995-3.23-3.953v-0.05 c0.537,0.299,1.16,0.486,1.82,0.511C3.534,9.419,2.823,8.184,2.823,6.787c0-0.748,0.199-1.434,0.548-2.032 c1.983,2.443,4.964,4.04,8.306,4.215c-0.062-0.3-0.1-0.611-0.1-0.923c0-2.22,1.796-4.028,4.028-4.028 c1.16,0,2.207,0.486,2.943,1.272c0.91-0.175,1.782-0.512,2.556-0.973c-0.299,0.935-0.936,1.721-1.771,2.22 c0.811-0.088,1.597-0.312,2.319-0.624C21.104,6.712,20.419,7.423,19.633,7.997z"
                ></path>
            </svg>
            </a>
            <a
            href="https://github.com/Kamona-WD"
            target="_blank"
            class="transition-transform transform hover:scale-125"
            >
            <span class="sr-only">Github</span>
            <svg
                aria-hidden="true"
                class="w-8 h-8 text-black"
                fill="currentColor"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 24 24"
            >
                <path
                fill-rule="evenodd"
                clip-rule="evenodd"
                d="M12.026,2c-5.509,0-9.974,4.465-9.974,9.974c0,4.406,2.857,8.145,6.821,9.465 c0.499,0.09,0.679-0.217,0.679-0.481c0-0.237-0.008-0.865-0.011-1.696c-2.775,0.602-3.361-1.338-3.361-1.338 c-0.452-1.152-1.107-1.459-1.107-1.459c-0.905-0.619,0.069-0.605,0.069-0.605c1.002,0.07,1.527,1.028,1.527,1.028 c0.89,1.524,2.336,1.084,2.902,0.829c0.091-0.645,0.351-1.085,0.635-1.334c-2.214-0.251-4.542-1.107-4.542-4.93 c0-1.087,0.389-1.979,1.024-2.675c-0.101-0.253-0.446-1.268,0.099-2.64c0,0,0.837-0.269,2.742,1.021 c0.798-0.221,1.649-0.332,2.496-0.336c0.849,0.004,1.701,0.115,2.496,0.336c1.906-1.291,2.742-1.021,2.742-1.021 c0.545,1.372,0.203,2.387,0.099,2.64c0.64,0.696,1.024,1.587,1.024,2.675c0,3.833-2.33,4.675-4.552,4.922 c0.355,0.308,0.675,0.916,0.675,1.846c0,1.334-0.012,2.41-0.012,2.737c0,0.267,0.178,0.577,0.687,0.479 C19.146,20.115,22,16.379,22,11.974C22,6.465,17.535,2,12.026,2z"
                ></path>
            </svg>
            </a>
        </div>
        </div>
    </div>



    <svg width="0" height="0" xmlns="http://www.w3.org/2000/svg">
        <defs>
        <!-- rounded polygon generator => https://weareoutman.github.io/rounded-polygon/ -->
        <!-- polygon size 190 * 190 almost the same size as the image -->
        <clipPath id="roundedPolygon">
            <path
            d="M79 6.237604307034a32 32 0 0 1 32 0l52.870489570875 30.524791385932a32 32 0 0 1 16 27.712812921102l0 61.049582771864a32 32 0 0 1 -16 27.712812921102l-52.870489570875 30.524791385932a32 32 0 0 1 -32 0l-52.870489570875 -30.524791385932a32 32 0 0 1 -16 -27.712812921102l0 -61.049582771864a32 32 0 0 1 16 -27.712812921102"
            />
        </clipPath>
        </defs>
    </svg>
</div>


<div class="fixed bottom-5 right-5 flex items-center space-x-4">
  <a href="https://twitter.com/ak_kamona" target="_blank" class="transition-transform transform hover:scale-125">
    <span class="sr-only">Twitter</span>
      <svg
         aria-hidden="true"
         class="w-8 h-8 text-blue-500"
         fill="currentColor"
         xmlns="http://www.w3.org/2000/svg"
         viewBox="0 0 24 24"
            >
      <path d="M19.633,7.997c0.013,0.175,0.013,0.349,0.013,0.523c0,5.325-4.053,11.461-11.46,11.461c-2.282,0-4.402-0.661-6.186-1.809 c0.324,0.037,0.636,0.05,0.973,0.05c1.883,0,3.616-0.636,5.001-1.721c-1.771-0.037-3.255-1.197-3.767-2.793 c0.249,0.037,0.499,0.062,0.761,0.062c0.361,0,0.724-0.05,1.061-0.137c-1.847-0.374-3.23-1.995-3.23-3.953v-0.05 c0.537,0.299,1.16,0.486,1.82,0.511C3.534,9.419,2.823,8.184,2.823,6.787c0-0.748,0.199-1.434,0.548-2.032 c1.983,2.443,4.964,4.04,8.306,4.215c-0.062-0.3-0.1-0.611-0.1-0.923c0-2.22,1.796-4.028,4.028-4.028 c1.16,0,2.207,0.486,2.943,1.272c0.91-0.175,1.782-0.512,2.556-0.973c-0.299,0.935-0.936,1.721-1.771,2.22 c0.811-0.088,1.597-0.312,2.319-0.624C21.104,6.712,20.419,7.423,19.633,7.997z"
            ></path>
    </svg>
  </a>
  <a href="https://github.com/Kamona-WD" target="_blank" class="transition-transform transform hover:scale-125">
    <span class="sr-only">Github</span>
      <svg
         aria-hidden="true"
         class="w-8 h-8 text-black"
         fill="currentColor"
         xmlns="http://www.w3.org/2000/svg"
         viewBox="0 0 24 24"
         >
        <path
              fill-rule="evenodd"
              clip-rule="evenodd"
              d="M12.026,2c-5.509,0-9.974,4.465-9.974,9.974c0,4.406,2.857,8.145,6.821,9.465 c0.499,0.09,0.679-0.217,0.679-0.481c0-0.237-0.008-0.865-0.011-1.696c-2.775,0.602-3.361-1.338-3.361-1.338 c-0.452-1.152-1.107-1.459-1.107-1.459c-0.905-0.619,0.069-0.605,0.069-0.605c1.002,0.07,1.527,1.028,1.527,1.028 c0.89,1.524,2.336,1.084,2.902,0.829c0.091-0.645,0.351-1.085,0.635-1.334c-2.214-0.251-4.542-1.107-4.542-4.93 c0-1.087,0.389-1.979,1.024-2.675c-0.101-0.253-0.446-1.268,0.099-2.64c0,0,0.837-0.269,2.742,1.021 c0.798-0.221,1.649-0.332,2.496-0.336c0.849,0.004,1.701,0.115,2.496,0.336c1.906-1.291,2.742-1.021,2.742-1.021 c0.545,1.372,0.203,2.387,0.099,2.64c0.64,0.696,1.024,1.587,1.024,2.675c0,3.833-2.33,4.675-4.552,4.922 c0.355,0.308,0.675,0.916,0.675,1.846c0,1.334-0.012,2.41-0.012,2.737c0,0.267,0.178,0.577,0.687,0.479 C19.146,20.115,22,16.379,22,11.974C22,6.465,17.535,2,12.026,2z"
              ></path>
    </svg>
  </a>
</div>

How to create a User Card with Tailwind CSS?

  1. Create a new HTML file and add the following code:
<div class="bg-white rounded-lg overflow-hidden shadow-md p-6">
  <div class="flex items-center gap-4">
    <img class="w-16 h-16 rounded-full" src="https://via.placeholder.com/150" alt="Profile Picture">
    <div>
      <h2 class="text-lg font-medium text-gray-700">John Doe</h2>
      <p class="text-sm text-gray-500">Web Developer</p>
    </div>
  </div>
</div>
  1. In the code above, we have used the bg-white, rounded-lg, overflow-hidden, shadow-md, and p-6 classes to style the card. We have also used the flex, items-center, and gap-4 classes to center the content vertically and add a gap between the elements.

  2. We have added an image tag with the w-16 and h-16 classes to set the width and height of the profile picture. We have also used the rounded-full class to round the corners of the image.

  3. Inside the div tag, we have added a h2 tag with the text-lg, font-medium, and text-gray-700 classes to set the font size, weight, and color of the name. We have also added a p tag with the text-sm and text-gray-500 classes to set the font size and color of the job title.

  4. Save the file and open it in a web browser. You should see a user card with the name, profile picture, and job title.

Conclusion

In this article, we have learned how to create a user card UI component with Tailwind CSS. We have seen how Tailwind CSS can simplify the styling process and make it easier to create complex layouts and designs. With the pre-defined classes provided by Tailwind CSS, developers can focus on the functionality of the UI component and not worry about the styling.