简体   繁体   English

如何使CSS表格单元格内容填充单元格的高度?

[英]How to make CSS table-cell contents fill the height of the cell?

I have a div table layout like this: 我有这样的div表布局:

在此处输入图片说明

...but I'd like the nested table on the right to match the height of the table on the left, so that the right table rows are equally spaced apart. ...但是我希望右侧的嵌套表格与左侧的表格高度匹配,以便右侧的表格行等距隔开。 It seems that there's some padding going on that's preventing the right table from filling the full height of it's container. 似乎有些填充正在阻止正确的表填充其容器的整个高度。

Want I want is this (it's a photoshop mock, but you get the idea): 我想要的是这个吗(这是一个photoshop模拟,但您知道了):

在此处输入图片说明

I do not want to set a fixed height on the outer container . 我不想在外部容器上设置固定高度 Both left and right table heights should match whichever one is tallest. 左右桌子的高度都应与最高的那个相匹配。 I'm using a div table solution at the moment to contain the tables because it solves the problem whereby the table containers (light-green) height will match (I'm open to other possible solutions). 我现在使用div表解决方案来包含表,因为它解决了表容器(浅绿色)高度匹配的问题(我愿意接受其他可能的解决方案)。 However, it still leaves the problem of the shorter table not filling the height of it's container, as in the image. 但是,仍然存在较短的桌子无法填充其容器高度的问题,如图所示。

Here's a fiddle. 这是一个小提琴。

The HTML: HTML:

<div class="outer-container">
    <div class="inner-container">
      <div class="child">
        <table>
          <tr>
            <td>Label 1a</td>
            <td>Value 1a</td>  
          </tr>
          <tr>
            <td>Label 1b</td>
            <td>Value 1b</td>
          </tr>
          <tr>
            <td>Label 1c</td>
            <td>Value 1c</td>
          </tr>
          <tr>
            <td>Label 1d</td>
            <td>Value 1d</td>
          </tr>
          <tr>
            <td>Label 1e</td>
            <td>Value 1e</td>
          </tr>
        </table>
      </div>
      <div class="spacer"></div>
      <div class="child">
        <table>
          <tr>
            <td>Label 2a</td>
            <td>Value 2a</td>
          </tr>
          <tr>
            <td>Label 2b</td>
            <td>Value 2b</td>
          </tr>
          <tr>
            <td>Label 2c</td>
            <td>Label 2c</td>
          </tr>
      </table>
    </div>
  </div>
</div>

and the styling: 和样式:

.outer-container {
  display: table;
  padding: 10px;
  background: #5f5f5f;
  width: 400px;
  margin-left: auto;
  margin-right: auto;
}
.inner-container {
    display: table-row;
    padding: 10px;
    margin-top: 50px;
    width: 100%;
}
.child {
    display: table-cell;
    background: #d3e4d1;
    border: 1px solid white;
}
.spacer {
  display: table-cell;
  width: 10%;
}
.child table {
    width: 100%;
    height: 100%;
    padding: 10px;
}
.child td {
    width: 50%;
}
.child td:first-child {
    text-align: right;
    padding-right: 10px;
}

IE8 and up solution required. 需要IE8及更高版本的解决方案。

The table-cell contents were filling up the height of the cells, the tables (and table cells) were just different sizes because the contents were different and they weren't declared to be the same height. 表格单元格的内容填满了单元格的高度,表格(和表格单元格)的大小不同,因为内容不同,并且没有声明它们的高度相同。

Here's a very minimal working example: https://jsfiddle.net/egzs1sm3/ 这是一个非常简单的工作示例: https : //jsfiddle.net/egzs1sm3/

If you'd like you could remove the div s they're nested in and just apply the static height to the table s, I don't see why not. 如果您希望删除嵌套的div并仅将静态高度应用于table s,我不知道为什么不这样做。

Here's how to accomplish it via flexbox: 以下是通过flexbox完成此操作的方法:

https://jsfiddle.net/tqvqsd2y/1/ https://jsfiddle.net/tqvqsd2y/1/

display:flex on the container is inconsequential, that's just to make to make them be right next to eachother for comparison's sake. display:flex在容器上是无关紧要的,这只是为了使其比较而使它们彼此紧靠。 No reason you couldn't use that though, I think it's what you wanted. 但是,没有理由您不能使用它,我想这就是您想要的。

Me again. 又是我。

Do they have to be separate tables? 它们必须是单独的表吗? Because two cells in the same table would automatically adjust the height and justify the content... 因为同一表格中的两个单元格会自动调整高度并证明内容的合理性...

<table>
  <tbody>
    <tr>
      <td>
        <h3>
          <span>asdf 1</span>
          <span>asdf 2</span>
        </h3>
        <h3>
          <span>asdf 1</span>
          <span>asdf 2</span>
        </h3>
      </td>
      <td>
        <h3>
          <span>asdf 1</span>
          <span>asdf 2</span>
        </h3>
        <h3>
          <span>asdf 1</span>
          <span>asdf 2</span>
        </h3>
        <h3>
          <span>asdf 1</span>
          <span>asdf 2</span>
        </h3>
      </td>
    </tr>
  </tbody>
</table>

https://jsfiddle.net/7w963q7v/ https://jsfiddle.net/7w963q7v/

Notice how different labels are now inline elements within block elements. 请注意,现在在块元素中的内联元素中如何使用不同的标签。

I have changed your existing HTML, that will work on IE8 too. 我已经更改了您现有的HTML,也可以在IE8上使用 Though I didn't tested on it, but I am sure, it won't deviate. 尽管我没有对此进行测试,但是我敢肯定,它不会偏离。

Option 1: 选项1:

Using a cellspacing on the main table . 在主table上使用cellspacing

Please find the structure below: 请找到以下结构:

<div class="outer-container">
  <div class="inner-container">
    <table cellspacing="15">
      <tbody>
        <tr>
          <td class="child">
            <table>
              <!-- put your contents of the first table -->
            </table>
          </td>
          <td class="child">
            <table>
              <!-- put your contents of the second table -->
            </table>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

Watch the demo here . 此处观看演示。

Option 2: 选项2:

Without using the cellspacing . 不使用cellspacing

<div class="outer-container">
  <div class="inner-container">
    <table> <!-- no 'cellspacing' here -->
      <tbody>
        <tr>
          <td class="child">
            <table>
              <!--put your contents of the first table -->
            </table>
          </td>
          <td class="spacer"></td> <!-- class spacer with 'width: 10%' -->
          <td class="child">
            <table>
              <!--put your contents of the second table -->
            </table>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

Watch the demo here . 此处观看演示。

Update 1: 更新1:

See the updated demo : 请参阅更新的演示

I don't think, the height of the nested table would be able to occupy the height of the largest table using only CSS. 我不认为,仅使用CSS,嵌套tableheight就能够占据最大table的高度。 That's why I have made their parents as td , where it fills up the containers and doesn't depends upon the content size of its elements only, but the largest one out of all its siblings td 's. 这就是为什么我把他们的父母定为td ,它在其中填充了容器,而不仅取决于元素的内容大小,还取决于所有同级td最大的元素。

Anyways, one can always target the child elements of any other elements using either JS or CSS. 无论如何,始终可以使用JS或CSS定位其他任何元素的子元素。

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

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