简体   繁体   English

如何将具有不同高度的元素对齐到表格单元格的顶部和底部?

[英]How to align elements with different height to the top AND to the bottom in table cells?

I'm working on a Drupal site that promotes rural products and services of several vendors. 我正在一个Drupal网站上工作,该网站推广了多家供应商的农村产品和服务。 On the main page I display a few items in a grid, each on a 'product card' (vendor, address, phone, product image, product name, price, 'Buy now' button). 在主页上,我在网格中显示一些项目,每个项目都在“产品卡”上(供应商,地址,电话,产品图片,产品名称,价格,“立即购买”按钮)。 Since each part of these attributes of a product may has different lenght, the layout of the page is very confused (see this screenshot: http://tinypic.com/r/2i1ede9/5 ). 由于产品的这些属性的每个部分可能具有不同的长度,因此页面的布局非常混乱(请参见此屏幕快照: http : //tinypic.com/r/2i1ede9/5 )。

Actually I have two questions about this issue: 其实我对此有两个问题:

  1. Is there any web designing method how to display elements of different height in fix-sized containers nicely? 有什么网页设计方法可以很好地在固定大小的容器中显示不同高度的元素? Is it possible to level their height? 可以调高它们的高度吗?
  2. How should I align one div (vm_prod_cat) to the top and the other (offsite_selling) to the bottom of the cell? 我应该如何将一个div(vm_prod_cat)对齐到单元格的顶部,将另一个(offsite_selling)对齐到单元格的底部? This can solve my problem more or less: a given compontent of product cards (eg address of vendors) won't be of equal height, but the layout would look balanced. 这可以或多或少地解决我的问题:给定的产品卡组件(例如供应商地址)高度不相等,但是布局看起来很平衡。 I tried to add the display: inline-block; 我试图添加display: inline-block; and the vertical-align: top; the vertical-align: top; or vertical-align: bottom; vertical-align: bottom; attributes but they didn't worked for me. 属性,但它们对我没有用。

Drupal generates the grid with these table cells and divs: Drupal使用这些表单元格和div生成网格:

<tr class="row-3 row-last">
<td class="col-1 col-first">
<td class="col-2">
<div class="vm_prod_cat prod_cat_tur">
<div>
<div class="views-field views-field-uc-product-image">
<div class="vm_product">
<div class="offsite_selling">
</td>
<td class="col-3 col-last">
</tr>

You could use the display:table family rules to have boxes behaving like tables : 您可以使用display:table家庭规则来使框的行为类似于table:

.gal {
  width:80%;
  margin:auto;
  text-align:center;
  min-width:350px;
}
.gal article {
  display:inline-table;
  height:300px;
  width:30%;
  min-width:100px;
  box-shadow: 0.5em 0.5em 0.5em,
    inset 0 0 0 1px;;
  margin:10px;
}
.gal article > header ,
.gal article > footer ,
.gal article > div {
  display:table-row;
  padding:0.5em;
  background:gray
}
.gal article > div {
  display:table-cell;
  height:100%;
  vertical-align:middle;
  background:rgba(0,0,0,0.2);
}
<section class="gal">
  <article>
    <header>
      header that takes room it needs
    </header>
    <div>
      content that takes room left
    </div>
    <footer>
      footer that takes room needed
    </footer>
  </article>
<!-- ... and so on -->

see test here : http://codepen.io/gc-nomade/pen/ALifh 参见此处的测试: http : //codepen.io/gc-nomade/pen/ALifh

each article may grow alone. 每篇文章可能会单独增长。 To avoid them to grow on their width, add table-layout:fixed; 为了避免它们的宽度增加,请添加table-layout:fixed; , nothing will stop them growing on their height. ,没有什么能阻止他们成长。 You need to set a minimal safe height. 您需要设置一个最小的安全高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM