html结构:

<div class="parent">
<div class="child"></div>
</div>

方法一: display:flex

  .parent {
width: 500px;
height: 500px;
background: red;
display: flex;
align-items: center;
justify-content: center;
}
.child {
width: 100px;
height: 100px;
background: blue;
}

方法二:display:table-cel

  .parent{
width: 500px;
height: 500px;
background: red;
display: table-cell;
vertical-align: middle;
}
.child{
width: 100px;
height: 100px;
background: blue;
margin: auto;
}

方法三:绝对定位和0

  .parent{
width: 500px;
height: 500px;
background: red;
position: relative;
}
.child{
width: 100px;
height: 100px;
background: blue;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}

方法四:负边距

  .parent{
width: 500px;
height: 500px;
background: red;
position: relative;
}
.child{
width: 100px;
height: 100px;
background: blue;
position: absolute;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}

以上四种方法都可以完成用css实现子元素在父元素实现水平和垂直居中。

 

最新文章

  1. YII2生成增删改查
  2. 用CSS3实现文字描边
  3. c语言二叉树
  4. jstl标签库基础教程及其使用代码(一)。
  5. easyui反选全选和全不选代码以及方法的使用
  6. tap,touch,touchstart,事件与click事件的区别
  7. poj 2151
  8. [Python]更加Pythonic的多个List合并和Python的安利
  9. 转载:[Java]读取文件方法大全
  10. MySql 取一天的开始时间和结束时间
  11. Cloudera Development Kit(CDK) 简介
  12. Nape &quot;不动&quot;刚体的碰撞交互
  13. 前后端分离密码登陆加密RSA方案(java后端)
  14. php函数 array_column
  15. PHP使用RabbitMQ实例
  16. 交叉编译Python-3.6.0到aarch64/aarch32 —— 支持sqlite3
  17. React组件设计(转)
  18. TFlearn——(2)SVHN
  19. android学习-异步消息处理机制
  20. SpringMVC使用Cron表达式的定时器

热门文章

  1. php获取linux服务器CPU、内存、硬盘使用率的实现代码
  2. php面试专题---5、流程控制考点
  3. 115、TensorFlow变量的使用
  4. jmeter添加自定义扩展函数之if判断
  5. 关于JAVA的环境变量和那些jar包
  6. UVA1629_Cake slicing
  7. VC6.0在Win8,10下的兼容性调整
  8. HDU - 2181 C - 哈密顿绕行世界问题(DFS
  9. CSS实现不换行/自动换行/文本超出隐藏显示省略号
  10. 用python编写排序算法