Solution 1 :

I think this is more of a CSS question, than a Thymeleaf question. But here is one approach which may help you.

The approach is inspired by details in this question.

The following Thymeleaf template assumes you want to display five identially-sized squares in a row, with an image (or video) in each square:

<!doctype html>
<html lang="en" xmlns_th="">
        <meta charset="utf-8">
        <title>Grid Demo</title>


            <!-- included just for reference 
            div.three {
                float: left;
                position: relative;
                width: 29%;
                padding-bottom: 29%;
                margin: 1.7%;
                border: 1px solid grey;
            div.four {
                float: left;
                position: relative;
                width: 22%;
                padding-bottom: 22%;
                margin: 1%;
                border: 1px solid grey;

            <!-- five squares in a horizontal row -->
            div.five {
                float: left;
                position: relative;
                width: 17%;
                padding-bottom: 17%;
                margin: 1.1%;
                border: 1px solid grey;

            div.content {
                position: absolute;
                height: 80%;
                width: 90%;
                padding: 10% 5%;

            div.table {
                display: table;
                height: 100%;
                width: 100%;

            div.table-cell {
                display: table-cell;
                vertical-align: middle;

            .content .rs {
                width: auto;
                height: auto;
                max-height: 90%;
                max-width: 100%;



        <div th_each="inst, iStat : ${instances}" class="five">
            <div class="content">
                <div class="table">
                    <div class="table-cell">
                        <img class="rs" th_attr="src=${inst}"/>


In my case, the end result looks like this, across the width of my browser screen:

Five squares

Points to note:

1) If you want to dynamically display 3, 4, or 5 boxes, then you can parameterize the class="five" attribute in the Thymeleaf template. The size of the ${instances} object will tell you which class you need:


2) If you want to add video controls in a square, that would involve passing an object to Thymeleaf which contains not only the media file name, but a boolean (or similar) to indicate if the media is playable. Add such content in its own <div>, following the relevant <img> tag:

<div th_if="${isVideo}">...</div>

I hope that gives you some ideas.

Problem :

I have a div with a list of divs inside which carry images and videos. I need them to be all the same size (a square with the same size would be good). Cause now it’s all misaligned

That’s what I have tried till now:

<div class="row text-center text-lg-left">
                        <div th_each="inst, iStat : ${instances}" class="col-lg-3 col-md-4 col-6"
                            th_if="${inst.fileStatus} eq ${T(}">
                            <a href="#" class="d-block mb-4 h-100">
                                <img class="img-fluid img-thumbnail" data-target="#showMedia" data-toggle="modal" th:data-slide-to="${iStat.index}"
                                    style="width: 100%;" th_src="@{'/instances/' + ${} + '/thumbnail' + ${folder != null ? '?folder=' + : ''}}" alt="">

This space will support probably 5 images and that’s how it looks like currently:
current behavior

The middle one is a video.

I also would need something to distinguish between video and photos, how could I indicate it’s a video? Is that possible to attach a “play button” in front of it?