dongwen7813 2017-03-11 10:49
浏览 39
已采纳

如何对齐动态生成的div或段落

I want to display a product page with every item aligned with each other. These products are dynamically generated using PHP code. The problem is, they won't align horizontally when one product has a longer name than the other.

To further understand my problem, here is a screenshot: Product misalignment screenshot

You can see that the product prices are not aligned with each other because they are influenced by the length of the product names.

Here is my PHP and HTML code:

<!--PHP CODE FOR EXTRACTING DATA FROM TABLE-->
<div class="span_container">
                        <div class="col-md-4 bottom-cd simpleCart_shelfItem" style="display: inline-block;">
                            <div class="product-at ">
                                <a href="single.php"><img class="img-responsive" style="width:300px; height:auto;" src="/EDGE/storeadmin/product_images/<?php echo $row['i_image']; ?>" alt="">
                                <div class="pro-grid">
                                            <span class="buy-in">View Details</span>
                                </div>
                            </a>
                            </div>
                            <p class="tun"><?php echo $row['i_name'];?></p>
                            <a href="checkout.php" class="item_add"><p class="number item_price"><i> </i>₱<?php echo $row['i_price']; ?></p></a>
                        </div>
                    </div>

And this is the CSS code of my paragraph (tun):

    p.tun {
    font-size:1em;
    color: #949494;
    text-align: center;
    line-height: 1.3em;
    padding: 1em 0;
}
  • 写回答

2条回答 默认 最新

  • dongyan6235 2017-03-11 11:01
    关注

    This is simple. Set position:relative; for the containing .simpleCart class, and a height like height:300px - replace 300 with whatever your box height needs to be so every containing box has the same height.

    Then use position:absolute; to position the elements within .simpleCart according to the heights that we know already.

    We know and can deduce:

    • the height of the image: therefore we use, as an example: position:absolute;top:5%; - change 5% according to your needs.
    • the height of the product prices and that those will be at the bottom no matter how high the .simpleCart box will be. So we use position:absolute;bottom:5% - change 5% according to your needs, again. Now every product price is at the same level. Provided that every .simpleCart box is of the same height.
    • Finally, we place the paragraph text with the .tun class in between with position:absolute;top:150px; - where you change 150 according to your needs so it fits between the image and the product pricing.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向