首页 > 解决方案 > 如何在移动视图中居中对齐 td 单元格或表格?

问题描述

.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" width="100%" 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;" height="24"></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>

标签: htmlcssmobilehtml-tableresponsive

解决方案


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

.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" width="100%" 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;" height="24"></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>


推荐阅读