Implement the checkerboard pattern display of elements using code written in PHP
P粉068486220
P粉068486220 2023-09-14 11:46:21
0
1
565

I display elements from the php side like this:

<?php foreach ($cases as $case) { ?>
    <div class="case">
        <div class="row">
            <div class="col-6">
                <img src="<?= $case->getImageUrl() ?>">
            </div>
            <div class="col-6">
                <div class="text"><?= $case->text ?></div>
            </div>
        </div>
    </div>
<?php } ?>

On the html page they look like this

.case {
  padding: 50px;
}

img {
  width: 300px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" integrity="sha512-t4GWSVZO1eC8BM339Xd7Uphw5s17a86tIZIj8qRxhnKub6WoyhnrxeCIMeAqBPgdZGlCcG2PrZjMc+Wr78+5Xg==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<div class="case">
    <div class="row">
        <div class="col-6">
            <img src="https://i.imgur.com/JMHHMVl.jpg">
        </div>
        <div class="col-6">
            <div class="text">case 1</div>
        </div>
    </div>
</div>

<div class="case">
    <div class="row">
        <div class="col-6">
            <img src="https://i.imgur.com/m3cq9ez.jpg">
        </div>
        <div class="col-6">
            <div class="text">case 2</div>
        </div>
    </div>
</div>

<div class="case">
    <div class="row">
        <div class="col-6">
            <img src="https://i.imgur.com/IKXSAiw.jpg">
        </div>
        <div class="col-6">
            <div class="text">case 3</div>
        </div>
    </div>
</div>

Is it possible to alternately swap col-6 so that the elements on the page are displayed in a checkerboard pattern, like this

.case {
  padding: 50px;
}

img {
  width: 300px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" integrity="sha512-t4GWSVZO1eC8BM339Xd7Uphw5s17a86tIZIj8qRxhnKub6WoyhnrxeCIMeAqBPgdZGlCcG2PrZjMc+Wr78+5Xg==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<div class="case">
    <div class="row">
        <div class="col-6">
            <img src="https://i.imgur.com/JMHHMVl.jpg">
        </div>
        <div class="col-6">
            <div class="text">case 1</div>
        </div>
    </div>
</div>

<div class="case">
    <div class="row">
        <div class="col-6">
            <div class="text">case 2</div>
        </div>
        <div class="col-6">
            <img src="https://i.imgur.com/m3cq9ez.jpg">
        </div>
    </div>
</div>

<div class="case">
    <div class="row">
        <div class="col-6">
            <img src="https://i.imgur.com/IKXSAiw.jpg">
        </div>
        <div class="col-6">
            <div class="text">case 3</div>
        </div>
    </div>
</div>

P粉068486220
P粉068486220

reply all(1)
P粉204079743

Since bootstrap switched to display: flex, you can use pure modern CSS to achieve this effect:

.case:nth-of-type(2n) .row{
    flex-direction: row-reverse;
}

No changes to the HTML are required.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template