简体   繁体   English

表和H1不对齐

[英]Table And H1 Not Aligning

Why are both my H1 and table not aligning to the center? 为什么我的H1和桌子都没有对准中心? I would like both my H1 id and my table to be centered with one and other I posted a image below of how I would like my website to look and the current state of it. 我希望我的H1 ID和我的桌子都以一个和另一个居中,我在下面贴了一张图片,显示我希望网站的外观及其当前状态。 I tried text-align:center; 我尝试了text-align:center; to shift both my H1 id and my table but it doesnt seem to work, I also tried to use for my table to center with my H1 but that also did not work. 来转移我的H1 ID和我的桌子,但它似乎不起作用,我还尝试使用我的桌子将我的H1居中,但这也没有用。 I am unsure how to approach this problem and what I need to go about to do to solve it. 我不确定如何解决该问题以及解决该问题所需要做的工作。

HTML 的HTML

  <h1 id="meetsh1">Recent Meets</h1>
  <table align="center">
    <tr>
      <th>Date</th>
      <th>Teams</th>
      <th>Score</th>
    </tr>
    <tr>
      <td>03/03/18</td>
      <td>Harrison at NYS Championships (Finals)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>03/02/18</td>
      <td>Harrison at NYS Championships (Prelims)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>02/13/18</td>
      <td>Harrison at Section I Championships (Finals)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>02/12/18</td>
      <td>Harrison at Section I Championships (Prelims)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>02/08/18</td>
      <td>Harrison at Section I Championships (Dive)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>01/29/18</td>
      <td>Harrison VS Ossining</td>
      <td>53-41</td>
    </tr>
  </table>

CSS 的CSS

#meetsh1 {
  text-align: center;
  padding-top: 30px;
  font-weight: bolder;
  text-decoration: underline;
  color: #000000;
  font-size: 25px;
}

table {
  border-collapse: collapse;
  width: 80%;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 100px;
}

td, th {
  border: 1px solid #dddddd;
  text-align: center;
  padding: 8px;
}

What my website currently looks like. 我的网站目前的外观。

How I want it to look. 我希望它看起来如何。

Jfiddle 杰菲德尔

Table and header are actually centered. 表和标题实际上居中。 The problem is that the calculated size for the outer columns differ slightly. 问题在于外部列的计算大小略有不同。 Therefore the center column is not centered. 因此,中心列未居中。

To solve that, you could add a class to the 'Date' and 'Score' column and set a fixed width via CSS. 为了解决这个问题,您可以在“日期”和“分数”列中添加一个类,并通过CSS设置固定宽度。

<tr>
  <th class="same-size-col">Date</th>
  <th>Teams</th>
  <th class="same-size-col">Score</th>
</tr>

and in your CSS file: 并在您的CSS文件中:

.same-size-col {
   width: 15%; /* or e.g. 100px */
}

Or you use th:first-child, th:last-child , which keeps your html cleaner. 或者您使用th:first-child, th:last-child ,这可以使您的html保持整洁。

The first column is a bit bigger than the last. 第一列比最后一列大一点。 So the middle column is between two unequally sized columns. 因此,中间列位于两个大小不等的列之间。 Add this to your css: 将此添加到您的CSS:

th:first-child, th:last-child{ 
    width: 175px; /* fixed width! */
}

The column sizes auto-adjust depending on the text inside them. 列大小会根据其中的文本自动调整。

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

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