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

如何对齐动态生成的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;
}

我想显示一个产品页面,其中每个项目彼此对齐。 这些产品是使用PHP代码动态生成的。 问题是,当一个产品的名称比另一个产品的名称更长时,它们不会水平对齐。 p>

为了进一步了解我的问题,这里有一个截图: strong> p>

您可以看到产品价格是 彼此不一致,因为它们受产品名称长度的影响。 p>

这是我的PHP和HTML代码: strong> p> \ n

 &lt;! - 用于从表中提取数据的PHP代码 - &gt; 
&lt; div class =“span_container”&gt; 
&lt; div class =“col-md-4 bottom-cd  simpleCart_shelfItem“style =”display:inline-block;“&gt; 
&lt; div class =”product-at“&gt; 
&lt; a href =”single.php“&gt;&lt; img class =”img- 响应“style =”width:300px; height:auto;“  src =“/ EDGE / storeadmin / product_images /&lt;?php echo $ row ['i_image'];?&gt;”  alt =“”&gt; 
&lt; div class =“pro-grid”&gt; 
&lt; span class =“buy-in”&gt;查看详细信息&lt; / span&gt; 
&lt; / div&gt; 
&lt;  ; / a&gt; 
&lt; / div&gt; 
&lt; p class =“tun”&gt;&lt;?php echo $ row ['i_name'];?&gt;&lt; / p&gt; 
&lt; a href  =“checkout.php”class =“item_add”&gt;&lt; p class =“number item_price”&gt;&lt; i&gt;  &lt; / i&gt;₱&lt;?php echo $ row ['i_price'];  ?&gt;&lt; / p&gt;&lt; / a&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
  code>  pre> 
 
 

这是 我的段落(tun)的CSS代码: strong> p>

  p.tun {
 font-size:1em; 
 color:#949494; 
 text  -align:center; 
 line-height:1.3em; 
 padding:1em 0; 
} 
  code>  pre> 
  div>

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.

You can use the following js function to match the module heights to the tallest.

equalheight = function(container){

var currentTallest = 0,
     currentRowStart = 0,
     rowDivs = new Array(),
     $el,
     topPosition = 0;
 $(container).each(function() {

   $el = $(this);
   $($el).height('auto')
   topPostion = $el.position().top;

   if (currentRowStart != topPostion) {
     for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
       rowDivs[currentDiv].height(currentTallest);
     }
     rowDivs.length = 0; // empty the array
     currentRowStart = topPostion;
     currentTallest = $el.height();
     rowDivs.push($el);
   } else {
     rowDivs.push($el);
     currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
  }
   for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
     rowDivs[currentDiv].height(currentTallest);
   }
 });
}

$(window).load(function() {
  equalheight('.equalise-height');
});

</div>