mPDF:表格中的 p、h1-h6 文本对齐无效

5
这段代码在使用mPDF PHP类的表格中无法运行。 图片描述
    <table>
    <tr>
      <td class="contentDetails">
  <td class="contentDetails">
         <h3 style="text-align: right;"><strong>text align right</strong></h3>
         <h3 style="text-align: center;"><strong>text align center</strong></h3>
         <h3 style="text-align: left;"><strong>text align left</strong></h3>
     </td>
    </tr>
    </table>

我尝试使用

.contentDetails > h3 {display: block;} 

但是不起作用,并且在td之间的内容是来自编辑器tinymce的HTML。

这是完整的代码,当输出内容时,发现td文本左对齐而不是右对齐或居中。

<?php

$html = '
<h1>mPDF</h1>
    <table style="border-collapse: collapse;
    font-size: 12px;
    font-weight: 700;
    margin-top: 5px; 
    border-top: 1px solid #777;
    width: 100%;">
<tbody>
<tr>
  <td class="contentDetails">
         <h3 style="text-align: right;"><strong>text align right</strong></h3>
         <h3 style="text-align: center;"><strong>text align center</strong></h3>
         <h3 style="text-align: left;"><strong>text align left</strong></h3>
     </td>
</tr>
</tbody>
</table>';

include("mpdf.php");

$mpdf=new mPDF('c'); 
$mpdf->WriteHTML($html);

$mpdf->Output();

exit;
?>

请问您有什么问题?https://stackoverflow.com/help/how-to-ask - hiro protagonist
文本对齐:在h3中无法工作? - GoldenFingers
请发布您问题的演示。 - m4n0
更新更多细节。 - GoldenFingers
它不会起作用,因为您正在将文本对齐到单元格而不是整个单元格。 - m4n0
显示剩余2条评论
3个回答

13

尝试以下代码,我认为它会对你有帮助。

<table width="100%">
  <tr>
    <td class="contentDetails">
     <th align="left"> <h3><strong>text align right</strong></h3></th>
     <th align="center"> <h3><strong>text align center</strong></h3></th>
     <th align="right"> <h3><strong>text align left</strong></h3></th>
    </td>
  </tr>
</table>

在这里使用额外的<br>标签。


2
最佳答案是在td ContentDetails中使用th,我已经将其用于新表中。请查看最终代码:
33333333333333
- GoldenFingers

6

我已经准备好了很多小时的内容,要打印(mPdf v.6)。现在我可以给大家一个建议。不要使用表格来对齐内容或仅仅是保持内容。几乎所有元素的css属性都能正常工作。但是,不能将其中一个元素嵌套在表格中。


1
你是对的。- “但是 - 但不得嵌套在表格中。” - Raghavendra N

0
问题在于容器的宽度适合内容。将宽度设置为100%

table {
  width: 100%;
}
<table>
  <tr>
    <td class="contentDetails">
      <h3 style="text-align: right;"><strong>text align right</strong></h3>
      <h3 style="text-align: center;"><strong>text align center</strong></h3>
      <h3 style="text-align: left;"><strong>text align left</strong></h3>
    </td>
  </tr>
</table>


还没有生效,我认为必须使用TCPDF,它比MPDF更好。 - GoldenFingers
CSS对齐与mpdf或tcpdf无关 :) - m4n0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接