1、水平居中
<style>
div.txtCenter{
text-align:center;
}
</style>
2、宽度固定居中显示
<style> div{ border:1px solid red;/*为了显示居中效果明显为 div 设置了边框*/ width:500px;/*定宽*/ margin:20px auto;/* margin-left 与 margin-right 设置为 auto */ } </style>3、设置垂直高度
父元素高度确定的单行文本的竖直居中的方法是通过设置父元素的 height 和 line-height 高度一致来实现的。
<style>
.container{
height:100px;
line-height:100px;
background:#999;
}
</style>
4、设置宽度不起作用时,如
position:float; 可改为absolute 宽度会显示出来
<style>
.container a{
position:absolute;
width:200px;
background:#ccc;
}
</style>