HTML表格不如预期对齐。

huangapple go评论51阅读模式
英文:

HTML table does not line up as expected

问题

我已经创建了一个基于直播聊天活动的PHP应用程序,它呈现一个网页。它的排版有问题。

问题在于“标题”(在这个示例中显示为“这是直播标题应该在的地方”)应该占用所有的作者和Mod区域,并延伸到“消息”区域的开头(为此使用了colspan=2),但实际上它停在了Mod列。我正在使用colspan来控制这个,但colspan似乎没有按我期望的那样工作。我尝试过在多个平台上使用Chrome以及在iOS上使用最新的Safari,结果都一样。

换句话说,为了确保清楚,我看到的问题在于红色的那一行。我期望标题会延伸到消息区域,紧挨着它的是URL,而不是停在Mod列。另外,如果仔细看,Mod列的标题与其下面的值并不完全对齐,这可能是相同问题的结果。

通常我可以相当可靠地做到5以内的算术,但在这种情况下,我和计算机对于2加3的结果存在分歧。你知道我必须做什么才能解决这个问题吗?

英文:

I have created a PHP application that presents a web page based on livestream chat activity. It's not lining up properly.

The problem is that the "title" (which in this example says "This is where the livestream title should be") should consume all of the Author and Mod areas, and reach into the beginning of the "messages" area (which was created with colspan=2 for that purpose), but instead it stops at the Mod column. I'm using colspan to control this, and colspan does not seem to be doing what I expect. I have tried this with Chrome on several platforms, and with latest Safari on iOS... all with the same result.

Put another way, to be sure it's clear, the issue I see, is on the red-colored line. I expect the title to dip into the Messages area, with the URL immediately next to it, and not to stop at the Mod column. Also, if one looks closely, that Mod column heading doesn't exactly align on the right side with the values below it, and that's probably a result of the same problem.

I can usually do arithmetic pretty reliably up to 5, but in this instance the computer and I disagree about what 2+3 adds up to. Any idea what I must do in order to resolve this?

HTML表格不如预期对齐。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

headingLink {
  color: white;
  text-decoration: none;
}

.freeze-table {
  border-spacing: 0;
  padding: 0;
}

thead th {
  top: 0;
  position: sticky;
  background-color: #666;
  color: #fff;
  z-index: 20;
  min-height: 30px;
  height: 30px;
  text-align: left;
}

tr:nth-child(even) {
  background-color: #f2f2f2;
}

th,
td {
  padding: 0 5px;
  font-name: Courier;
  font-size: 12;
  outline: 1px solid #ccc;
  border: none;
  outline-offset: -1px;
  padding-left: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

tr {
  min-height: 25px;
  height: 25px;
}

pageElement {
  display: flex;
  flex-wrap: nowrap;
  align-items: center
}

* {
  padding: 0;
  margin: 0;
}

.fit {
  /* set relative picture size */
  max-width: 100%;
  max-height: 100%;
}

.center {
  display: block;
  margin: auto;
}

.imgbox {
  display: grid;
  width: 100%;
}

.center-fit {
  max-width: 100%;
  max-height: 100vh;
  margin: auto;
}

<!-- language: lang-html -->

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th colspan=2 style=&#39;text-align:center;font-weight:bold;&#39;&gt;&amp;nbsp;Date/Time&amp;nbsp;&lt;/th&gt;
      &lt;th style=&#39;font-weight:bold;&#39;&gt;Author&lt;/th&gt;
      &lt;th style=&#39;font-weight:bold;text-align:center;&#39;&gt;Mod&lt;/th&gt;
      &lt;th colspan=2 style=&#39;font-weight:bold;&#39;&gt;Message&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;

  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td colspan=2 style=&#39;text-align:center;color:red;font-weight:bold;&#39;&gt;2023-05-14&lt;/td&gt;
      &lt;td colspan=3 style=&#39;color:red;font-weight:bold;&#39;&gt;2023-05-14 This is where the livesteam title should be&lt;/td&gt;
      &lt;td style=&#39;text-align:right;&#39;&gt;&lt;a href=&#39;https://youtu.be/dummy&#39;&gt;https://youtu.be/dummy&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style=&#39;text-align:right;color:Black;&#39;&gt;1&amp;nbsp;R&lt;/td&gt;
      &lt;td style=&#39;text-align:center;color:Black;&#39;&gt;07:21&amp;nbsp;(1:14:21)&lt;/td&gt;
      &lt;td style=&#39;text-align:left;color:Black;&#39;&gt;Some Watcher&lt;/td&gt;
      &lt;td style=&#39;text-align:center;color:Black;&#39;&gt; (Mod)&lt;/td&gt;
      &lt;td colspan=2 style=&#39;text-align:left;color:Black;&#39;&gt;&lt;a href=&quot;https://youtu.be/ddummy?t=4461&quot; target=&quot;_blank&quot;&gt;_&lt;/a&gt;&amp;nbsp;Thanks for the concert Brother&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案1

得分: 1

以下是您提供的代码的中文翻译部分:

headingLink {
    color: white;
    text-decoration: none;
}

.freeze-table {
    border-spacing: 0;
    padding: 0;
}

thead th {
    top: 0;
    position: sticky;
    background-color: #666;
    color: #fff;
    z-index: 20;
    min-height: 30px;
    height: 30px;
    text-align: left;
}

tr:nth-child(even) {
    background-color: #f2f2f2;
}

th,
td {
    padding: 0 5px;
    font-name: Courier;
    font-size: 12;
    outline: 1px solid #ccc;
    border: none;
    outline-offset: -1px;
    padding-left: 5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

tr {
    min-height: 25px;
    height: 25px;
}

pageElement {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
}

* {
    padding: 0;
    margin: 0;
}

.fit {
    /* 设置相对图片尺寸 */
    max-width: 100%;
    max-height: 100%;
}

.center {
    display: block;
    margin: auto;
}

.imgbox {
    display: grid;
    width: 100%;
}

.center-fit {
    max-width: 100%;
    max-height: 100vh;
    margin: auto;
}
<table>
    <thead>
        <tr>
            <th colspan=2 style='text-align:center;font-weight:bold;'>&nbsp;日期/时间&nbsp;</th>
            <th style='font-weight:bold;'>作者</th>
            <th style='font-weight:bold;text-align:center;'>修改</th>
            <th colspan=2 style='font-weight:bold;'>消息</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td colspan=2 style='text-align:center;color:red;font-weight:bold;'>2023-05-14</td>
            <td colspan=3>
                <table>
                    <tr>
                        <td style='color:red;font-weight:bold;'>2023-05-14 这是直播标题应该放置的地方</td>
                        <td style='text-align:right;'><a href='https://youtu.be/dummy'>https://youtu.be/dummy</a></td>
                    </tr>
                </table>
            </td>
        </tr>

        <tr>
            <td style='text-align:right;color:Black;'>1&nbsp;R</td>
            <td style='text-align:center;color:Black;'>07:21&nbsp;(1:14:21)</td>
            <td style='text-align:left;color:Black;'>某位观众</td>
            <td style='text-align:center;color:Black;'> (修改)</td>
            <td colspan=2 style='text-align:left;color:Black;'><a href="https://youtu.be/ddummy?t=4461" target="_blank">_</a>&nbsp;感谢演唱会兄弟</td>
        </tr>
    </tbody>
</table>

这是您提供的代码的中文翻译部分。如有其他需要,请告诉我。

英文:

Try this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

headingLink {
  color: white;
  text-decoration: none;
}

.freeze-table {
  border-spacing: 0;
  padding: 0;
}

thead th {
  top: 0;
  position: sticky;
  background-color: #666;
  color: #fff;
  z-index: 20;
  min-height: 30px;
  height: 30px;
  text-align: left;
}

tr:nth-child(even) {
  background-color: #f2f2f2;
}

th,
td {
  padding: 0 5px;
  font-name: Courier;
  font-size: 12;
  outline: 1px solid #ccc;
  border: none;
  outline-offset: -1px;
  padding-left: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

tr {
  min-height: 25px;
  height: 25px;
}

pageElement {
  display: flex;
  flex-wrap: nowrap;
  align-items: center
}

* {
  padding: 0;
  margin: 0;
}

.fit {
  /* set relative picture size */
  max-width: 100%;
  max-height: 100%;
}

.center {
  display: block;
  margin: auto;
}

.imgbox {
  display: grid;
  width: 100%;
}

.center-fit {
  max-width: 100%;
  max-height: 100vh;
  margin: auto;
}

<!-- language: lang-html -->

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th colspan=2 style=&#39;text-align:center;font-weight:bold;&#39;&gt;&amp;nbsp;Date/Time&amp;nbsp;&lt;/th&gt;
&lt;th style=&#39;font-weight:bold;&#39;&gt;Author&lt;/th&gt;
&lt;th style=&#39;font-weight:bold;text-align:center;&#39;&gt;Mod&lt;/th&gt;
&lt;th colspan=2 style=&#39;font-weight:bold;&#39;&gt;Message&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td colspan=2 style=&#39;text-align:center;color:red;font-weight:bold;&#39;&gt;2023-05-14&lt;/td&gt;
&lt;td colspan=3&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td style=&#39;color:red;font-weight:bold;&#39;&gt;2023-05-14 This is where the livesteam title should be&lt;/td&gt;
&lt;td style=&#39;text-align:right;&#39;&gt;&lt;a href=&#39;https://youtu.be/dummy&#39;&gt;https://youtu.be/dummy&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#39;text-align:right;color:Black;&#39;&gt;1&amp;nbsp;R&lt;/td&gt;
&lt;td style=&#39;text-align:center;color:Black;&#39;&gt;07:21&amp;nbsp;(1:14:21)&lt;/td&gt;
&lt;td style=&#39;text-align:left;color:Black;&#39;&gt;Some Watcher&lt;/td&gt;
&lt;td style=&#39;text-align:center;color:Black;&#39;&gt; (Mod)&lt;/td&gt;
&lt;td colspan=2 style=&#39;text-align:left;color:Black;&#39;&gt;&lt;a href=&quot;https://youtu.be/ddummy?t=4461&quot; target=&quot;_blank&quot;&gt;_&lt;/a&gt;&amp;nbsp;Thanks for the concert Brother&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

Have added new &lt;table&gt; with two &lt;td&gt; (title and link) inside of &lt;td colspan=3&gt;

答案2

得分: 1

Table cell calculation is complex and fraught. To resolve your problem you need at least one row with individual column cells to act as a basis for the layout. You can then hide them with CSS and ARIA attributes (the latter for accessibility).

.dummy-row {
  height: 1px;
  visibility: hidden;
}

.freeze-table {
  border-spacing: 0;
  padding: 0;
}

thead th {
  top: 0;
  position: sticky;
  background-color: #666;
  color: #fff;
  z-index: 20;
  min-height: 30px;
  height: 30px;
  text-align: left;
}

tr:nth-child(even) {
  background-color: #f2f2f2;
}

th,
td {
  padding: 0 5px;
  font-name: Courier;
  font-size: 12;
  outline: 1px solid #ccc;
  border: none;
  outline-offset: -1px;
  padding-left: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

tr {
  min-height: 25px;
  height: 25px;
}

* {
  padding: 0;
  margin: 0;
}

.fit {
  /* set relative picture size */
  max-width: 100%;
  max-height: 100%;
}

.center {
  display: block;
  margin: auto;
}

.imgbox {
  display: grid;
  width: 100%;
}

.center-fit {
  max-width: 100%;
  max-height: 100vh;
  margin: auto;
}
<table>
  <thead>
    <tr>
      <th colspan=2 style='text-align:center;font-weight:bold;'>&nbsp;Date/Time&nbsp;</th>
      <th style='font-weight:bold;'>Author</th>
      <th style='font-weight:bold;text-align:center;'>Mod</th>
      <th colspan=2 style='font-weight:bold;'>Message</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td colspan=2 style='text-align:center;color:red;font-weight:bold;'>2023-05-14</td>
      <td colspan=3 style='color:red;font-weight:bold;'>2023-05-14 This is where the livestream title should be</td>
      <td style='text-align:right;'><a href='https://youtu.be/dummy'>https://youtu.be/dummy</a></td>
    </tr>

    <tr>
      <td style='text-align:right;color:Black;'>1&nbsp;R</td>
      <td style='text-align:center;color:Black;'>07:21&nbsp;(1:14:21)</td>
      <td style='text-align:left;color:Black;'>Some Watcher</td>
      <td style='text-align:center;color:Black;'> (Mod)</td>
      <td colspan=2 style='text-align:left;color:Black;'><a href="https://youtu.be/ddummy?t=4461" target="_blank">_</a>&nbsp;Thanks for the concert Brother</td>
    </tr>

    <tr class="dummy-row" aria-hidden="true">
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

Please note that the code and text have been translated, and any code-related content remains in its original form.

英文:

Table cell calculation is complex and fraught. To resolve your problem you need at least one row with individual column cells to act as a basis for the layout.

You can then hide them with CSS and ARIA attributes (the latter for accessibility).

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.dummy-row {
height: 1px;
visibility: hidden;
}
.freeze-table {
border-spacing: 0;
padding: 0;
}
thead th {
top: 0;
position: sticky;
background-color: #666;
color: #fff;
z-index: 20;
min-height: 30px;
height: 30px;
text-align: left;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
th,
td {
padding: 0 5px;
font-name: Courier;
font-size: 12;
outline: 1px solid #ccc;
border: none;
outline-offset: -1px;
padding-left: 5px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
tr {
min-height: 25px;
height: 25px;
}
* {
padding: 0;
margin: 0;
}
.fit {
/* set relative picture size */
max-width: 100%;
max-height: 100%;
}
.center {
display: block;
margin: auto;
}
.imgbox {
display: grid;
width: 100%;
}
.center-fit {
max-width: 100%;
max-height: 100vh;
margin: auto;
}

<!-- language: lang-html -->

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th colspan=2 style=&#39;text-align:center;font-weight:bold;&#39;&gt;&amp;nbsp;Date/Time&amp;nbsp;&lt;/th&gt;
&lt;th style=&#39;font-weight:bold;&#39;&gt;Author&lt;/th&gt;
&lt;th style=&#39;font-weight:bold;text-align:center;&#39;&gt;Mod&lt;/th&gt;
&lt;th colspan=2 style=&#39;font-weight:bold;&#39;&gt;Message&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td colspan=2 style=&#39;text-align:center;color:red;font-weight:bold;&#39;&gt;2023-05-14&lt;/td&gt;
&lt;td colspan=3 style=&#39;color:red;font-weight:bold;&#39;&gt;2023-05-14 This is where the livesteam title should be&lt;/td&gt;
&lt;td style=&#39;text-align:right;&#39;&gt;&lt;a href=&#39;https://youtu.be/dummy&#39;&gt;https://youtu.be/dummy&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#39;text-align:right;color:Black;&#39;&gt;1&amp;nbsp;R&lt;/td&gt;
&lt;td style=&#39;text-align:center;color:Black;&#39;&gt;07:21&amp;nbsp;(1:14:21)&lt;/td&gt;
&lt;td style=&#39;text-align:left;color:Black;&#39;&gt;Some Watcher&lt;/td&gt;
&lt;td style=&#39;text-align:center;color:Black;&#39;&gt; (Mod)&lt;/td&gt;
&lt;td colspan=2 style=&#39;text-align:left;color:Black;&#39;&gt;&lt;a href=&quot;https://youtu.be/ddummy?t=4461&quot; target=&quot;_blank&quot;&gt;_&lt;/a&gt;&amp;nbsp;Thanks for the concert Brother&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;dummy-row&quot; aria-hidden=&quot;true&quot;&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案3

得分: 0

<td colspan=3 style='color:red;font-weight:bold;'>2023-05-14 这里应该是直播标题</td>

尝试将 colspan 属性改为 1,如下示例:

<td colspan=1 style='color:red;font-weight:bold;'>2023-05-14 这里应该是直播标题</td>

英文:

I think you have a typo in the next line:

&lt;td colspan=3 style=&#39;color:red;font-weight:bold;&#39;&gt;2023-05-14 This is where the livesteam title should be&lt;/td&gt;

Try to change the colspan attribute to 1 as the example below:

&lt;td colspan=1 style=&#39;color:red;font-weight:bold;&#39;&gt;2023-05-14 This is where the livesteam title should be&lt;/td&gt;

huangapple
  • 本文由 发表于 2023年5月26日 01:40:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定