css 实现垂直水平居中常用方法
2024-09-06 22:31:41
html
<div class="outer">
<div class="inner"></div>
</div>
基本样式
.outer {
background: #ddd;
width: 500px;
height: 500px;
}
.inner {
width: 100px;
height: 100px;
background: red;
}
一、宽高不固定
1.display: flex
.outer {
display: flex;
align-items: center;
justify-content: center;
}
2.absolute + transform
.outer {
position: relative;
}
.inner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
3.absolute + margin: auto
.outer {
position: relative
}
.inner {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
}
4.display: table-cell
.outer {
display: table-cell;
vertical-align: middle;
}
.inner {
margin: auto;
}
二、宽高固定
1.text-align + display: inline-block + vertical-align: middle
.outer {
text-align: center;
}
.outer::after {
content: '';
display: inline-block;
vertical-align: middle;
height: 500px;
}
.inner {
vertical-align: middle;
display: inline-block;
}
2.absolute + top + left + margin-top + margin-left
.outer {
position: relative;
}
.inner {
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
参考:https://github.com/louzhedong/blog/issues/2
最新文章
- jdbc java数据库连接 7)获取插入数据的自增长值
- zip命令的常用选项
- Strus2第一次课:dom4j操作xml
- ubuntu方块乱码
- url-pattern
- HBase修改压缩格式及Snappy压缩实测分享
- 实现ajax
- Klayge 引擎的安装
- DML、DDL、DCL区别
- nginx 重启命令
- 独立线程中实现QT GUI
- 前端----表格的具体使用(jquery)
- 关于DocumentCompleted事件
- sqlserver 创建索引
- Cygwin下vim按方向键出现ABCD;
- Decorator模式设计模式
- (Java 多线程系列)Java 线程池(Executor)
- 听大佬学长RQY报告有感
- HDU 1102
- 【Spring】12、Spring Security 四种使用方式