【问题标题】:how to align different table row elements in html table如何在html表格中对齐不同的表格行元素
【发布时间】:2020-04-09 22:05:24
【问题描述】:

我创建了一个 2 行的 html 表。 第二行必须是 colspan="4" 请帮我对齐这些元素。 动作文本和两个按钮以给定宽度居中。 和许可证密钥:ZZZZ-ZZZZ-ZZZZ-ZZZZ 右对齐, 但它没有对齐响应能力。 我想在图片下方对齐许可证密钥:ZZZZ-ZZZZ-ZZZZ-ZZZZ

<table width="100%">
  <thead>
    <tr className="custom-tr">
      <th className="custom-th" width="35%">
        Product Name
      </th>
      <th className="custom-th" width="20%">
        Subscription Plan
      </th>
      <th className="custom-th" width="15%">
        Purchased Date
      </th>
      <th className="custom-th" width="30%" style="text-align: center;">
        Actions
      </th>
    </tr>
  </thead>
  <tbody>
    <tr className="custom-tr">
      <td className="custom-td">
        ESSENTIAL PC ANTIVIRUS
      </td>
      <td className="custom-td">
        MONTHLY PLAN
      </td>
      <td className="custom-td">
        4/9/2020
      </td>
      <td className="custom-td" style="text-align: center;">
        <button className="border-btn blue-border cursor">
              View License Key
            </button>
        <button className="border-btn danger-border cursor">
              Unsubscribe
            </button>
      </td>
    </tr>
    <tr className="custom-tr">
      <td colspan="4" className="custom-td p-r-21" style="text-align: right;">
        <span className="icon-color">
              Licence Key :
            </span> ZZZZ-ZZZZ-ZZZZ-ZZZZ
        <i className="f18 icon-color m-r-5 cursor fas fa-envelope m-l-20" aria-hidden="true"></i>
      </td>
    </tr>
  </tbody>
</table>

【问题讨论】:

  • 对不起,看不到图片,你能用css发布你的html代码吗?
  • 如果您能提供代码参考,这将对我们有所帮助。你能把代码放在stackbliz上,以便我们更好地理解你的问题。
  • 我添加了图片和代码
  • 对错误感到抱歉
  • 我认为您不需要额外的&lt;tr&gt;。在按钮下方添加许可证。您将更好地控制布局。

标签: html css


【解决方案1】:

我会直接在 td 中添加 许可证密钥

<table width="100%">
  <thead>
    <tr className="custom-tr">
      <th className="custom-th" width="35%">
        Product Name
      </th>
      <th className="custom-th" width="20%">
        Subscription Plan
      </th>
      <th className="custom-th" width="15%">
        Purchased Date
      </th>
      <th className="custom-th" width="30%" style="text-align: center;">
        Actions
      </th>
    </tr>
  </thead>
  <tbody>
    <tr className="custom-tr">
      <td className="custom-td">
        ESSENTIAL PC ANTIVIRUS
      </td>
      <td className="custom-td">
        MONTHLY PLAN
      </td>
      <td className="custom-td">
        4/9/2020
      </td>
      <td className="custom-td" style="text-align: center;">
        <button className="border-btn blue-border cursor">
          View License Key
        </button>
        <button className="border-btn danger-border cursor">
          Unsubscribe
        </button>
      </td>
    </tr>
    <tr className="custom-tr">
      <td></td>
            <td></td>
            <td></td>
      <td
        className="custom-td p-r-21"
        style="text-align: center;"
      >
        <span className="icon-color">
          Licence Key :
        </span>
        ZZZZ-ZZZZ-ZZZZ-ZZZZ
        <i
          className="f18 icon-color m-r-5 cursor fas fa-envelope m-l-20"
          aria-hidden="true"
        ></i>
      </td>
    </tr>
  </tbody>
</table>

添加多个空的&lt;td&gt; 不是很语义化,但这应该根据您的设计来解决问题。

【讨论】:

  • 我需要那个额外的 tr。这就是设计。正如我上面提到的,这就是设计
  • 如果您只希望额外的&lt;tr&gt; 显示按钮和许可证密钥之间的分线,您可以选择&lt;hr&gt; 而不是&lt;tr&gt;
【解决方案2】:

**只需在第二行再添加一个 TD... ** 请检查下面的sn-p

body {
margin:0;
}

table {
border-collapse: collapse;
}
table td, table th {
border: 1px solid #ddd;
border-collapse: collapse;
}
<table>
  <thead>
    <tr className="custom-tr">
      <th className="custom-th" width="35%">
        Product Name
      </th>
      <th className="custom-th" width="20%">
        Subscription Plan
      </th>
      <th className="custom-th" width="15%">
        Purchased Date
      </th>
      <th className="custom-th" width="30%" style="text-align: center;">
        Actions
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <!-- first row -->
      <td></td>
      <td></td>
      <td></td>
      <td><button>first but</button>
        <button>second</button>
      </td>
    </tr>
    <tr>
      <!-- second row -->
      <td colspan="3"></td>
      <td style="text-align:left; ">
        <!-- license info -->
        zz----zz-z-z-zzz--z-
      </td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 我这样做了,如果我这样做,“动作” - 表头没有正确对齐,在大型设备中没有响应。我需要在我上面提到的设计中完美对齐(第二张图片)跨度>
  • 您可以为包含按钮和许可证的 td 添加“text-align: left”...而且,操作标题可以有 'text-align: center' .. 而且,所有其他单元格都可以有文本对齐:左..
猜你喜欢
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 2017-07-23
  • 2021-09-18
  • 1970-01-01
  • 2021-03-27
相关资源
最近更新 更多