render grid like pinterest with razor and bootstrap

Rene 1 Reputation point
2021-10-18T07:32:08.527+00:00

I would like to render a grid like in the picture using razor + bootstrap.
Currently by using rows, you get whitespace when an element is shorter.
How can i render this like displayed in the picture ?
The orderening should be left to right 3 elements per 'row'.

141256-grid.jpg

razor syntax

<div class="row">          
        @foreach (Boeket boeket in Model)  
        {  
            <div class="col-md-4">  
            <div>  
                <div class="ribbon2 shadow">  
                    <div class="likes">  
                        <i class="bi bi-heart-fill"></i>  
                        &nbsp;@boeket.NumberOfLikes<br />  
                    </div>  
                    <div class="comments">  
                        <i class="bi bi-chat-left-fill"></i>  
                        &nbsp;@boeket.NumberOfComments  
                    </div>  
                </div>  
            </div>  
  
            <div class="boeket">  
                <figure>  
                    <img src="@Url.Content(boeket.Foto)" alt="Image" class="img-fluid" />  
                    <figcaption class="figure-caption">@boeket.Titel</figcaption>  
                    <figcaption class="figure-caption">Gebruikersnaam</figcaption>  
                </figure>  
            </div>  
            </div>  
  
        }  
    </div>  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,041 Reputation points
    2021-10-19T20:52:23.14+00:00
    0 comments No comments

  2. Zhi Lv - MSFT 32,006 Reputation points Microsoft Vendor
    2021-10-20T07:55:32.393+00:00

    Hi @Rene ,

    As Bruce-SqlWork said, you could try to use the masonry layout, you can refer the following sample:

    <div class="container ">  
        <div class="row" data-masonry='{"horizontalOrder": true }'>  
            @foreach (var item in Model.Cards)  
            {   
                <div class="col-4">  
                    <div class="card border-primary">  
                        <div class="card-body">  
                            <h3 class="card-title">@item.Title</h3>  
                            <p class="card-text">@item.Text</p>  
                            <a href="#" class="btn btn-outline-secondary">Outline</a>  
                        </div>  
                    </div>  
                </div>  
            }  
        </div>  
    </div>  
    
    <style type="text/css">  
        .col-4{  
           margin-bottom:10px;  
        }  
    </style>  
     @section Scripts{  
        < script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/4.2.2/masonry.pkgd.min.js"></script>  
        < link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet">  
        < script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js"></script>  
    }  
    

    [Note]: Here we are using bootstrap 5.1.1 and masonry 4.2.2 version. And in the above sample, there has a space in the script tag (between '<' and "script"), please remember to remove it.

    The result as below:

    142035-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments