1.居中对齐文本
如果仅需在元素内居中文本,请使用 text-align: center;
.center {
text-align: center;
border: 3px solid green;
}
效果如下:
2.居中对齐图像
如需居中图像,请将左右外边距设置为 auto,并将其设置为块元素:
img {
display: block;
margin-left: auto;
margin-right: auto;
padding:20px 0;
}
效果如下:
3. 左和右对齐 - 使用 position
对齐元素的一种方法是使用 position: absolute;
.right {
position: absolute;
right: 0px