以PHP编写的代码实现元素的棋盘格模式展示
P粉068486220
P粉068486220 2023-09-14 11:46:21
0
1
587

我这样从php端显示元素:

<?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 } ?>

在html页面上它们看起来像这样

.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>

是否可以使col-6交替交换,使页面上的元素以棋盘格的形式显示,像这样

.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

全部回复(1)
P粉204079743

自从bootstrap切换到display: flex,就可以使用纯现代CSS来实现这样的效果:

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

不需要对HTML进行任何更改。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板