水平居中设置

6 篇文章 0 订阅

1、水平居中包括行内元素还是块状元素 ,块状元素里面又分为定宽块状元素,以及不定宽块状元素。

1.1、行内元素的水平居中,像文本、图片等行内元素,水平居中是通过给父元素设置 text-align:center 来实现的。
如下:
<style>
div{
    border:1px solid red;
    margin:20px;
}
.txtCenter{
	text-align:center;
}
.imgCenter{
    text-align: center;   
}
</style>
</head>

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

<div class="imgCenter"><img src="http://img.mukewang.com/52da54ed0001ecfa04120172.jpg" /></div>
</body>

1.2、定长的块级元素居中。(定宽块状元素:块状元素的宽度width为固定值)满足定宽和块状两个条件的元素是可以通过设置“左右margin”值为“auto”来实现居中的。
如下:
<style>
div{
    border:1px solid red;
	width:200px;
	margin:20px auto;
}

</style>
</head>

<body>
<div>我是定宽块状元素,我要水平居中显示。</div>
1.3、不定宽的块级元素居中。
1.3.1 第一种方式是,使用table标签包裹起来,table标签有自动居中的功能,
如下:两种定义table标签的方式。
<style>
table{
    margin: 0 auto;
}
.wrap{
    background: #ccc;
    /** 设置为table显示也可以实现table标签 */
    display: table; 
    margin: 0 auto;
}
</style>
</head>

<body>
<div>
<table>
  <tbody>
    <tr><td>
	<ul>
    	<li>我是第一行文本</li>
        <li>我是第二行文本</li>
        <li>我是第三行文本</li>
    </ul>
    </td></tr>
  </tbody>
</table>
</div>

<div class="wrap">
  设置我所在的div容器水平居中  
</div>

1.3.2、第二种不定长的块级元素设置居中使用display将块级元素变成行级元素,然后在父元素中设置text-align: center,来实现居中。如下
<style>
.container{text-align:center;background: red;}
.container ul{list-style:none;margin:0;padding:0;display:inline;}
.container li{margin-right:8px;display:inline;}
</style>
</head>

<body>
<div class="container">
    <ul>
    	<li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
    </ul>
</div>
</body>

1.3.3、第三种不定长的块级元素居中,通过给父元素设置 float,然后给父元素设置 position:relative 和 left:50%,子元素设置 position:relative 和 left: -50% 来实现水平居中。如下:
<style>
.container{
    float:left;
	position:relative;
	left:50%
}

.container ul{
	list-style:none;
	margin:0;
	padding:0;
	position:relative;
	left:-50%;
}
.container li{float:left;display:inline;margin-right:8px;}

.wrap{
    float: left;
    position: relative;
    left: 50%;
}
.wrap-center{
    background:#ccc;
    position: relative;
    left: -50%; 
}
</style>
</head>

<body>
<div class="container">
	<ul>
    	<li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
    </ul>
</div>
<br/><br/>
<div class="wrap">
    <div class="wrap-center">我们来学习一下这种方法。</div>
</div>
</body>




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用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、付费专栏及课程。

余额充值