![]()
Breakpoint-heavy card grids often need another round of adjustments whenever the content changes, the parent container gets narrower, or a new screen size exposes an awkward gap. A CSS Grid Layout built with intrinsic sizing solves that maintenance problem by fitting as many usable columns as the available space allows, without media queries. This guide connects accessible WordPress card markup to a flexible grid, so the layout responds to its container rather than a list of device breakpoints.
You will create a reusable post-card loop, style its cards and tracks, enqueue the stylesheet in your theme, and test the result with real content. The approach suits an archive or other collection of posts, but you can adapt the fields and minimum card width to match your design. Before editing, prepare the following:
- A WordPress development or staging site where you can edit theme files.
- A child theme or custom theme, so your changes survive theme updates.
- Basic familiarity with PHP template files, HTML, and CSS.
- A card content source, such as posts with featured images, titles, excerpts, and links.
- A current browser with developer tools for checking Grid behavior at different container widths.
- A recent site backup or version-controlled copy of the theme.
Plan the card structure and sizing behavior
Start by deciding what one card must contain and how narrow it can become before the content feels cramped. For a typical post archive, plan for an optional featured image, a linked post title, a short excerpt, and a clear reading link. Keep the card markup separate from the grid wrapper: the wrapper creates the columns, while each article supplies the content. This distinction makes the structure easier to reuse in an archive template or a dedicated template part.
Choose a practical minimum track width based on your theme typography and content; 17rem is a useful starting point, not a universal rule. At that size, titles and excerpts usually have room to wrap without making cards excessively wide. The grid will respond to the width its parent actually provides, so a wide content area may fit several cards while a narrow sidebar may fit only one—even when both appear in the same viewport. The number of columns should emerge from that available space, rather than from named device sizes or manually chosen breakpoints. In the next step, build the WordPress markup around this content contract, then let the CSS Grid Layout handle the changing column count.
Create accessible card markup for WordPress content
Place the grid wrapper around the cards, and give each post its own semantic <article>. This archive-template example uses the main WordPress query, includes a featured image only when one exists, and escapes output for its context. You can adapt the fields or move the markup into a reusable template part.
<div class="pnet-card-grid">
<?php
if (!function_exists('pnet_render_archive_card')) {
function pnet_render_archive_card($post_id) {
$title = get_the_title($post_id);
$url = get_permalink($post_id);
$excerpt = get_the_excerpt($post_id);
$heading_id = 'pnet-card-title-' . $post_id;
echo '<article class="pnet-card" aria-labelledby="' . esc_attr($heading_id) . '">';
$image_id = get_post_thumbnail_id($post_id);
if ($image_id) {
$alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
if ('' === trim($alt)) {
$alt = $title;
}
echo '<a class="pnet-card__media" href="' . esc_url($url) . '">';
echo wp_get_attachment_image($image_id, 'large', false, array(
'class' => 'pnet-card__image',
'alt' => $alt,
'loading' => 'lazy',
'decoding' => 'async',
));
echo '</a>';
}
echo '<h3 class="pnet-card__title" id="' . esc_attr($heading_id) . '"><a href="' . esc_url($url) . '">' . esc_html($title) . '</a></h3>';
echo '<div class="pnet-card__excerpt">' . wp_kses_post($excerpt) . '</div>';
echo '<a class="pnet-card__link" href="' . esc_url($url) . '">Read the full article: ' . esc_html($title) . '</a>';
echo '</article>';
}
}
if (have_posts()) {
while (have_posts()) {
the_post();
pnet_render_archive_card(get_the_ID());
}
}
?>
</div>
The attachment helper outputs image markup with WordPress-managed attributes, while the alt text uses the attachment description or falls back to the post title. Excerpts pass through wp_kses_post() to retain permitted content safely; titles and URLs use their matching escaping functions. The heading ID includes the post ID, so each card has a unique accessible label. If a post has no featured image, the image link is omitted and the rest of the card remains intact.

