CSS水平居中/垂直居中的N个方法
2016-06-05 20:18
549 查看
<center>
不建议用了。
text-align:center
在父容器里水平居中 inline 文字,或 inline 元素
vertical-align:middle
垂直居中 inline 文字,inline 元素,配合
line-height
与 height 联手,垂直居中文字
margin:auto
.Absolute-Center.is-Overflow {
width: 50%;
height: 300px;
max-height: 100%;
margin: auto;
overflow: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}[/code]
.Absolute-Center.is-Image {
width: 50%;
height: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}[/code]
.is-Negative {
width: 300px;
height: 200px;
padding: 20px;
position: absolute;
top: 50%; left: 50%;
margin-left: -170px; /* (width + padding)/2 */
margin-top: -120px; /* (height + padding)/2 */
}[/code]
.Pos-Container.is-Table { display: table; }
.is-Table .Table-Cell {
display: table-cell;
vertical-align: middle;
}
.is-Table .Center-Block {
width: 50%;
margin: 0 auto;
}[/code]
不建议用了。
text-align:center
在父容器里水平居中 inline 文字,或 inline 元素
vertical-align:middle
垂直居中 inline 文字,inline 元素,配合
display:table,
display:table-cell,有奇效。
line-height
与 height 联手,垂直居中文字
margin:auto
注意:高度必须定义,建议加overflow: auto,防止内容溢出。
.Absolute-Center.is-Responsive {
width: 60%;
height: 60%;
min-width: 400px;
max-width: 500px;
padding: 40px;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
.Absolute-Center.is-Overflow {
width: 50%;
height: 300px;
max-height: 100%;
margin: auto;
overflow: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}[/code]
.Absolute-Center.is-Image {
width: 50%;
height: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}[/code]
.is-Negative {
width: 300px;
height: 200px;
padding: 20px;
position: absolute;
top: 50%; left: 50%;
margin-left: -170px; /* (width + padding)/2 */
margin-top: -120px; /* (height + padding)/2 */
}[/code]
.Pos-Container.is-Table { display: table; }
.is-Table .Table-Cell {
display: table-cell;
vertical-align: middle;
}
.is-Table .Center-Block {
width: 50%;
margin: 0 auto;
}[/code]
相关文章推荐
- 实习入职第十六天:seekBar自定义样式
- gulp-mockapi
- css基础
- JQ promise详解
- css3
- 问 如何使用css将select的边框以及右边的小三角形去掉?
- CSS 中如何把 Span 标签设置为固定宽度
- css中 Span 元素的 width 属性无效果原因及多种解决方案
- 【转】CSS3 @font-face
- CSS学习笔记
- flexbox相关
- css2
- css1
- CSS盒子模型
- 安卓开发——通过selector实现对TextView点击样式改变
- 【转】@font-face中#iefix的详解
- 【转】CSS3的自定义字体@font-face:如何将icon变成字体?
- 【转】基于display:table的CSS布局
- 5个简单,但很实用的css属性
- CSS定位