Creating a Pinterest-Style Card Layout with Bootstrap and Masonry

CK CK 3788 
Created at  
594 0 0

Pinterest's card layout has become quite popular due to its visually appealing and responsive nature. In this tutorial, we'll create a similar layout using Bootstrap 5 and Masonry.

Prerequisites

Before we start, make sure you have Bootstrap and Masonry included in your project.

<!-- Bootstrap 5 CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">

<!-- Masonry CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/masonry/4.2.2/masonry.pkgd.min.css" rel="stylesheet">

 

HTML Structure

First, let's set up our HTML structure. We'll create a container with rows and columns where each column represents a card.

<div class="container">
    <div class="row" id="masonry-grid">
        <div class="col-md-4 mb-3">
            <div class="card">
                <img src="image1.jpg" class="card-img-top" alt="Image 1">
                <div class="card-body">
                    <h5 class="card-title">Card title</h5>
                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                </div>
            </div>
        </div>
        <!-- More columns -->
    </div>
</div>

 

JavaScript Initialization

Now, let's initialize Masonry on our grid container. We'll set the column width and gutter to ensure proper alignment.

<script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/4.2.2/masonry.pkgd.min.js"></script>
<script>
    var grid = document.getElementById('masonry-grid');
    var masonry = new Masonry(grid, {
        itemSelector: '.col-md-4',
        columnWidth: '.col-md-4',
        gutter: 30
    });
</script>

 

Making it Responsive

To ensure our layout adjusts properly on different screen sizes, we'll need to re-layout Masonry on window resize.

<script>
    window.addEventListener('load', function() {
        var masonry = new Masonry(grid, {
            itemSelector: '.col-md-4',
            columnWidth: '.col-md-4',
            gutter: 30
        });
    });

    window.addEventListener('resize', function() {
        masonry.layout();
    });
</script>

 

Finishing Touches

Finally, let's add some CSS to adjust the spacing and appearance of our cards.

#masonry-grid .card {
    width: 100%;
}

#masonry-grid .card-img-top {
    width: 100%;
    height: auto;
}

#masonry-grid .card-body {
    padding: 1rem;
}

 

And there you have it! You now have a Pinterest-style card layout using Bootstrap and Masonry.

Feel free to customize the content and styles to match your project requirements.

Tags Bootstrap Card Layout Frontend Development Grid Layout HTML JavaScript Masonry Pinterest Style Responsive Design Web Development Facebook X
Comments 0
Similar posts
  1. Mastering Excel Data Importation in PHP
    554
  2. Equal Height Blocks in Bootstrap with JavaScript
    710
  3. Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file
    972
  4. JavaScript code to load XML Document
    601
  5. Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
    1,071
  6. Building a Basic DNS Server in PHP/Python: A Beginner's Guide
    739
  7. Dynamic DNS Made Easy: Building a Python-Based Solution
    795
  8. CentOS 7 PHP Apache Kafka: Installation, Examples, and System Diagram
    306
  9. How do we make better SEO with schema.org?
    423
  10. Simple Java Code to display Hello, World on the web
    563
  11. Tailwind CSS and shadcn/ui for React App with Charts and Graphs
    1,040
  12. What Replaced JSP? Modern Java Web Dev Explained (with Examples)
    255
  13. To block copy & paste on your blog content
    901
  14. Shake your browser window by Javascript
    935
  15. Photoshop script programming in Javascript
    531
  16. To compare two HTML tables on web page in real time
    515
  17. To hide/show area of web page without re-loading a page
    1,021
  18. Displays the html tags without rendering on a webpage
    351
  19. Javascript based query parser for AJAX application
    565
  20. To get web browser capability
    525
  21. Photo thumbnail viewer implemented in javascript
    486
  22. Javascript Confirmation
    593
  23. Javascript Prompts
    450
  24. How to know browser version in javascript ?
    443
  25. How to know the browser name in Javascript ?
    639
  26. Select All, None, Invert checkbox
    506
  27. Simple RSS Reader based on javascript
    502
  28. How to find the ip address of a client machine from a web application?
    518
  29. Clipboard copy function working
    3,368
  30. IFRAME Auto Resize in IE, FireFox by Javascript
    455
  1. What is Twitter Cards?
    402
  2. Code to Extract the Site Thumbnail Image for Twitter or Facebook
    390
  3. JavaScript Code to Get the Page Title
    334
  4. JavaScript Code to Get Meta Description from HTML
    394
  5. Javascript code to extract favicon URL
    396
  6. RSS to Array in JavaScript
    2,149
  7. Javascript function to convert sitemap.xml to array
    598
  8. Understanding Elasticsearch from Installation to Trouble Shooting
    911
  9. IndexNow for better SEO
    317
  10. Enhancing SEO for Single Page Applications: Implementing Dynamic URL Updates
    1,039
  11. Method to ignore JavaScript Errors globally instead of try... catch for each of cases
    969
  12. Method to catch the browser console log and errors for remote web debugging
    2,171
  13. Atlassian Jira Ticket API Response Example
    2,765
  14. Three different type of Word-Wrap options in HTML CSS
    1,183
  15. JavaScript code to load XML Document
    601
  16. Equal Height Blocks in Bootstrap with JavaScript
    710
  17. Copy text to clipboard in Javascript
    1,715
  18. To block copy & paste on your blog content
    901
  19. Shake your browser window by Javascript
    935
  20. Photoshop script programming in Javascript
    531
  21. To compare two HTML tables on web page in real time
    515
  22. To hide/show area of web page without re-loading a page
    1,021
  23. Displays the html tags without rendering on a webpage
    351
  24. Javascript based query parser for AJAX application
    565
  25. To get web browser capability
    525
  26. Photo thumbnail viewer implemented in javascript
    486
  27. Javascript Confirmation
    593
  28. Javascript Prompts
    450
  29. How to know browser version in javascript ?
    443
  30. How to know the browser name in Javascript ?
    639
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    39
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,610
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    93
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    156
  7. Array of arguments passed to script in PHP
    89
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    201
  10. The Two Clocks of Ad Server Architecture
    69
  11. Frequency Cap Architecture Overview
    513
  12. Collecting Proof-of-Play from 100,000 Screens
    145
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    286
  14. What is RedisVL?
    108
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    229
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    232
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    582
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    335
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    201
  22. Machine Learning: Training vs. Ranking
    220
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    221
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    194
  25. Designing a Data Curation System Around a Vector Database
    219
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    255
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    655
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    194
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    215
  30. Understanding Data Lineage
    259