You might also like:
Build the responsive CSS Grid Layout
Put the column rule on the wrapper, not on individual cards. The browser calculates how many tracks fit inside that wrapper: repeat() creates repeated tracks, auto-fit chooses the usable count, and minmax() sets each track minimum and maximum. Here, min(100%, 17rem) keeps the minimum from exceeding a narrow parent, while 1fr shares remaining space evenly.
.pnet-card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 17rem), 1fr));
gap: 1.5rem;
min-width: 0;
}
.pnet-card-grid,
.pnet-card-grid * {
box-sizing: border-box;
}
.pnet-card {
display: flex;
min-width: 0;
flex-direction: column;
overflow-wrap: anywhere;
padding: 1rem;
border: 1px solid #d9e0e8;
border-radius: 0.5rem;
background: #fff;
}
.pnet-card__media {
display: block;
overflow: hidden;
margin: -1rem -1rem 1rem;
background: #eef2f6;
}
.pnet-card__image {
display: block;
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
}
.pnet-card__title {
min-width: 0;
margin: 0 0 0.75rem;
}
.pnet-card__excerpt {
min-width: 0;
}
.pnet-card__link {
margin-top: auto;
padding-top: 1rem;
}
.pnet-card a:focus-visible {
outline: 3px solid #155eef;
outline-offset: 3px;
}
As the container grows, another minimum-width track fits; as it shrinks, tracks wrap into fewer columns. No @media rules or device-specific column counts are needed. The wrapper width, available space, and 17rem minimum determine the result, so a sidebar naturally gets fewer columns than a wide archive area. The image rule keeps featured images consistently proportioned and crops them within their frame; cards without an image simply omit the media element and retain their spacing.

