php 如何将html表格居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2609003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:06:29  来源:igfitidea点击:

How to center html table?

phphtmltcpdf

提问by user198003

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

尝试使用 PHP 和 TCPDF 创建正确的 PDF 文档。

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

你能帮我吗,我如何使用 writeHTML 函数在 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.

......但没有运气。

回答by RAfAR

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.

你必须制作一个有 3 列的表格,为每一列设置宽度,在中间的一列你必须创建你的表格。

<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 :)

我不为这种方法感到自豪,但它有效:)

回答by user198003

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.

但是,我确实设法通过使用 writeHTMLCell 函数来解决它,即。

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

If somebody can find better solution, please reply.

如果有人能找到更好的解决方案,请回复。

Tnx!

天啊!

回答by Dustin Carpenter

Try replacing your opening div tag with this...

尝试用这个替换你的开始 div 标签......

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

回答by Dustin Carpenter

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

如果 pdfs 支持 css,我会建议使用 css 设置 html 元素的样式

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

Hope this helps!

希望这可以帮助!

回答by user4455060

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>