Skip to content
Tutorials Center
My learning

Topics

Lesson 125 of 139 ยท Projects

Project: Profile Card

Build a polished profile card with CSS in seven stages. Practice the box model, rounded corners, shadows, typography, flexbox and smooth hover effects.

Profile cards are everywhere: team pages, author boxes, social apps, conference sites. They are small enough to finish in one sitting, yet they use a surprising amount of CSS. Spacing, borders, shadows, type, a little layout and some carefully tuned interaction all have to work together before a card looks "finished".

In this project you will build one from plain HTML to a polished component in seven stages. Every stage has its own editor with the complete code so far, so you can jump in anywhere, break things and compare. Each stage adds one idea and links back to the lesson that explains it in depth.

Our example person is Noor Haddad, a fictional product designer. Swap in your own name, role and numbers as you go.

Stage 1: The markup and a centered stage

Good CSS starts with good HTML. The card is an <article>, because it is a self-contained piece of content that would make sense on its own. Inside it: a decorative banner, a photo, a heading, two paragraphs, a list of stats and two links. The class names follow a simple block__element pattern (see Organizing CSS), so every rule you write later targets exactly one part.

The page styles do three jobs. The universal box-sizing: border-box rule makes widths include padding and borders (box-sizing). display: grid with place-items: center puts the card in the middle of the page (Centering Anything). And a soft gray background gives the white card something to stand out against.

Stage 1: markup and page

It already works. It just looks like a document, not a component. That is exactly the point: the content is readable before a single style touches it.

Stage 2: Turning a box into a card

A card is a box with a clear edge. Give it a white background, a maximum width, rounded corners and a shadow, and it suddenly floats above the page.

Four details matter here:

  • width: 100% plus max-width: 22rem. The card fills narrow screens but stops growing on wide ones. See Width and Height.
  • border-radius: 1.25rem. Generous corners feel friendly. See Rounded Corners.
  • overflow: hidden. The banner you add next is a rectangle. Hiding the overflow clips it to the card's rounded corners. See Overflow.
  • Two shadows instead of one. A tight, faint shadow defines the edge; a large, soft one adds depth. Layering them looks far more natural than a single dark blur. See Box Shadows.
Stage 2: the card box

Try removing overflow: hidden after the next stage and watch the banner's square corners poke out. The negative spread (-8px) in the second shadow pulls the blur in from the sides, so the shadow mostly appears below the card, as if light came from above.

Stage 3: Banner and overlapping avatar

The signature look of a profile card is the round photo that overlaps a colored banner. It takes surprisingly little CSS.

The banner is an empty <div> with a fixed height and a gradient background. The avatar becomes a block, gets a fixed size, and is centered with margin-inline: auto (Margins). Then comes the trick: a negative top margin equal to half the avatar's height pulls it up so that it straddles the banner's bottom edge.

border-radius: 50% turns the square image into a circle, and a thick white border separates it from the banner. object-fit: cover (Sizing Images) is a safety net: if someone uploads a photo that isn't square, it is cropped to fill the circle instead of being squashed.

Stage 3: banner and avatar

The avatar paints on top of the banner without any z-index, because it comes later in the source and neither element is positioned. If you ever add position: relative to the banner, you may need z-index to restore the order.

Change -3.5rem to -1rem and then to -5rem to see how the overlap changes. Keeping the value at exactly half the avatar's height makes the banner edge cut through the middle of the circle.

Stage 4: Typography

Text makes or breaks a card. Three pieces of text each need a different voice:

  • The name is the largest and boldest, with tight line-height and a hint of negative letter-spacing, which makes big type look tidier.
  • The role is small, uppercase and spaced out, a classic "eyebrow" style. Letter spacing is essential for uppercase text, which otherwise looks cramped. See Styling Text.
  • The bio is body text: a calm gray, a comfortable line-height of 1.6 (Line Height) and text-wrap: pretty, which asks the browser to avoid leaving a single word alone on the last line (Wrapping, Overflow and Hyphens).

Notice that the margins are set explicitly on every text element. Browsers give headings and paragraphs default margins; replacing them with your own values means the spacing is intentional, not accidental.

Stage 4: typography

text-wrap: pretty is a progressive enhancement: browsers that don't support it simply wrap text the normal way, so it is safe to use today.

Stage 5: The stats row

Three numbers side by side is a job for Flexbox. The list becomes a flex container, and each item gets flex: 1, so all three share the width equally no matter how long their labels are (Flex Item Properties).

Each item is itself a flex container with flex-direction: column, which stacks the number above its label. The thin dividers between items come from one neat selector: li + li matches every item that follows another item, so the first one gets no left border (Combinators). Using border-inline-start instead of border-left means the divider flips automatically in right-to-left languages (Logical Properties).

