Skip to content
Tutorials Center
My learning

Topics

Lesson 137 of 139 ยท Practice and Review

CSS Recipes Gallery

Twenty small, copy-ready CSS recipes with live editors, from toggle switches, tooltips and flip cards to timelines, segmented controls and skeleton loaders.

Most interfaces are built from the same small pieces: a switch, a badge, a tooltip, a striped table, a loading placeholder. This page collects twenty of them. Each recipe is short, uses modern CSS, works without a framework, and comes with a live editor so you can change it and see what happens.

Treat the recipes as starting points, not finished products. Every one explains the one or two ideas that make it work and links to the lesson that covers those ideas in depth. If you can explain why each recipe works, you have mastered most of the course. A good exercise: read the description, try to build the component yourself, and only then compare with the code.

The recipes keep accessibility in mind: real form controls instead of clickable <div> elements, visible focus styles, and animations that calm down when the visitor asks for reduced motion.

Controls

1. Toggle switch

A switch is a checkbox in disguise. appearance: none removes the browser's own checkbox drawing, so the <input> becomes an empty box you can style like any element. The track is the input itself, and the knob is its ::before pseudo-element. When the box is :checked, the track turns green and the knob slides with translate. Because it's still a real checkbox, it works with the keyboard, submits with forms and reads as a switch to screen readers thanks to role="switch" (User Interface Properties, Styling Forms).

Recipe 1: toggle switch

The knob moves exactly 1.25rem: the track width minus its height. Change the track size and you only need to update that one number.

2. Segmented control

A segmented control is a group of radio buttons that look like one pill-shaped button bar. The radio inputs stay in the page but become invisible with opacity: 0 and position: absolute, so keyboard and screen reader users still get real radio buttons. The highlight comes from label:has(:checked): the label that contains the checked radio gets a white background and a shadow. label:has(:focus-visible) gives the focused option a ring (:is(), :where(), :not() and :has()).

Recipe 2: segmented control

Click an option, or press Tab to reach the group and use the arrow keys. The keyboard behavior is free, because these are real radio buttons.

3. Breadcrumbs

Breadcrumbs are an ordered list inside a <nav>. The separators are not in the HTML; the rule li + li::before adds one before every item except the first (Combinators). The second content line uses alternative text: "/" / "" shows a slash but gives it empty alt text, so screen readers don't announce "slash" between the links. Browsers that don't understand that syntax ignore the line and use the first one. The current page is plain text marked with aria-current="page".

Recipe 3: breadcrumbs

4. Badges

A count badge sits on the corner of an icon button. The button is position: relative, and the badge is absolutely positioned against it with small negative offsets (Positioning). min-width plus padding makes the badge a circle for one digit and a pill for "99+". An empty badge becomes a plain dot through :empty. The white border separates the badge from the icon, so it reads well on any background. The real information is in the button's aria-label; the visible number is hidden from screen readers to avoid reading it twice.

Recipe 4: badges

5. Tooltip

The tooltip is a real element, not generated content, so assistive technology can find it: the button points to it with aria-describedby, and screen readers read it as the button's description. It is hidden with opacity: 0 and visibility: hidden and shown on hover or keyboard focus. Centering above the button uses left: 50% plus translate: -50% 0, and the little arrow is a rotated square made with ::after (Tooltips).

Recipe 5: tooltip

Hover over the button, or press Tab to focus it. For tooltips that must stay inside the screen or follow their anchor, see Anchor Positioning.

Cards and effects

6. Centered card

The shortest way to center anything in both directions is a grid container with place-items: center (Centering Anything). The page gets min-height: 100dvh, so there is height to center within. The card's width uses min(100%, 22rem): 22rem when there is room, the full width on a tiny screen.

Recipe 6: centered card

7. Flip card

A flip card has two faces stacked in the same spot, and the inner wrapper rotates 180 degrees around the vertical axis. Three properties make the 3D effect work (3D Transforms):

  • perspective on the outer card gives depth, so the rotation looks like a real turn rather than a squash.
  • transform-style: preserve-3d on the wrapper keeps the faces in the same 3D space.
  • backface-visibility: hidden hides each face while it points away from you. The back face starts already rotated, so it faces you when the wrapper is turned.

