如何在移动视图中居中对齐 td 单元格或表格?

Posted

技术标签:

【中文标题】如何在移动视图中居中对齐 td 单元格或表格?【英文标题】:how to center align td cell or table in mobile view? 【发布时间】:2020-10-16 12:54:56 【问题描述】:

.button-center 
  max-width: 100% !important;
  min-width: 100px !important;
  width: 100% !important;
  text-align: center !important;
  display: inline-block !important;
  margin:auto;
<table cellpadding="0" cellspacing="0" role="presentation"  align="center" dir="ltr" valign="top" style="max-width:100%;">
  <tbody>

    <tr>
      <td style="text-align: left; font-family: arial; font-size: 12px;  color: #000;">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

      </td>

    </tr>
    <tr>
      <td style="height:24px;" ></td>

    </tr>
    <tr>
      <td style="text-align:center;">
        <table cellpadding="0" class="button-center" cellspacing="0" role="presentation" style="background-color:#9E2776;max-width:100%;" align="left">
          <tbody>
            <tr>
              <td>

                <a href="" style="color:ffffff;padding:10 10;font-family: Arial;font-size: 14px;text-align: center;text-decoration: none;display: block;border-collapse: collapse;font-weight: bold;">Lorem Ipsum</a>
              </td>

            </tr>
          </tbody>
        </table> 
      </td>
    </tr>
  </tbody>
</table>

【问题讨论】:

【参考方案1】:

您正在为该单元格 (.button-center) 定义 display: inline-block,这违背了自动表格格式化的整个原则。只需删除它,该单元格/表格的内容就会居中:

.button-center 
  max-width: 100% !important;
  min-width: 100px !important;
  width: 100% !important;
  text-align: center !important;
  margin: auto;
<table cellpadding="0" cellspacing="0" role="presentation"  align="center" dir="ltr" valign="top" style="max-width:100%;">
  <tbody>


    <tr>
      <td style="text-align: left; font-family: arial; font-size: 12px;  color: #000;">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.

      </td>

    </tr>
    <tr>
      <td style="height:24px;" ></td>

    </tr>
    <tr>
      <td style="text-align:center;">
        <table cellpadding="0" class="button-center" cellspacing="0" role="presentation" style="background-color:#9E2776;max-width:100%;" align="left">
          <tbody>
            <tr>
              <td>

                <a href="" style="color:ffffff;padding:10 10;font-family: Arial;font-size: 14px;text-align: center;text-decoration: none;display: block;border-collapse: collapse;font-weight: bold;">Lorem Ipsum</a>
              </td>

            </tr>
          </tbody>
        </table>

【讨论】:

以上是关于如何在移动视图中居中对齐 td 单元格或表格?的主要内容,如果未能解决你的问题,请参考以下文章

如何在表格单元格内垂直对齐 div?

word2003论文中的公式如何居中,编号如何右对齐?

在Word里面做表格如何将文字上下居中

请问在java JTable中如何设置单元格字体居中对齐?

表格文字上下居中怎么设置

excel中怎样使表格数据显示为水平居中和垂直居中?