HTML CSS Grid Component for Responsive Cards

Pure HTML/CSS/JS Grid Card for clean, responsive content. Features a 2-column layout and one-click 'Copy Button' to enhance UX.
HTML CSS Grid  Component for Responsive Cards

If you need to display a series of items—whether they are AI prompts, fitness tips, product reviews, or news summaries—in a clean, organized layout, scrolling through long articles can frustrate your readers. The solution is this powerful Responsive Grid Card Component. This versatile component is designed to make your content visually digestible for any type of blog or article.

This component is your ultimate tool for delivering information efficiently without relying on heavy frameworks, as it's built using only pure HTML, CSS, and JavaScript. It automatically structures content into an appealing 2-column grid on desktop (powered by CSS Grid) and flawlessly collapses into a clean, single stack on all mobile devices, ensuring an excellent reading experience across any niche. This responsiveness is a key factor for modern SEO performance.

Its standout features, like the intelligent 'Read More' toggle and the immediate one-click 'Copy Button', transform static text into an interactive utility. This is achieved through minimal, efficient JavaScript. The universal design improves user engagement (UX) for any blog post, making your site a valuable resource by offering a quick way to view, expand, and instantly copy the essential content.

Table of Contents

Quick Note!
The responsive behavior of this component—displaying two cards side-by-side on desktop and stacking them in a single column on mobile—is achieved using modern CSS Grid, a technique that is both highly performant and stable across all modern browsers.


Installation Guide: Integrating the HTML, CSS, and JavaScript

Follow these precise steps to add the component to any Plus UI blog post. Remember to work in the post editor's HTML View mode.

  1. Step 1: HTML Structure
    Locate the section of your post where you wish the grid of cards to appear. Copy the entire <div id="photostory-container"> block (including all placeholder cards) and paste it there.
  2. Step 2: CSS Styling
    Copy the full <style> block and paste it near the top of your post content. This defines the look, the responsive grid, and the 'Read More' visual effects.
  3. Step 3: JavaScript Logic
    Copy the complete <script> block and paste it at the absolute end of your post content, just before the closing body tag (if possible) or as the very last element of your post. This handles the copy function and the expand/collapse logic.

Warning! Code Placement is Crucial
Always ensure the <style> and <script> tags are included exactly as provided. Incorrect placement or modification of the class names could completely break the interactive features.


The Complete Source Code Block

This multi-tab block contains all three parts. The placeholders are clearly marked with the <mark> tag for easy editing.

<div id="photostory-container">
<!-- START: Card Template (Copy this block for more items) -->
 <div class="slide-card">
  <div class="slide-image">
   <img src="[Placeholder Image Link]" alt="Relevant Alt Text for SEO">
    <span class="slide-counter"></span> 
     </div>
      <div class="slide-body">
      <button class="copy-btn" title="Copy Content"></button> 
     <h2>Use an SEO Keyword Here (e.g., Python Data Structure)</h2>
    <div class="text-content">
   <p>Introduce the topic here. This visible text should hook the reader.</p>
  <p>Add the detailed code, prompt, or explanation. This long content is what the 'Read More' feature will hide.</p>
  </div>
 <button class="read-more">Read More</button> 
            </div>
        </div>
   <!-- END: Card Template -->
   </div>
