html - 使两个 float 表高度相同

标签 html css css-tables

我有两个行数不同的 float 表(请参阅 JSFiddle ).

我希望这两个表具有相同的高度,而不是将高度明确设置为例如 400 像素。我尝试按照 this 的回答中的建议将它们放入高度为 100% 的 div 容器中问题,但这只会导致表格不再水平对齐。

有没有办法只使用 HTML 和 CSS 来实现这一点?

这是表格的 HTML 代码:

<table class="left">
    <tr>
        <th>Row1</th>
        <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>        
    </tr>
        <tr>
        <th>Row2</th>
        <td>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</td>        
    </tr>
        <tr>
        <th>Row3</th>
        <td>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. </td>
        </tr>
    <tr>
        <th>Row4</th>
        <td>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</td>      
    </tr>
</table>

<table class="right">
    <tr>
        <th>1</th>
        <td>Monday</td>     
    </tr>
    <tr>
        <th>2</th>
        <td>Tuesday</td>        
    </tr>
    <tr>
        <th>3</th>
        <td>Wednesday</td>      
    </tr>
    <tr>
        <th>4</th>
        <td>Thursday</td>
    </tr>
        <tr>
        <th>5</th>
    <td>Friday</td>     
    </tr>
    <tr>
        <th>6</th>
        <td>Saturday</td>       
    </tr>
    <tr>
        <th>7</th>
        <td>Sunday</td>     
    </tr>
</table>

使用以下 CSS:

table {
  border-collapse: collapse;
  border-spacing: 0;
}
table td, th {
    border: 1px solid darkgrey;
    text-align: left;
    padding: 0.4em;
}

table.left {
    width: 70%;
    height: 400px;
    float: left;
}

table.right {
    width: 25%;
    height: 400px;
    float: right;
}

最佳答案

一个相当简单和健壮的方法是通过定义一个父容器将你的两个表包装到一个 CSS 表中 .wrap和两个细胞杠杆 wrapper .cell-wrap .

.wrapdisplay: table和宽度 100%,这为您提供了屏幕(或父 block )的整个宽度。

每个.cell-wrapdisplay: table-cell这迫使这两个元素具有相同的高度,您还需要将高度设置为 100% 才能正常工作,并且 vertical-align: top (或类似的)。

要获得所需的宽度,只需设置 .cell-wrap 之一的宽度即可元素到某个值,例如左侧的 70% .cell-wrap .

然后您将嵌套表格的高度设置为 100%,这两个表格将缩放到相同的高度。请记住将嵌套表格的宽度设置为 100%,否则它们将采用收缩至适合的值。

虽然它需要额外的标记,但它易于理解、健壮并且不需要 JavaScript。在这种情况下,我会原谅额外的标记以获得您需要的设计。

.wrap {
  display: table;
  width: 100%;
  height: 100%; /* need to set height for this to work in Chrome */
}
.cell-wrap {
  display: table-cell;
  vertical-align: top;
  height: 100%;
}
.cell-wrap.left {
  width: 70%;
  padding-right: 10px;  /* if you need some whitespace */
}
table {
  border-collapse: collapse;
  border-spacing: 0;
  height: 100%;
  width: 100%;
}
table td, table th {
  border: 1px solid darkgrey;
  text-align: left;
  padding: 0.4em;
}
<div class="wrap">
  <div class="cell-wrap left">
    <table class="left">
      <tr>
        <th>Row1</th>
        <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
      </tr>
      <tr>
        <th>Row2</th>
        <td>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</td>
      </tr>
      <tr>
        <th>Row3</th>
        <td>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</td>
      </tr>
      <tr>
        <th>Row4</th>
        <td>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</td>
      </tr>
    </table>
  </div>
  <div class="cell-wrap right">
    <table class="right">
      <tr>
        <th>1</th>
        <td>Monday</td>
      </tr>
      <tr>
        <th>2</th>
        <td>Tuesday</td>
      </tr>
      <tr>
        <th>3</th>
        <td>Wednesday</td>
      </tr>
      <tr>
        <th>4</th>
        <td>Thursday</td>
      </tr>
      <tr>
        <th>5</th>
        <td>Friday</td>
      </tr>
      <tr>
        <th>6</th>
        <td>Saturday</td>
      </tr>
      <tr>
        <th>7</th>
        <td>Sunday</td>
      </tr>
    </table>
  </div>
</div>

关于html - 使两个 float 表高度相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29745558/

相关文章:

javascript - 为具有计数效果的元素添加宽度

javascript - 外部 JS 文件无法识别 html 中的元素

javascript - 使用 css 动画 spritesheet

css - 尝试在浏览器中使用Segoe字体

html - 使用 CSS 显示两列,一列具有固定宽度,另一列占用剩余空间

html - 动态图像垂直居中时遇到问题

html - 滚动条在不同浏览器中的宽度是否始终相同?

html - 如何在 Angular JS 中获取按钮的徽章值

html - 查看xsl转换后的xml文档的html源码

jquery - 如何在我的搜索输入中添加搜索图标