Stage 5: stats row

font-variant-numeric: tabular-nums gives every digit the same width, so numbers line up neatly and don't jiggle if they change. It is a small detail that dashboards and price tables use all the time.

list-style: none removes the bullets, and in Safari it can also stop VoiceOver from announcing the element as a list. If the "list-ness" matters to your users, add role="list" to the <ul>. Styling Lists explains the details.

Stage 6: Buttons with real states

Links styled as buttons need to respond to the person using them. A good button has at least four looks: resting, hovered, pressed and focused from the keyboard (Styling Buttons and Links).

  • The shared .button class sets the shape: pill corners (border-radius: 999px), padding, a 2px border and bold text. flex: 1 inside the flex row makes both buttons the same width.
  • .button--primary is a modifier: it only changes the colors. The secondary button keeps the outlined look.
  • :hover darkens or tints the background, :active nudges the button down by one pixel so it feels pressed, and :focus-visible draws a clear amber ring for keyboard users only (Pseudo-classes and Outlines).
  • transition smooths the color change over 0.2 seconds (Transitions).
Stage 6: buttons

Click inside the preview and press Tab to see the focus ring move between the buttons. Then click one with the mouse: no ring appears, because :focus-visible only shows it when the browser decides a visible indicator is needed.

Why does .button--primary:hover win over .button:hover? Both selectors have the same specificity, one class plus one pseudo-class, so the one that comes later in the stylesheet wins. Keep modifier rules after the base rules and the cascade works for you.

Stage 7: Custom properties, a hover lift and reduced motion

The card is nearly done. The last stage does three things that turn a good component into a reusable one.

1. Design tokens. The teal color appears in five places. Moving it, and the other repeated values, into custom properties on the .card means you can re-theme the whole card by changing one line. Because custom properties inherit, every element inside the card can read them.

2. A gentle lift. On hover, the card rises 4 pixels and its shadow grows, as if it were moving closer to you. Adding :focus-within gives keyboard users the same feedback when they tab to a button inside the card (2D Transforms).

3. Respect for motion preferences. Some people get dizzy or distracted by movement and turn on "reduce motion" in their operating system. The prefers-reduced-motion media query switches the movement off for them while keeping the color changes (Motion and Performance).

Stage 7: the finished profile card

Now try a re-theme: change --accent to #7c3aed, --accent-dark to #5b21b6 and --accent-soft to #f5f3ff. The banner, role, buttons and hover states all turn purple at once. The middle gradient stop is still a hard-coded teal; replacing it with a custom property of its own is a good small exercise.

The hover lift animates only transform and box-shadow. Transforms are cheap for the browser to animate because they don't change the layout of anything around the card.

Ideas to take it further

You have a complete component. If you want to keep practicing, here are some extensions, each tied to a lesson:

  • Put three cards side by side with a responsive grid: grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)) (Grid Tracks).
  • Add a dark version by redefining the custom properties inside @media (prefers-color-scheme: dark) (Dark Mode).
  • Add a small "Available for work" badge pinned to the avatar with position: absolute (Positioning).
  • Replace the gradient banner with /samples/sunset.svg and background-size: cover (Backgrounds).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does the card need overflow: hidden?

    border-radius rounds the card's own background, but child content can still paint outside the curve. overflow: hidden clips the banner so the corners stay round.
  2. 2. How does the avatar overlap the banner?

    margin-top: -3.5rem pulls the 7rem avatar halfway up over the banner. It paints on top because it comes later in the source and neither element is positioned.
  3. 3. Which selector adds a divider before every stat except the first?

    The next-sibling combinator matches an item only when another item comes right before it, so the first item is skipped.
  4. 4. What does :focus-visible give you that :focus doesn't?

    The browser decides when focus should be visible. Keyboard users get the ring; mouse clicks on buttons usually don't show it.
  5. 5. Why define the colors as custom properties on .card?

    Custom properties inherit down the tree, so every rule inside the card reads the same tokens. One change updates the banner, text, buttons and hover states.

Summary

  • Start from semantic HTML with clear class names; it should be readable before any CSS.
  • A card is a box with max-width, border-radius, overflow: hidden and layered shadows.
  • A negative margin plus border-radius: 50% and object-fit: cover creates the overlapping round avatar.
  • Give each text role its own size, weight, spacing and color, and set margins explicitly.
  • Flexbox with flex: 1 shares space evenly; li + li adds dividers between items.
  • Style every button state: :hover, :active and :focus-visible, with short transitions.
  • Custom properties make the component easy to re-theme, and prefers-reduced-motion keeps the hover lift comfortable for everyone.

Next: Project: Responsive Navigation Bar, where you build a site header that turns into a mobile menu on small screens.