Course Content
Tailwind CSS for Web Development
Tailwind CSS for Web Development
Cards
Cards are versatile UI components that can display a variety of content such as images, text, and interactive elements like buttons. Tailwind CSS provides a range of utilities that make it easy to create and style card components.
Card Structure
A basic card component typically includes the following elements: an image, a title, descriptive text, and action buttons.
We already know the separate elements of this component, so let's combine all our knowledge into one complete card.
index
index
index
Explanation
max-w-sm
: Sets the maximum width of the element to small size (24rem or 384px);rounded
: Applies small rounded corners to the element;overflow-hidden
: Ensures any content that overflows the bounds of the element is hidden;shadow-lg
: Applies a large box shadow to the element.
w-full
: Sets the width of the image to 100% of its container;px-6
: Adds horizontal padding of 1.5rem (24px) on both left and right;py-4
: Adds vertical padding of 1rem (16px) on both top and bottom;font-bold
: Applies bold font weight;text-xl
: Sets the font size toxl
(1.25rem or 20px);mb-2
: Adds a bottom margin of 0.5rem (8px);text-gray-700
: Sets the text color to a medium gray;text-base
: Sets the font size to base (1rem or 16px).
px-6
: Adds horizontal padding of 1.5rem (24px) on both left and right;pt-4
: Adds top padding of 1rem (16px);pb-2
: Adds bottom padding of 0.5rem (8px);bg-blue-500
: Sets the background color to a shade of blue;hover:bg-blue-700
: Changes the background color to a darker shade of blue on hover;text-white
: Sets the text color to white;font-bold
: Applies bold font weight;py-2
: Adds vertical padding of 0.5rem (8px);px-4
: Adds horizontal padding of 1rem (16px);rounded
: Applies small rounded corners to the button;bg-gray-500
: Sets the background color to a shade of gray;hover:bg-gray-700
: Changes the background color to a darker shade of gray on hover;text-white
: Sets the text color to white;font-bold
: Applies bold font weight;py-2
: Adds vertical padding of 0.5rem (8px);px-4
: Adds horizontal padding of 1rem (16px);rounded
: Applies small rounded corners to the button;ml-2
: Adds a left margin of 0.5rem (8px).
As you can see, all the utility classes have already been considered. Now, we combine everything in one card component.
Let's add one more section to the previous example, which will be badges.
index
index
index
Explanation - Badges
px-6
: Adds horizontal padding of 1.5rem (24px) on both left and right;pt-4
: Adds top padding of 1rem (16px);pb-2
: Adds bottom padding of 0.5rem (8px);inline-block
: Displays the element as an inline-level block container;bg-gray-200
: Sets the background color to a light gray;rounded-full
: Applies fully rounded corners to the element;px-3
: Adds horizontal padding of 0.75rem (12px) on both left and right;py-1
: Adds vertical padding of 0.25rem (4px) on both top and bottom;text-sm
: Sets the font size to small (0.875rem or 14px);font-semibold
: Applies a semibold font weight;text-gray-700
: Sets the text color to a medium gray;mr-2
: Adds a right margin of 0.5rem (8px);mb-2
: Adds a bottom margin of 0.5rem (8px).
Note
All in-depth information about the Card component can be found here.
Thanks for your feedback!