search

Home  >  Q&A  >  body text

Implement the checkerboard pattern display of elements using code written in PHP

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粉068486220P粉068486220438 days ago573

reply all(1)I'll reply

  • P粉204079743

    P粉2040797432023-09-15 09:10:35

    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.

    reply
    0
  • Cancelreply