Pro Tip
Did you know: You can explore our complete guide on Customize WordPress Login Page for step-by-step details.
Choose auto-fit or auto-fill for the card tracks
Both keywords calculate how many repeated tracks can fit at the chosen minimum width, but they treat unoccupied tracks differently. With auto-fit, the browser collapses empty tracks after placing the cards, then lets occupied tracks expand into the freed space. That usually suits a post archive: if only one or two cards appear, they can use more of the available row instead of sitting in narrow columns with unused space beside them.
auto-fill keeps those empty tracks in the layout. This can be useful when a partially populated grid should retain a stable track pattern—for example, a catalog or dashboard where cards should line up with the columns expected when more items are present. Use the existing pnet-card-grid rule as the default, and add the is-fill class only when preserving empty tracks is intentional:
.pnet-card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 17rem), 1fr));
gap: 1.5rem;
}
/* Add is-fill to the grid wrapper to preserve empty tracks. */
.pnet-card-grid.is-fill {
grid-template-columns: repeat(auto-fill, minmax(min(100%, 17rem), 1fr));
}
Enqueue the stylesheet and connect the template
Save the grid CSS in your active child theme at assets/css/card-grid.css. Enqueue it from the child theme functions.php file so WordPress loads it through the normal front-end asset hook. This example checks that the file exists and uses its modification time as the version, which helps browsers request updated styles after you edit the file.
function pnet_enqueue_card_grid_assets() {
$relative_path = '/assets/css/card-grid.css';
$file_path = get_stylesheet_directory() . $relative_path;
if (!is_readable($file_path)) {
return;
}
wp_enqueue_style(
'pnet-card-grid',
get_stylesheet_directory_uri() . $relative_path,
array(),
(string) filemtime($file_path)
);
}
add_action('wp_enqueue_scripts', 'pnet_enqueue_card_grid_assets');
get_stylesheet_directory() and get_stylesheet_directory_uri() point to the active theme stylesheet directory, which is the child theme when one is active. Keep the file path and URL path aligned if you choose a different asset location. If you place the rules directly in the child theme style.css instead, enqueue that file with get_stylesheet_uri(); do not enqueue both copies unless you need both stylesheets.
Put the card loop from the previous step inside the appropriate archive template, such as archive.php or a custom post-type archive, or move it into a reusable template part called by that template. Confirm that the outer wrapper uses class="pnet-card-grid" and each card uses class="pnet-card", matching the CSS selectors. The WordPress Developer Resources reference for wp_enqueue_style explains the enqueue arguments used here.
You might also like:
Test the grid with real content and container widths
Verify the layout at a narrow phone width, a medium content width, and a wide desktop width. Treat these as test conditions, not breakpoint targets: the grid should respond to the width of its parent at each size. A grid inside a narrow sidebar should form fewer columns than one in a wide archive area, even when both are viewed on the same desktop screen.
Run the same checks with one card, two cards, and enough cards to fill several rows. With auto-fit, a sparse row should let its occupied tracks expand; with a full grid, cards should form additional tracks when the container has room. Use realistic posts, including a long title, a long unbroken string, varied excerpt lengths, and a post with no featured image. Confirm that text wraps, the image-free card still looks intentional, and nothing creates horizontal scrolling.
In browser developer tools, select the grid wrapper and inspect its computed grid-template-columns value. Confirm the stylesheet loaded and that the computed tracks change as you resize the parent container. If styles seem stale, use a hard reload with the browser cache disabled, then check the Network panel for the CSS file and verify its response. Also inspect the grid inside its actual theme layout; a max-width, sidebar, or parent padding can reduce the space available to tracks.
Finally, navigate through card links with the keyboard and confirm each has a visible focus indicator and a descriptive label. Check heading order and image alternatives, too. Test content with missing images and uneven excerpts without imposing fixed card heights that could clip text.
Common Errors
If cards never form columns, check that display: grid and the track rule are applied to the parent wrapper, not to each card. Confirm its class matches the stylesheet selector, then inspect the browser developer tools to make sure the CSS file loaded and is not being replaced by a stale cached version.
Horizontal scrolling on narrow screens usually means a child is wider than its track. Remove fixed widths, set min-width: 0 on grid items and their relevant children, and allow long text to wrap. Keep the track minimum safe for narrow parents with minmax(min(100%, 17rem), 1fr); check for oversized images or other content with a large intrinsic width, too.
If one or two cards stretch across the row more than expected, confirm that auto-fit is the intended behavior: it collapses empty tracks so occupied cards can expand. Use auto-fill instead when preserving empty track space is important to the design.
When WordPress displays unstyled cards, compare the template wrapper and card class names with the CSS selectors. Verify that the enqueue callback is attached to wp_enqueue_scripts, and that the stylesheet path points to the active child theme and the correct file.
Uneven titles and excerpts can make cards different heights; avoid rigid heights that clip content. Let text flow naturally, and use the existing flex layout and margin-top: auto on the reading link to align links without hiding content.
For distorted images, check the image element itself—not only its wrapper—for width: 100%, aspect-ratio, and object-fit: cover. If recent edits do not appear, verify the active theme, inspect the stylesheet response in the Network panel, and clear browser, page, and CDN caches. A file modification version can also help browsers request the updated asset.
Conclusion
A card grid that depends on viewport-specific breakpoints can require repeated adjustments as content and theme layouts change. This implementation replaces those rules with a clear sequence: use semantic WordPress card markup, load the stylesheet through the theme enqueue hook, and let intrinsic Grid sizing respond to the space available in the parent container. That keeps the card structure reusable while avoiding a growing list of column rules.
The track definition combines repeat(), auto-fit, and minmax() to place as many cards as can fit at the chosen minimum width. Empty tracks collapse so a sparse row can use its available space, while the minimum stays constrained inside a narrow parent. Adjust that minimum to suit your theme typography and content rather than assuming one card width works everywhere.
Before deploying, test a single card and a full archive, then check long titles, missing images, and narrow containers as well as wide ones. Confirm the stylesheet is loading, links remain keyboard accessible, and no card or child forces horizontal scrolling. Adapt the card fields and minimum track width to your theme, then verify the result at several container widths. With those checks complete, your CSS Grid Layout can respond naturally to real content without media queries.