<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>常用对齐方式</title>
</head>
<style type="text/css">
.box{
width: 200px;
height: 200px;
background: red;
text-align: center;
}
.box a{
line-height: 200px;
text-decoration: none;
color: #fff;
}
.box1 {
width: 200px;
height: 200px;
background-color: #77f770;
text-align: center;
/*以下二个声明可以使块级子元素垂直居中*/
display: table-cell; /*设置显示方式为表格单元格*/
vertical-align: middle; /*设置该单元格内的元素垂直居中*/
}
.box1 a{
text-decoration: none;
color: #fff;
}
.box2{
width: 200px;
height: 200px;
background: chocolate;
/*以下二个声明可以使块级子元素垂直居中*/
display: table-cell; /*设置显示方式为表格单元格*/
vertical-align: middle; /*设置该单元格内的元素垂直居中*/
}
.box3{
width: 100px;
height: 100px;
background: darkcyan;
margin: 0 auto;/*水平居中*/
}
.box4 {
width: 200px;
height: 200px;
background-color: #ec51ee;
text-align: center;
/*以下二个声明可以使块级子元素垂直居中*/
display: table-cell; /*设置显示方式为表格单元格*/
vertical-align:bottom; /*设置该单元格内的元素底边居中*/
}
.box4 ul {
margin: 0;
padding: 0;
}
.box4 li {
list-style: none;
display: inline;
}
</style>
<body>
1、单行文本居中对齐
<div class="box">
<a href=""> 单行文本居中对齐</a>
</div>
2、多行文本居中对齐
<div class="box1">
<a href="">我是多行文本1</a><br>
<a href="">我是多行文本2</a><br>
<a href="">我是多行文本3</a>
</div>
3、区域居中
<div class="box2">
<div class="box3"></div>
</div>
4,底部水平居中
<div class="box4">
<ul>
<li><a href="">1</a></li>
<li><a href="">2</a></li>
<li><a href="">3</a></li>
<li><a href="">4</a></li>
<li><a href="">5</a></li>
</ul>
</div>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号