【问题标题】:How to center alight content in a div with knowing the height of both parent & child如何在知道父母和孩子的高度的情况下居中对齐 div 中的内容
【发布时间】:2016-02-28 23:42:23
【问题描述】:

我有像这样显示的产品项目

现在对我来说这里的挑战是垂直居中对齐第一列和第三列中的文本,而中间列中的文本可以合理增加或减少,但我必须保持左右列中的内容始终垂直居中对齐。

例如这里是代码

<div class="product">
  <div class="title">My Test Title</div>
  <div class="price">
    From<br/>
    $2500
  </div>
  <div class="description">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et blanditiis, quibusdam commodi beatae, dolorum reiciendis, ex veniam esse recusandae iusto sapiente labore quisquam illo deserunt odio non magni velit! Sed.</p>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et blanditiis, quibusdam commodi beatae, dolorum reiciendis, ex veniam esse recusandae iusto sapiente labore quisquam illo deserunt odio non magni velit! Sed.</p>
  </div>
</div>

// AND THE CSS is 
.product {
  border: 1px solid #CCC;
}
.product > div {
  padding : 10px;
  border: 1px solid #CCC;
}
.title
{
  width : 120px;
  float:left;
}
.price {
  width:120px;
  float:right;
  text-align:right;
}
.description {
  overflow:hidden;
}

这里是小提琴:https://jsfiddle.net/exleedo/rexwya5e/

有没有办法做到这一点而不使用display:table-cell; 或使用 javscript 来查找父级的高度?

使用table-cell的原因是因为table-cell不会再从css中取宽度,而使用JS计算父级的高度我觉得不是很好。

【问题讨论】:

  • @Paulie_D 对不起,保利,我的错。我现在添加了一个简单的小提琴。我已经有了使用 display:table-cell; 的解决方案并使用js计算父级的高度,但它对更简单的解决方案的好奇心让我问这个问题。
  • 答案基本上..是否。 CSS 无法对齐不同容器中的元素。
  • Flexbox 可以使容器等高,它甚至可以使内容居中,但它仍然不能对齐容器之间的东西。
  • 感谢@Paulie_D,我想我会选择表格单元格选项。我又想了想,我遇到了 display:table-cell 的宽度问题,但是我可以将宽度分配给 table-cell div 内的 div。我想那是完美的。我将在答案中添加小提琴。

标签: html css alignment


【解决方案1】:

我想我会选择 display:table-cell;因为这对我来说似乎是最合适的。我之前没有这样做的原因是我试图将宽度分配给表格列 div,这是不正确的。现在我在 table-column 中添加了一个 div 并在那里分配了宽度,这解决了我的问题。

例如这里是代码:

<div class="table product">
  <div class="table-column">
    <div class="title">
      The Title
    </div></div>
    <div class="table-column">
      <div class="description">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro fugit vero non dolorum, nisi modi veritatis ipsum magni, praesentium blanditiis vel error, odit dolores atque culpa, ratione tempore iusto neque. </p>
      </div>
    </div>
    <div class="table-column">
      <div class="price">
        Price
      </div>
    </div>
  </div>

// And the CSS

/* Start : CSS Table*/
.table .table-column {
  display: table-cell;
  vertical-align: middle;
  padding-right: 10px;
}

.table .table-column.align-top {
  vertical-align: top;
}

.table .table-column.align-bottom {
  vertical-align: bottom;
}

.table .table-column.padding-left-10px {
  padding-left: 10px;
}

.table .table-column.padding-right-15px {
  padding-right: 15px;
}

.table .table-column.padding-right-20px {
  padding-right: 20px;
}

.table .table-column.padding-right-40px {
  padding-right: 40px;
}

.table .table-column.no-paddings {
  padding: 0px;
}

.table .table-column.full {
  width: 100%;
}
/* End : CSS Table*/

.product {
  border: 1px solid #CCC;
}
.product > div {
  padding : 10px;
  border: 1px solid #CCC;
}
.title
{
  width : 120px;
  float:left;
}
.price {
  width:120px;
  float:right;
  text-align:right;
}
.description {
  overflow:hidden;
}

在这个小提琴中检查它:https://jsfiddle.net/exleedo/zL9arsj7/1/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2023-03-04
    • 1970-01-01
    • 2017-07-02
    • 2013-08-14
    • 2021-12-07
    相关资源
    最近更新 更多