On this page
How to build a responsive card with CodePen #
1. Open a fresh Pen and name the exercise
Sign in and open a new Pen. The current default layout shows Files, editor groups and a Preview. The standard files are index.html, style.css and script.js, as described in CodePen's view documentation.
Use the header's command search, Search Files, Settings & more..., to find Edit Pen Title. Name the Pen Responsive workshop card. The Omnibar documentation confirms that command. A clear title makes this example easier to find once your account has several practice Pens.
If you have set a custom default template, its files may differ. Use a plain HTML/CSS starting point for this exercise. There is no JavaScript to add, so an unused script file can stay empty.
2. Put the card markup in index.html
Open index.html in Files and replace its contents with this original practice document:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive workshop card</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<main>
<article class="card">
<div class="card-label">CSS practice</div>
<div class="card-content">
<h1>A layout that leaves room for the words</h1>
<p>Change the preview width and watch this card rearrange.</p>
<a href="#details">Read the exercise</a>
</div>
</article>
<section id="details">
<h2>Try a longer heading</h2>
<p>Replace the card heading with your own sentence, then narrow the preview.</p>
</section>
</main>
</body>
</html>
The stylesheet link is what connects the two files. With this markup alone, you should see the heading, paragraph and link in the preview. The card will look plain until the next step. The link has a real destination in the example: the section below the card.
3. Add the small-screen styles to style.css
Open style.css, replace its contents and use these rules:
* { box-sizing: border-box; }
body {
margin: 0;
padding: 1rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
color: #202134;
background: #f5f5f8;
}
main { max-width: 44rem; margin: 2rem auto; }
.card {
display: grid;
grid-template-columns: minmax(0, 1fr);
border: 1px solid #c8c8d5;
border-radius: 0.75rem;
overflow: hidden;
background: white;
}
.card-label {
padding: 2rem;
color: white;
background: #4935c7;
font-size: 1.25rem;
font-weight: 700;
}
.card-content { min-width: 0; padding: 1.5rem; }
h1 { margin: 0 0 1rem; font-size: 1.7rem; line-height: 1.2; }
p { overflow-wrap: anywhere; }
.card-content a { color: #3522a4; font-weight: 600; }
a:focus-visible { outline: 3px solid #202134; outline-offset: 4px; }
#details { margin-top: 2rem; scroll-margin-top: 1rem; }
CodePen builds after a pause in typing. You should see a violet label area above the white content area, with space around the card. This is the narrow layout, so it doesn't rely on squeezing two columns into a phone-sized preview.
The min-width: 0 declaration allows the grid's content item to shrink. MDN explains the automatic minimum width of grid and flex items. The wrapping rule also gives a long URL or uninterrupted word somewhere to break.
4. Add the wider layout with a media query
Add this block at the bottom of style.css:
@media (min-width: 40rem) {
.card { grid-template-columns: 12rem minmax(0, 1fr); }
.card-label { display: grid; align-content: center; }
.card-content { padding: 2rem; }
}
The card now has two columns when the preview viewport is at least 40rem wide. Below that width, the earlier one-column rule still applies. This is a viewport media query, so the preview's width controls the change; the width of your code panel doesn't directly set the card's columns.
MDN's media-query guide explains this width condition. The breakpoint is an example choice for this card. If your own wording needs more space, move it higher instead of reducing the text until it fits.
5. Resize the preview and check the real content
Drag the divider between the editors and Preview to give the preview more or less width. You should see the label move above the text when the viewport narrows, then return to the left when there is enough room.
Replace the heading with a longer sentence and the paragraph with a few lines of your own text. Check that the card grows vertically and the link remains visible. A short placeholder can hide a fixed-height problem, so avoid adding a height simply to make the card match one screenshot.
Move focus through the example with the keyboard. The Read the exercise link should have a visible outline, and activating it should move to the section below. If the preview is stuck on an older result, use Refresh Preview and inspect Blocks for build errors. CodePen's build documentation explains that a failed build can leave the last successful result visible.
6. Save the Pen and copy its link
Use Save, or Control-S on a PC and Command-S on a Mac. CodePen's current Pen documentation confirms both shortcuts. After the first save, the default auto-save setting saves further changes periodically.
Open Share to get the saved Pen's sharing URL. Open that URL separately and check that it shows the intended files and result. Saving an editor example is separate from deploying a standalone website. For a practice card, the saved Pen link is enough.
Free saved Pens are public. The CodePen vs JSFiddle comparison explains the licensing and privacy controls before you replace this original sample with client material.
Two reported problems to recognize #
A converted Pen still links a stylesheet you aren't using
In an August 1, 2026 discussion, a user found that a converted Pen had added a style.css link while their CSS lived elsewhere. A reply explained that an unused file and its link can be removed together.
For this exercise, keep style.css and the matching <link> in index.html. Put all the sample CSS in that file. If you move the styles into a <style> element instead, remove the unused file reference as well. Check the file path before rewriting the card rules.
The preview disappears with a crash-prevention warning
The same thread reports an intermittent warning that removed the preview to prevent a browser crash. Moving the CSS didn't resolve that warning. A CodePen team member asked the reader to contact support for further debugging.
Save your work first. If your own JavaScript caused a loop, the official support page documents adding ?turn_off_js=true to the Pen URL so you can fix the script. This card has no JavaScript; if the warning keeps returning here, send support the Pen link and browser details.
Troubleshooting the card #
| Problem | Likely cause | Fix |
|---|---|---|
| Text appears with no card styling | CSS file isn't linked, or the filename differs | Match ./style.css to the actual file and keep the link inside head |
| Card stays stacked on a wide screen | Preview viewport is still under 40rem | Widen Preview; the query uses its viewport width |
| Long text stretches the grid | Content item keeps an automatic minimum width | Keep min-width: 0 and the minmax(0, 1fr) track |
| Preview shows an earlier design | Failed build or automatic refresh is paused | Inspect Blocks, fix the error and use Refresh Preview |
| Image upload asks for an upgrade | Media uploads require a paid plan | Keep this text-only example or use an authorized external image URL |
Three ways to adapt the exercise #
Add an image you have permission to use
Replace the label area with an image and write alt text that explains its content. Use a reachable HTTPS image URL if you stay on the free plan. Keep its dimensions flexible so it doesn't force the grid wider than the preview. The CodePen files documentation describes paid media uploads and file paths.
Turn the card into a repeated list
Duplicate the article markup inside a surrounding grid, then decide how many cards the content can support at each width. Keep each card's heading readable. A row of cards and this card's internal two-column layout are separate decisions, so change one at a time.
Move the files into a regular website
Copy the HTML and CSS into your own project, then adjust the stylesheet path and heading level for the page. An existing page may already have an H1. Examine inherited styles after moving it; the playground's short stylesheet has fewer competing rules than a full site.
Check the finished example #
- The HTML links the stylesheet that contains the rules.
- The narrow card stacks without sideways overflow.
- The wider card uses two columns.
- Long wording expands the card without hiding the link.
- Keyboard focus is visible and the example link reaches its section.
- The saved sharing link opens the intended Pen.
Continue with another editor or exercise #
The free CSS playground shortlist compares tools for local practice and framework work. JSFiddle is another home for a small reproduction, Web Maker covers offline work, and W3Schools offers lesson examples to alter.
Featured illustration: original conceptual artwork created for WebIndexly with OpenAI image generation.




