水平居中设置

html代码:

<body>
  <div class="txtCenter">我想要在父容器中水平居中显示。</div>
</body>

css代码:

<style>
  .txtCenter{
    text-align:center;
  }
</style>

定宽块状元素

html代码:

<body>
  <div>我是定宽块状元素,哈哈,我要水平居中显示。</div>
</body>

css代码:

<style>
div{
    border:1px solid red;/*为了显示居中效果明显为 div 设置了边框*/
    
    width:200px;/*定宽*/
    margin:20px auto;/* margin-left 与 margin-right 设置为 auto */
}

</style>

也可以写成:

margin-left:auto;
margin-right:auto;

注意:元素的“上下 margin” 是可以随意设置的。

宽高定盒子水平居中

效果如下:

 宽高不定水平居中

 <div class="box">
        <div class="box1">
            慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网慕课网
        </div>
    </div>

添加样式:

 .box {
        border: 1px solid #00ee00;
        height: 300px;
        position: relative;

    }

    .box1 {
        border: 1px solid red;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }

效果如下:

技术点的解释:

1、利用父元素设置相对定位,子元素设置绝对定位,那么子元素就是相对于父元素定位的特性。

2、子元素设置上和左偏移的值都为50%。

3、然后再用css3属性translate位移,给上和左都位移-50%距离,就能达到垂直水平居中的效果。

 

您可以使用PdfPTable的setHorizontalAlignment方法来设置表格的水平居中对齐。以下是一个示例代码: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class PDFTableExample { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("table_example.pdf")); document.open(); PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100); // 设置表格宽度为100% // 创建单元格并设置内容 PdfPCell cell1 = new PdfPCell(); cell1.addElement(new Paragraph("Cell 1")); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中对齐 table.addCell(cell1); PdfPCell cell2 = new PdfPCell(); cell2.addElement(new Paragraph("Cell 2")); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中对齐 table.addCell(cell2); PdfPCell cell3 = new PdfPCell(); cell3.addElement(new Paragraph("Cell 3")); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中对齐 table.addCell(cell3); document.add(table); document.close(); System.out.println("PDF created successfully."); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,我们创建了一个包含三列的PdfPTable对象,并在每个单元格中设置了内容和水平居中对齐。通过调用setHorizontalAlignment方法并传递Element.ALIGN_CENTER作为参数,我们可以将单元格内容水平居中对齐。最后,我们将表格添加到文档中并保存为PDF文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值