The card flips on hover and on keyboard focus, thanks to tabindex="0" and :focus-visible. Motion-sensitive visitors get an instant flip.

Recipe 7: flip card

Content that only appears on hover is easy to miss on touch screens. Use flip cards for playful extras, such as flashcards, not for information people need.

8. Corner ribbon

A ribbon is a band rotated 45 degrees across a card's corner. The card is position: relative with overflow: hidden, so the ends of the band are cut off cleanly at the card's edges. The band is wider than it looks; only the middle part shows. Adjust top and right together to slide it along the corner.

Recipe 8: corner ribbon

9. Gradient border

CSS has no "gradient border color", but two background layers fake one perfectly. The border is transparent, so the backgrounds show through it. The first layer is a solid color clipped to the padding-box, which covers everything inside the border. The second layer is the gradient, painted under the whole border-box, so only the border ring shows it (Multiple Backgrounds and background-clip). Unlike border-image, this keeps the rounded corners.

The second card animates the gradient. A conic gradient starts at the angle --angle, and @property registers that custom property as an <angle>, which lets the browser animate it smoothly from 0deg to 360deg (Custom Properties).

Recipe 9: gradient border

10. Glass card

The "frosted glass" look comes from backdrop-filter, which blurs whatever is behind an element (Filters). The card itself is a nearly transparent white with a faint white border, and the colorful shapes behind it turn into soft blurs. The -webkit- prefixed line is for Safari versions before 18. If a browser doesn't support backdrop filters at all, the @supports not block gives the card a solid, readable background instead (Feature Queries).

Recipe 10: glass card

Glass effects can hurt readability when the background is busy. Keep the text large enough, and check the contrast over the brightest part of the background.

11. Cutout text

Gradient or image-filled text uses background-clip: text: the background is painted only where the text is, and color: transparent lets it show through. The prefixed -webkit-background-clip line is still useful for older browsers. Clipped text stays real, selectable text, so screen readers and search engines read it normally (Multiple Backgrounds and background-clip).

Recipe 11: cutout text

Lists, tables and media

12. Striped table

Zebra stripes help the eye follow a row across a wide table. tbody tr:nth-child(even) picks every second body row (Structural Pseudo-classes), and a hover color highlights the row under the pointer. Numbers are right-aligned and use font-variant-numeric: tabular-nums, which gives every digit the same width, so the columns of numbers line up (Styling Tables).

Recipe 12: striped table

13. Timeline

A vertical timeline is an ordered list with a line down its left edge and a dot on each item. The line is the list's own border-inline-start. Each dot is an item's ::before, absolutely positioned so that its center sits on the line. A <time> element with a datetime attribute gives each date a machine-readable form (Styling Lists).

Recipe 13: timeline

The dot's left moves it back across the list's padding, then by half the dot's full width (12px plus a 2px border on each side makes 16px, so 8px) and half the 2px line (1px): calc(-1.5rem - 9px). Change the padding and update the calc() with it.

14. Steps progress

A checkout progress bar shows where you are in a series of steps. Each step is a list item that shares the row equally with flex: 1. The numbers come from a CSS counter, shown in a circle drawn by ::before. The connecting line is each item's ::after, stretching from the previous step's center to its own. Completed steps (.done) swap the number for a check mark and color the line leading into the next step. The current step is marked with aria-current="step", which screen readers announce.

Recipe 14: steps progress

Move class="done" and aria-current="step" along the list to see every state update with no extra CSS.

15. Avatar group

Overlapping avatars save space in a team list or a comment thread. Every avatar after the first gets a negative inline margin, so it slides over its neighbor. A border in the page's background color draws a ring that separates the circles. On hover, an avatar moves up and comes to the front with z-index. The last item is a "+3" counter in the same shape. Each avatar's color comes from a --c custom property in its style attribute.

Recipe 15: avatar group

16. Responsive video

aspect-ratio: 16 / 9 reserves the right height for a video before it loads, so the page doesn't jump, and width: 100% makes it fill its container at any size (Sizing Images: object-fit and aspect-ratio). object-fit: contain keeps the whole frame visible if the file has a different shape, with the black background filling any gap. The same two lines work for an embedded <iframe> player. Older tutorials use a "padding-top: 56.25%" wrapper hack for this; you don't need it anymore.

