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条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题