居中 div 内的 HTML 表格无法正确显示

标签 html css

我有一个包含多个表格的 DIV,这些表格由数据库搜索填充,因此显示的表格数量会有所不同。

我希望表格彼此相邻,这适用于前几行,但随后某些东西将表格的第四行向下推且乱序。

这是一个 fiddle :https://jsfiddle.net/tcanfarotta22/yn4s6cpv/

<table id='scholarship' style='float:left;' align='center'>
  <th colspan='2'>
    <h3>University of Michigan--Dearborn</h3>
    <h5>MI, Public</h5>
    <h5>Competitive</h5></th>
  <tr>
    <td style='text-align:center;'>
      <h3>$11,524 in-state $23,866 out-of-state</h3>
      <br>
      <h5>Tuition</h5></td>
    <td style='text-align:center;'>
      <h3>63.60%</h3>
      <br>
      <h5>Acceptance Rate</h5></td>
  </tr>
  <tr>
    <td>Scholarship Annual Amount: Full Tuition</td>
    <td>Eligibility: In-state</td>
  </tr>
  <tr>
    <td colspan='2' style='text-align:center;'>
      <h3>Required Test Scores</h3></td>
  </tr>
  <tr>
    <td>SAT: 970 CR+M</td>
  </tr>
  <tr>
    <td>ACT: 21</td>
  </tr>
  <tr>
    <td>GPA: 3</td>
  </tr>
  <tr>
    <td colspan='2'>Mid 50% SAT Score: SAT Math 498-660 SAT Critical Reading not reported SAT Writing 470-600 </td>
  </tr>
  <tr>
    <td colspan='2'>Mid 50% ACT Score: 22-27 </td>
  </tr>
  <tr>
    <td colspan='2'>Is this Scholarship Available for International Students? .</td>
  </tr>
  <tr>
    <td colspan='2'>US News Ranking: not on the list</td>
  </tr>
  <tr>
    <td colspan='2'>Forbes Ranking: #437 Best Colleges</td>
  </tr>
  <tr>
    <td colspan='2'>Money Ranking: </td>
  </tr>
  <tr>
    <td colspan='2' style='text-align:center;'><a href='http://umdearborn.edu/fa_morefreshmanscholarships' target='_blank'>Click Here to Visit Site</a></td>
  </tr>
  <tr>
    <td colspan='2'>
      <input type='checkbox' value='ARLUJ' name='cell[]'>Check the Box to Save</td>
  </tr>
  <tr>
    <td colspan='2' style='text-align:center;'>Scholarship Details</td>
  </tr>
  <tr>
    <td colspan='2'>Detroit Compact Scholarship awards renewable full-tuition scholarships to qualified students from the Detroit Compact High Schools. Students are selected to participate in this program by school administrators. Students are required to have a recalculated GPA of 3.0 and a minimum composite ACT of 21 or SAT combined score of 970.</td>
  </tr>
</table>

编辑:我希望它们在每行中显示为 4 个元素。

最佳答案

如果您希望它们始终排成一行,则不应使用 float

相反,您可以这样做,将 form 设为表格,将 div 设为表格单元格。

form {
  display: table;
}

form > div {
  display: table-cell;
  vertical-align: top;
}

https://jsfiddle.net/yn4s6cpv/2/

或者使用flexbox,只需添加如下样式:

form {
  display: flex;
}

https://jsfiddle.net/yn4s6cpv/3/

编辑:以便每行显示 4 个元素。您可以在每 5 个元素上设置 clear:left

form > div:nth-child(4n+1) {
  clear: left;
}

https://jsfiddle.net/yn4s6cpv/4/

关于居中 div 内的 HTML 表格无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34619761/

相关文章:

javascript - 如何使输入字段中的某些文本不可编辑? - 扩展

html - HTML5 DOCTYPE 会影响 CSS3 效果的呈现方式吗?

css - 我应该如何使用 stylelint 检查 SASS 文件中的缩进?

CSS3背景图片位置过渡

html - 如何处理此类错误?

javascript - 用 twig 删除 img 字段中的 onerror 标签

css - Bootstrap 3 导航栏品牌和导航栏结尾之间的空间

javascript - Bootstrap Carousel 不适用于 Angular ng-route

html - 给箭头添加动画似乎改变了元素的中心点

html - 在父 div 悬停时填充 SVG(无 JS)