Recipe 16: responsive video

Text and loading states

17. Text truncation

Two techniques cover almost every case (Wrapping, Overflow and Hyphens):

  • One line with an ellipsis needs three properties together: white-space: nowrap keeps the text on one line, overflow: hidden cuts it off and text-overflow: ellipsis draws the "โ€ฆ". Leave one out and it doesn't work.
  • Several lines use line clamping. The -webkit- prefixed version, with display: -webkit-box and -webkit-box-orient: vertical, is odd-looking but works in every current browser.

The demo box has resize: horizontal, so you can drag its bottom-right corner and watch both truncations adapt. Remember that truncated text is still in the page: make sure people can reach the full text somewhere, such as on the article's own page.

Recipe 17: truncation

18. Loaders

A spinner is a circle with a light border and one colored side, rotating forever. rotate: 1turn in the keyframes is one full rotation. A three-dot loader reuses one animation with different animation-delay values, so the dots pulse one after another (Spinners, Loaders and Skeletons). Each loader has role="status" and hidden text, so screen readers announce "Loading". With reduced motion, the spinner turns slowly and the dots stop pulsing.

Recipe 18: loaders

The hidden text in the dot loader is a <b>, not a <span>, so the .dots span rule doesn't turn it into a fourth dot.

19. Skeleton screen

A skeleton shows gray shapes where content will appear, so the page feels faster and doesn't jump when the data arrives. The shimmer is a gradient twice as wide as the shape (200% 100%), with light in the middle and the base color at both ends. The animation moves the background by -200%, which for a background twice the element's width is exactly one tile, so the repeating background loops without a visible jump. aria-busy="true" on the card tells assistive technology that the content is still loading.

Recipe 19: skeleton

Page layout

On a page with little content, the footer should sit at the bottom of the window, not halfway up. A three-row grid on body solves it: grid-template-rows: auto 1fr auto gives the header and footer their natural height and hands all the remaining space to <main>, while min-height: 100dvh makes the body at least as tall as the viewport (Viewport Units). When the content grows, the page simply gets longer and the footer follows it. No fixed heights or positioning are involved.

Recipe 20: sticky footer

Copy the paragraph a dozen times and run again: the footer is pushed down below the content, exactly as it should be.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In the toggle switch, what does appearance: none do?

    The input keeps all its behavior (keyboard, forms, accessibility). Only the built-in visual is removed, which lets ::before draw the knob.
  2. 2. How does the gradient border recipe keep rounded corners?

    border-image ignores border-radius. Two background layers with different clip boxes give a gradient ring that follows the rounded corners.
  3. 3. Which set of properties is needed for a one-line ellipsis?

    All three are required. Without nowrap the text wraps instead of overflowing, and without overflow: hidden there is nothing for the ellipsis to replace.
  4. 4. Why does the segmented control hide its radio buttons with opacity: 0 instead of display: none?

    The radio buttons must stay focusable so that keyboard and screen reader users can choose an option. Opacity hides them visually without removing them.
  5. 5. In the sticky footer recipe, which part makes <main> take all the free space?

    The auto rows fit the header and footer, and the 1fr row takes everything left over, which pushes the footer to the bottom.

Summary

  • Style real form controls: appearance: none turns checkboxes into switches, and visually hidden radio buttons plus :has(:checked) make segmented controls.
  • Generated content (::before, ::after) draws separators, knobs, arrows, dots and connecting lines without extra HTML; alt text like content: "/" / "" keeps it quiet for screen readers.
  • position: relative on a parent plus position: absolute on a child places badges, tooltips and ribbons.
  • Background tricks go a long way: clipped layers for gradient borders, background-clip: text for cutout text, a moving 200% gradient for skeletons.
  • aspect-ratio, place-items: center and grid-template-rows: auto 1fr auto solve video sizing, centering and sticky footers in a line or two.
  • Respect prefers-reduced-motion, keep focus visible and give loaders role="status".

Next: CSS Practice Exercises, where you solve graded tasks across the whole course with hidden solutions.