Back to Blog
Lesson 40 of the Tailwind CSS: Utility-First Styling from Scratch course
CSSAugust 27, 20264 min read

Building a Card Component: UI Design with Tailwind CSS

Learn how to build a professional, responsive card component from scratch using Tailwind CSS. Master layout, shadows, borders, and spacing utilities.

Tailwind CSSUI DesignComponentsResponsive Web DesignWeb Development
HTML code displayed on a screen, demonstrating web structure and syntax.

Previously in this course, we explored Managing Z-Index and Positioning to handle complex layout overlaps. Now, we're taking those foundational skills to build a functional, reusable card component—a staple of any modern marketing site.

When you think of UI design and cards, don't just see them as "boxes." Think of them as containers that establish visual hierarchy. They group related information, provide clear boundaries, and keep your layout organized.

Structuring the Card HTML

A well-structured card is semantic. We typically use an <article> or <div> as our main wrapper. Inside, we need a clear hierarchy: a visual area (like an image or icon), a content area (heading and body text), and a footer (often for buttons or links).

Here is the fundamental structure:

HTML
style="color:#808080"><style="color:#4EC9B0">article class="bg-white rounded-lg shadow-md overflow-hidden">
  style="color:#808080"><style="color:#4EC9B0">div class="h-48 bg-gray-200">style="color:#808080"></style="color:#4EC9B0">div> <!-- Placeholder for image -->
  style="color:#808080"><style="color:#4EC9B0">div class="p-6">
    style="color:#808080"><style="color:#4EC9B0">h3 class="text-xl font-bold text-gray-900">Card Titlestyle="color:#808080"></style="color:#4EC9B0">h3>
    style="color:#808080"><style="color:#4EC9B0">p class="mt-2 text-gray-600">This is where your card description goes.style="color:#808080"></style="color:#4EC9B0">p>
    style="color:#808080"><style="color:#4EC9B0">div class="mt-4">
      style="color:#808080"><style="color:#4EC9B0">button class="bg-blue-600 text-white px-4 py-2 rounded">Actionstyle="color:#808080"></style="color:#4EC9B0">button>
    style="color:#808080"></style="color:#4EC9B0">div>
  style="color:#808080"></style="color:#4EC9B0">div>
style="color:#808080"></style="color:#4EC9B0">article>

Applying Layout, Shadow, and Border Utilities

To make this component feel "polished," we rely on three key pillars:

  1. Layout: We use overflow-hidden on the parent to ensure rounded corners aren't clipped by child images. We use flex or grid inside the card if we need complex side-by-side arrangements.
  2. Shadows: Shadows create depth. A subtle shadow-md or shadow-lg helps the card "lift" off the page, signaling to the user that it’s an interactive element.
  3. Borders: Sometimes a shadow isn't enough. Using border border-gray-100 adds a crisp edge, especially useful if your card is placed on a light gray background.
Utility TypeClass ExamplePurpose
Shadowshadow-mdAdds elevation/depth
Borderborder border-gray-200Defines boundaries
Roundingrounded-xlSoftens the aesthetic
Overflowoverflow-hiddenClips child elements to radius

Making the Card Responsive

In Advanced Responsive Patterns, we learned that components should adapt to their container. For a card, responsiveness usually means adjusting the padding or the layout of the internal content.

For example, on mobile, you might want a simpler, vertical layout. On larger screens, you might want to switch to a horizontal layout (where the image sits to the left of the text).

HTML
style="color:#808080"><style="color:#4EC9B0">article class="bg-white rounded-xl shadow-lg border border-gray-100 md:flex">
  style="color:#808080"><style="color:#4EC9B0">div class="md:w-1/3 bg-gray-200">style="color:#808080"></style="color:#4EC9B0">div> <!-- Image section -->
  style="color:#808080"><style="color:#4EC9B0">div class="p-6 md:w-2/3">
    style="color:#808080"><style="color:#4EC9B0">h3 class="text-xl font-bold">Responsive Titlestyle="color:#808080"></style="color:#4EC9B0">h3>
    style="color:#808080"><style="color:#4EC9B0">p class="mt-2">This card shifts layout at the md breakpoint.style="color:#808080"></style="color:#4EC9B0">p>
  style="color:#808080"></style="color:#4EC9B0">div>
style="color:#808080"></style="color:#4EC9B0">article>

Hands-on Exercise

  1. Create a card.html file.
  2. Build a card that contains an image placeholder, a heading, a paragraph, and a button.
  3. Apply rounded-2xl to the container and shadow-xl for a "premium" feel.
  4. Add a hover:shadow-2xl class to the container to make it interactive when the user hovers over it.

Common Pitfalls

  • Forgetting overflow-hidden: If you add an image inside a rounded container and don't use overflow-hidden, the image corners will spill over the rounded edges of the card.
  • Inconsistent Padding: Always use the same spacing scale (e.g., p-6) for your internal content. Mixing p-4 and p-8 inside the same card design leads to a messy, unprofessional look.
  • Over-nesting: Try to keep your HTML structure flat. Deeply nested divs make it harder to manage the layout with flexbox or grid.

FAQ

Q: Should I use div or article for cards? A: If the card represents an independent, self-contained piece of content (like a blog post preview or a feature highlight), use <article>. It's better for SEO and accessibility.

Q: How do I handle images that don't fit the aspect ratio? A: Use the object-cover utility on your <img> tag combined with a fixed height on the container to ensure the image fills the space without stretching.

Q: When should I switch from a standard card to a component? A: If you find yourself copying and pasting the same 10-15 classes across your project, it’s time to move toward a component-based approach using @apply or a framework-specific component, as discussed in Component Library Design.

Recap

We've moved from basic utility application to crafting a cohesive card component. By combining layout, depth (shadows), and responsive modifiers, you've created a UI element that is both functional and visually balanced. Use these building blocks to ensure your marketing site remains consistent and professional.

Up next: Building a Testimonial Section where we will group these cards into a repeating, balanced layout.

Similar Posts