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

最新文章

  1. jdbc java数据库连接 7)获取插入数据的自增长值
  2. zip命令的常用选项
  3. Strus2第一次课:dom4j操作xml
  4. ubuntu方块乱码
  5. url-pattern
  6. HBase修改压缩格式及Snappy压缩实测分享
  7. 实现ajax
  8. Klayge 引擎的安装
  9. DML、DDL、DCL区别
  10. nginx 重启命令
  11. 独立线程中实现QT GUI
  12. 前端----表格的具体使用(jquery)
  13. 关于DocumentCompleted事件
  14. sqlserver 创建索引
  15. Cygwin下vim按方向键出现ABCD;
  16. Decorator模式设计模式
  17. (Java 多线程系列)Java 线程池(Executor)
  18. 听大佬学长RQY报告有感
  19. HDU 1102
  20. 【Spring】12、Spring Security 四种使用方式

热门文章

  1. Table control 相关
  2. ME11创建信息记录 Function
  3. 【docker】kubernetes集群一键部署包
  4. CodeForces - 799B T-shirt buying 【贪心】
  5. Android Weekly Notes Issue #261
  6. HTML语义化理解
  7. VC DLL方法的__declspec导入导出
  8. 经典数学问题&lt;手电过河问题&gt;的动态解法--问题规模扩展至任意大小
  9. VLAN(虚拟局域网)划分
  10. matlab的代码注释