How to center html table?

↘锁芯ラ 提交于 2019-12-18 07:03:24

问题


Trying to create proper PDF document, using PHP and TCPDF.

Can you help me, how can I use writeHTML function to create and center table, in TCPDF?

Tryed with:

 $html = '

 <div style="margin-left: auto; margin-right: auto; width: 50%">
  <table border="1" width="200" align="center"><tr><td><b>Invoice number: '.$this->xInvoiceNumber.'</b></td></tr></table>
  <br />
  <table border="1" width="200" align="center"><tr><td>'.$this->xClient.'</td></tr></table>
  <br />
 </div>

... but no luck.


回答1:


You have to make a table with 3 columns, set the width for every one of them and in the middle one you have to create your table.

<table>
<tr>
<td style="width:25%"></td>
<td style="width:50%"><table><tr><td>Your content</td></tr></table></td>
<td style="width:25%"></td>
</tr>
</table>

I'm not proud of this method but it's working :)




回答2:


Ok, so I don't know if there is solution for my problem...

However, I did manage to solve it by using writeHTMLCell funcion, ie.

$this->writeHTMLCell(50, 0, 50, 50, 'cellcontent', 'LRTB', 1, 0, true, 'L'); 

If somebody can find better solution, please reply.

Tnx!




回答3:


Try replacing your opening div tag with this...

<div style="margin:5px auto; width:50%">



回答4:


Never done anything like this however, this is the code you would need to center a table that is the cross browser compatible

<div style="text:align:center;">
  <table style="margin:0px auto" border="1" width="200" align="center">
    <tr>
      <td><b>Invoice number: </b></td>
    </tr>
  </table>
  <br />
  <table style="margin:0px auto"border="1" width="200" align="center">
    <tr>
      <td>Client</td>
    </tr>
  </table>
  <br />
</div>

If pdfs support css i would advise styling the html elements using css

table{
  border:1px solid black;
  margin:0px auto;
  text-align:center;
  width:200px;
}

Hope this helps!




回答5:


Try this code; it worked for me:

<table style="width:100%">
  <tr>
    <td style="width:30%">left margin</td>
    <td style="width:40%">
      <table border="1" style="width:100%">
        <thead>
          <tr>
            <td style="width:100%" colspan="2"></td>
          </tr>
          <tr>
            <td style="width:40%"><b></b></td>
            <td style="width:60%"><b></b></td>
          </tr>
        </thead>
      </table>
    </td>
    <td style="width:30%">rigth margin</td>
  </tr>
</table>


来源:https://stackoverflow.com/questions/2609003/how-to-center-html-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!