<style>
#photostory-container{display:grid;grid-template-columns:1fr;gap:16px;max-width:1200px;margin:0 auto}
@media (min-width:768px){#photostory-container{grid-template-columns:1fr 1fr}
}
.slide-card{border-radius:16px;box-shadow:0 4px 12px 0 rgba(0,0,0,.04);overflow:hidden;position:relative;background:#fff;margin:0}
.slide-image{position:relative}
.slide-image img{width:100%;height:auto;display:block}
.slide-counter{align-items:center;background:#f6f6f6;border:1px solid #ccc;border-radius:100px;display:flex;left:16px;font-size:12px;line-height:16px;padding:4px 8px;position:absolute;bottom:-12px;color:#1a1a1a;z-index:2}
.slide-counter span:first-child{font-weight:bold}
.slide-body{position:relative;border:1px solid #ececec;border-top:0;padding:23px 16px 16px;border-radius:0 0 16px 16px}
.slide-body h2{font-size:16px;line-height:24px;font-weight:700;margin-bottom:8px;font-family:Georgia,serif;color:#1a1a1a;margin-top:0;padding-right:40px}
.text-content{color:#595955;font-size:14px;line-height:20px;font-weight:400;margin:0;padding-right:40px;max-height:100px;overflow:hidden;position:relative;transition:max-height 0.3s ease-out}
.text-content p{margin:0 0 10px 0}
.text-content p:last-child{margin-bottom:0}
.text-content.is-truncated::after{content:'';position:absolute;bottom:0;left:0;right:0;height:40px;background:linear-gradient(to bottom,transparent,white)}
.read-more{display:none;font-size:14px;font-weight:bold;color:#0066cc;background:none;border:none;padding:8px 0 0 0;cursor:pointer;margin-top:8px}
.slide-body.needs-read-more .read-more{display:block}
.slide-body.is-expanded .text-content{max-height:1000px}
.slide-body.is-expanded .text-content.is-truncated::after{display:none}
.slide-body.is-expanded .read-more{display:none}
.copy-btn{position:absolute;top:16px;right:12px;background:#f0f0f0;border:1px solid #ddd;border-radius:6px;cursor:pointer;width:32px;height:32px;padding:0;transition:all 0.2s;z-index:2}
.copy-btn:hover{background:#e0e0e0}
.copy-btn::before{content:'';display:block;width:16px;height:16px;margin:auto;background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23555' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='9' y='9' width='13' height='13' rx='2' ry='2'%3E%3C/rect%3E<path d='M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1'%3E</path%3E</svg%3E");background-repeat:no-repeat;background-position:center}
.copy-btn.copied{background-color:#28a745;border-color:#28a745;width:auto;padding:0 10px}
.copy-btn.copied::before{display:none}
.copy-btn.copied::after{content:'Copied!';color:white;font-size:12px;font-weight:bold}
</style>
<script>
window.onload=function(){ 
  const allSlides=document.querySelectorAll('.slide-card'); 
  const totalSlides=allSlides.length; 
  
  allSlides.forEach((slide,index)=>{ 
    const counterElement=slide.querySelector('.slide-counter'); 
    if(counterElement){ 
      const currentSlideNumber=index+1; 
      counterElement.innerHTML=`<span>${currentSlideNumber}</span> / <span>${totalSlides}</span>`; 
    } 
    
    const copyBtn=slide.querySelector('.copy-btn'); 
    const heading=slide.querySelector('h2'); 
    const textContent=slide.querySelector('.text-content'); 
    if(copyBtn && heading && textContent){ 
      copyBtn.addEventListener('click',function(){ 
        const textToCopy=`${heading.innerText}\n\n${textContent.innerText}`; 
        navigator.clipboard.writeText(textToCopy).then(()=>{ 
          copyBtn.classList.add('copied'); 
          setTimeout(()=>{ 
            copyBtn.classList.remove('copied'); 
          },2000); 
        }); 
      }); 
    } 
    
    const body=slide.querySelector('.slide-body'); 
    const content=slide.querySelector('.text-content'); 
    const readMoreBtn=slide.querySelector('.read-more'); 
    if(content && readMoreBtn && body){ 
      if(content.scrollHeight > content.clientHeight){ 
        body.classList.add('needs-read-more'); 
        content.classList.add('is-truncated'); 
      } 
      
      readMoreBtn.addEventListener('click',()=>{ 
        body.classList.add('is-expanded'); 
        body.classList.remove('needs-read-more'); 
        content.classList.remove('is-truncated'); 
      }); 
    } 
  }); 
};
</script>

Advantages of Deploying the Interactive Grid Card

Adopting this UI component offers several concrete benefits for technical publishers:

FeatureImpact on Your Blog
Enhanced ResponsivenessEnsures a flawless reading experience on any device, from a large desktop monitor to the smallest smartphone screen.
Better Content OrganizationAllows you to present multiple related concepts (e.g., Python methods, Linux commands) in a highly organized and visually distinct grid.
Instant Copy UtilityThe dedicated copy button drastically reduces the effort required for users to save and use your code snippets or technical prompts.
Optimized for SEO All text, even the content initially hidden by 'Read More', is fully present in the source HTML, guaranteeing full indexing by search engines for better long-tail keyword performance.

Target Audience and Use Cases

This component is an ideal solution for a variety of creators seeking to present sequential or comparative content in a clean, scannable format. It transforms static documentation into a dynamic, user-friendly layout.

  1. Development & Coding:

    Showcase CSS/JS snippets, terminal commands, or API key examples; use the 'Copy Button' for instant retrieval.

  2. AI & Prompt Engineering:

    Display multi-step AI prompts or complex model inputs; hide the full prompt behind the 'Read More' toggle.

  3. Fitness & Wellness:

    List steps in a workout routine, display diet plan details, or summarize nutrients/health tips.

  4. E-commerce & Products:

    Present product features, side-by-side comparisons, or structured FAQs in an organized grid.

  5. Education & Training:

    Break down learning modules, list key definitions/glossary terms, or sequence tutorial steps.

  6. Blogging & Reviews:

    Summarize the pros and cons of an item, list the "Top 10" items, or structure travel guides and itineraries.

  7. System Administration:

    Organize server configurations, troubleshooting guides, or commonly used maintenance scripts.

  8. Financial & Investment:

    Display steps for a financial planning strategy, list investment options, or break down market analysis points.


Frequently Asked Questions (FAQ)

How many cards can I safely use in one post?

You can use twenty, thirty, or more cards without issues. The JavaScript automatically handles the numbering and all functionality. Since most of the content is hidden by 'Read More', the page remains visually manageable, even with a high number of items.

Can I customize the copy button text, or the 'Read More' link?

Yes, the 'Read More' text is directly editable in the HTML for each card: <button class="read-more">Read More</button>. The copy button's visual text ('Copied!') is controlled via CSS/JavaScript and can be modified in the provided code block if necessary.

What if the text is short and does not require a 'Read More' button?

The JavaScript is smart. It checks the scroll height of the content. If the text is shorter than the defined maximum height (100px in the CSS), the 'Read More' button and the truncation effect will not be displayed, ensuring a clean presentation for short snippets.


Final Thoughts

This component represents a significant upgrade for any coding blog focused on user experience and content clarity. By successfully implementing this responsive grid, you are not just embedding code; you are deploying a solution that respects your reader's time, facilitates quick learning, and provides practical utility. A cleaner, faster, and more interactive blog is just a copy-paste away. We highly recommend using this structure for your next list of technical examples.

Post a Comment